#define _CRT_SECURE_NO_WARNINGS
#include "bits/stdc++.h"

#define PB push_back
#define PF push_front
#define LB lower_bound
#define UB upper_bound
#define fr(x) freopen(x,"r",stdin)
#define fw(x) freopen(x,"w",stdout)
#define iout(x) printf("%d\n",x)
#define lout(x) printf("%lld\n",x)
#define REP(x,l,u) for(ll x = l;x<u;x++)
#define RREP(x,l,u) for(ll x = l;x>=u;x--)
#define complete_unique(a) a.erase(unique(a.begin(),a.end()),a.end())
#define mst(x,a) memset(x,a,sizeof(x))
#define all(a) a.begin(),a.end()
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define MP make_pair
#define sqr(x) ((x)*(x))
#define lowbit(x) ((x)&(-(x)))
#define lson (ind<<1)
#define rson (ind<<1|1)
#define se second
#define fi first
#define sz(x) ((int)x.size())
#define EX0 exit(0);

typedef  long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
using namespace std;
typedef vector<ll> VLL;
typedef vector<int> VI;
const int block_size = 320;
typedef complex<ll> point;
const ll mod = 1e9 + 7;
const ll inf = 1e9 + 7;
const ll INF = 1e15;
const ld eps = 1e-9;
const db PI = atan(1) * 4;

template<typename T, typename S>inline bool upmin(T&a, const S&b) { return a > b ? a = b, 1 : 0; }
template<typename T, typename S>inline bool upmax(T&a, const S&b) { return a < b ? a = b, 1 : 0; }

ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a%b); }
ll pow(ll a, ll b) {
	if (b == 0) {
		return 1;
	}
	if (b == 1) {
		return a;
	}
	ll ans = pow(a, b / 2);
	ans *= ans;
	ans *= b % 2 == 1 ? a : 1;
	return ans;
}

namespace SOLVE {
	int n, a, b;
	vector<VI> edges;
	VI path;
	VI depths;
	VI visited;
	VI firstpath;

	struct SegTree {
		static const int maxn = 200005;

		struct node {
			int l, r, depth, og;
		};

		node no[maxn * 4];
		void push_up(int ind) {
			if (no[lson].depth < no[rson].depth) {
				no[ind].depth = no[lson].depth;
				no[ind].og = no[lson].og;
			}
			else {
				no[ind].depth = no[rson].depth;
				no[ind].og = no[rson].og;
			}
		}

		void push_down(int ind) {
		}

		void build(int l, int r, int ind) {
			no[ind].l = l;
			no[ind].r = r;
			no[ind].depth = 1e9;
			no[ind].og = 1e9;

			if (l == r) {
				no[ind].depth = depths[l];
				no[ind].og = path[l];
			}

			else {
				int mid = (l + r) / 2;
				build(l, mid, lson);
				build(mid + 1, r, rson);
				push_up(ind);
			}
		}
		
		void query(int l, int r, int ind, int& ans, int& depth) {
			if (l > no[ind].r || r < no[ind].l) {
				ans = 1e9;
				depth = 1e9;
				return;
			}
			if (l <= no[ind].l && no[ind].r <= r) {
				ans = no[ind].og;
				depth = no[ind].depth;
			}
			else {
				int ans1, ans2, depth1, depth2;
				push_down(ind);
				query(l, r, lson, ans1, depth1);
				query(l, r, rson, ans2, depth2);
				push_up(ind);

				if (depth1 < depth2) {
					ans = ans1;
					depth = depth1;
				}
				else {
					ans = ans2;
					depth = depth2;
				}
				

			}
		}

		void show(int n) {
			REP(i, 0, n) {
				cout << no[i].depth << ' ';
			} cout << endl;
			REP(i, 0, n) {
				cout << no[i].og << ' ';
			} cout << endl;
		}
	};



	SegTree tree;

	void dfs(int v, int depth) { // while traversing, leave a path
		visited[v] = 1;
		firstpath[v] = path.size();
		path.push_back(v);
		depths.push_back(depth);
		

		for (int u : edges[v]) {
			if (!visited[u]) {
				dfs(u, depth+1);
				path.push_back(v);
				depths.push_back(depth);
			}
		}
	}

	void main() {
		cin >> n;
		edges.resize(n);
		visited.resize(n);
		firstpath.resize(n);

		REP(i, 0, n) {
			cin >> a;
			REP(j, 0, a) {
				cin >> b;
				edges[i].push_back(b);
			}
		}

		dfs(0, 0);
		/*
		for (int a : path) {
			cout << a << ' ';
		} cout << endl;
		for (int a : depths) {
			cout << a << ' ';
		} cout << endl;
		*/

		tree.build(0, sz(path) - 1, 1);

		int ans, depth, q;

		cin >> q;
		REP(i, 0, q) {
			cin >> a >> b;
			int small = min(firstpath[a], firstpath[b]);
			int big = max(firstpath[a], firstpath[b]);
			tree.query(small, big, 1, ans, depth);
			cout << ans << endl;
		}
		


	}
}


signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int t;
	//    in(t);
	t = 1;
	while (t--) {
		SOLVE::main();

	}

	return 0;
}



Measured memory_bytes: 23628