#include<stdio.h>
#include<vector>
using namespace std;
int root[10001];
int num;
int findRoot(int x)
{
if (root[x] == -1)
{
return x;
}
else
{
int tmp = findRoot(root[x]);
root[x] = tmp;
return tmp;
}
}
int main()
{
num = 0;
for (int i = 0; i < 10001; i++)
{
root[i] = -1;
}
int N;
scanf("%d", &N);
for (int i = 0; i < N; i++)
{
int K;
scanf("%d", &K);
vector<int> birds;
for (int j = 0; j < K ; j++)
{
int bird;
scanf("%d", &bird);
if (bird > num)
{
num = bird;
}
birds.push_back(bird);
}
for(int j=0;j<K-1;j++)
{
int rootA = findRoot(birds[j]);
int rootB = findRoot(birds[j + 1]);
if (rootA < rootB)
{
root[rootB] = rootA;
}
else if (rootB < rootA)
{
root[rootA] = rootB;
}
}
}
int treeNum=0;
for (int i = 1; i <= num; i++)
{
if (root[i] == -1)
{
treeNum++;
}
}
printf("%d %d\n", treeNum, num);
int Q;
scanf("%d", &Q);
for (int i = 0; i < Q; i++)
{
int birdA, birdB;
scanf("%d%d", &birdA, &birdB);
if (findRoot(birdA) == findRoot(birdB))
{
printf("Yes\n");
}
else
{
printf("No\n");
}
}
return 0;
}