var a:array[1..100]of longint;
n,m,p,i,x,y,xx,yy:longint;
begin
readln(n,p);
for i:=1 to p do
a[i]:=i;
for i:=1 to n do
begin
readln(x,y);
a[x]:=a[y];
end;
readln(m);
for i:=1 to m do
begin
readln(x,y);
xx:=x;
while a[xx]<>xx do
xx:=a[xx];
a[x]:=xx;
yy:=y;
while a[yy]<>yy do
yy:=a[yy];
a[y]:=a[yy];
if a[x]=a[y] then
writeln('YES')
else
writeln('NO');
end;
end.
n,m,p,i,x,y,xx,yy:longint;
begin
readln(n,p);
for i:=1 to p do
a[i]:=i;
for i:=1 to n do
begin
readln(x,y);
a[x]:=a[y];
end;
readln(m);
for i:=1 to m do
begin
readln(x,y);
xx:=x;
while a[xx]<>xx do
xx:=a[xx];
a[x]:=xx;
yy:=y;
while a[yy]<>yy do
yy:=a[yy];
a[y]:=a[yy];
if a[x]=a[y] then
writeln('YES')
else
writeln('NO');
end;
end.