#include <bits/stdc++.h>
using namespace std;
constexpr int N = 2E4 + 10;
int f[N];
int find(int x) {
if (x != f[x]) return f[x] = find(f[x]);
return f[x];
}
int main() {
int n;
cin >> n;
for (int i = 1; i < N; i++) f[i] = i;
set<int> S;
for (int i = 1; i <= n; i++) {
int k;
cin >> k;
for (int j = 1; j <= k; j++) {
int p;
cin >> p;
S.insert(p);
p += n;
int fk = find(i), fp = find(p);
if (fk >= 1 && fk <= n) f[fp] = fk;
else f[fk] = fp;
}
}
int res = 0;
for (int i = 1; i <= n; i++) {
if (i == find(i)) {
res++;
}
}
cout << (int)S.size() << " " << res << "\n";
int q;
cin >> q;
for (int i = 0; i < q; i++) {
int a, b;
cin >> a >> b;
a += n, b += n;
int fa = find(a), fb = find(b);
if (fa == fb) cout << "Y" << "\n"[i == q - 1];
else cout << "N" << "\n"[i == q - 1];
}
return 0;
}