{
TASK: Y1984
LANG: PASCAL
}

var
  group: array[1..2000] of Integer;
  front: array[1..2000] of Char;
  n,m,i,q: Integer;
  t,k,r: Integer;
  br: Integer;

begin
  readln(n);
  readln(m);
  br:= 1;
  for i:= 0 to n-1 do
  begin
    front[i]:= 'c';
    group[i]:= 0;
  end;
  for i:= 1 to m do
  begin
    readln(t,k,r);
    if (front[t]='c') and (front[k]='c') then
    begin
      if r=0 then
      begin
        front[t]:= 'a';
        front[k]:= 'a';
        if (group[t]=0) and (group[k]=0) then
        begin
          group[t]:= br;
          group[k]:= br;
          br:= br+1;
        end;
        if (group[t]<>0) or (group[k]<>0) then
        begin
          if group[t]<>0 then
            group[k]:= group[t];
          if group[k]<>0 then
            group[t]:= group[k];
        end;
      end;
      if r=1 then
      begin
        front[t]:= 'a';
        front[k]:= 'b';
        if (group[t]=0) and (group[k]=0) then
        begin
          group[t]:= br;
          group[k]:= br;
          br:= br+1;
        end;
        if (group[t]<>0) or (group[k]<>0) then
        begin
          if group[t]<>0 then
            group[k]:= group[t];
          if group[k]<>0 then
            group[t]:= group[k];
        end;
      end;
    end;
    if (front[t]<>'c') or (front[k]<>'c') then
    begin
      if (front[t]<>'c') and (front[k]='c') then
      begin
        if r=0 then
        begin
          front[k]:= front[t];
          if (group[t]=0) and (group[k]=0) then
          begin
            group[t]:= br;
            group[k]:= br;
            br:= br+1;
          end;
          if (group[t]<>0) or (group[k]<>0) then
          begin
            if group[t]<>0 then
              group[k]:= group[t];
            if group[k]<>0 then
              group[t]:= group[k];
          end;
        end;
        if r=1 then
        begin
          if front[t]='a' then
            front[k]:= 'b';
          if front[t]='b' then
            front[k]:= 'a';
          if (group[t]=0) and (group[k]=0) then
          begin
            group[t]:= br;
            group[k]:= br;
            br:= br+1;
          end;
          if (group[t]<>0) or (group[k]<>0) then
          begin
            if group[t]<>0 then
              group[k]:= group[t];
            if group[k]<>0 then
              group[t]:= group[k];
          end;
        end;
      end;
      if (front[t]='c') and (front[k]<>'c') then
      begin
        if r=0 then
        begin
          front[t]:= front[k];
          if (group[t]=0) and (group[k]=0) then
          begin
            group[t]:= br;
            group[k]:= br;
            br:= br+1;
          end;
          if (group[t]<>0) or (group[k]<>0) then
          begin
            if group[t]<>0 then
              group[k]:= group[t];
            if group[k]<>0 then
              group[t]:= group[k];
          end;
        end;
        if r=1 then
        begin
          if front[k]='a' then
            front[t]:= 'b';
          if front[k]='b' then
            front[t]:= 'a';
          if (group[t]=0) and (group[k]=0) then
          begin
            group[t]:= br;
            group[k]:= br;
            br:= br+1;
          end;
          if (group[t]<>0) or (group[k]<>0) then
          begin
            if group[t]<>0 then
              group[k]:= group[t];
            if group[k]<>0 then
              group[t]:= group[k];
          end;
        end;   
      end;
    end;
  end;
  readln(q);
  for i:= 1 to q do
  begin
    readln(t,k);
    if (front[t]=front[k]) and (group[t]=group[k]) then
      writeln('0');
    if ((front[t]='a') and (front[k]='b')) or ((front[t]='b') and (front[k]='a')) then
      if group[t]=group[k] then
        writeln('1');
     if group[t]<>group[k] then
       writeln('2');  
  end;
end.