#include <iostream>
#include <list>
 
using namespace std;
 
#define gc getchar_unlocked
#define pc putchar_unlocked
inline int scan(){register int n=0,c=gc();while(c<'0'||c>'9')c=gc();while(c<='9'&&c>='0')n=(n<<1)+(n<<3)+c-'0',c=gc();return n;} 

class Graph
{
    int V;    // No. of vertices
    list<int> *adj;    // Pointer to an array containing adjacency lists
    int DFSUtil(int a ,int b, bool visited[]);  // A function used by DFS
public:
    Graph(int V);   // Constructor
    void addEdge(int a ,int b);   // function to add an edge to graph
    int DFS(int a ,int b);    // DFS traversal of the vertices reachable from v
};
 
Graph::Graph(int V)
{
    this->V = V;
    adj = new list<int>[V];
}
 
void Graph::addEdge(int v, int w)
{
    adj[v].push_back(w);
    adj[w].push_back(v);
}
 
int Graph::DFSUtil(int a, int b, bool visited[])
{
    // Mark the current node as visited and print it
    int ret=0;
    visited[a] = true;
    //out <<"Nodes= " << a <<"\n";
 	if(a == b) return 1;
    // Recur for all the vertices adjacent to this vertex
    list<int>::iterator i;
    for(i = adj[a].begin(); i != adj[a].end(); ++i)
        if(!visited[*i]){
            ret = DFSUtil(*i,b, visited);
            return ret;
            }
         
}
int Graph::DFS(int a, int b)
{
    // Mark all the vertices as not visited
    bool *visited = new bool[V];
    int ret=0;
    for(int i = 0; i < V; i++)
        visited[i] = false;
    ret = DFSUtil(a,b,visited);
    return ret;
}
 
int main()
{
    // Create a graph given in the above diagram
    int t,A,B,N,M,Q;
    t=scan();
    Graph g(200);
    while(t--){
    	N=scan();
    	M=scan();
    	while(M--){
    		A=scan();
    		B=scan();
    		if(A<N && B<N) g.addEdge(A,B);
    	}
    }
    //
    Q=scan();
    while(Q--){
    A=scan();
    B=scan();
    if(A<N && B<N){
      if(g.DFS(A,B)==1)cout <<"YES\n";
      else cout << "NO\n";
    }
    else cout << "NO\n";
    }
    
 
    return 0;
}