# include <iostream>
# include <algorithm>
#include <array>
# include <cassert>
#include <cctype>
#include <climits>
#include <numeric>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <tuple>
# include <utility>
# include <stack>
# include <queue>
# include <list>
# include <bitset>
# include <complex>
# include <chrono>
# include <random>
# include <limits.h>
# include <unordered_map>
# include <unordered_set>
# include <deque>
# include <cstdio>
# include <cstring>
#include <stdio.h>
#include<time.h>
#include <stdlib.h>
#include <cstdint>
#include <cfenv>
#include<fstream>
//#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
long long MOD = 1000000000 + 7;// 998244353;// ;
constexpr long long INF = numeric_limits<LL>::max() / 2;
const double PI = acos(-1);
#define fir first
#define sec second
#define thi third
#define debug(x) cerr<<#x<<": "<<x<<'\n'
typedef pair<LL, LL> Pll;
typedef pair<LL, pair<LL, LL>> Ppll;
typedef pair<LL, pair<LL, bitset<100001>>> Pbll;
typedef pair<LL, pair<LL, vector<LL>>> Pvll;
typedef pair<LL, LL> Vec2;
struct Tll { LL first, second, third; };
struct Fll { LL first, second, third, fourd; };
typedef pair<LL, Tll> Ptll;
#define rep(i,rept) for(LL i=0;i<rept;i++)
#define Rrep(i,mf) for(LL i=mf-1;i>=0;i--)
void YN(bool f) {
	if (f)
		cout << "YES" << endl;
	else
		cout << "NO" << endl;
}
void yn(bool f) {
	if (f)
		cout << "Yes" << endl;
	else
		cout << "No" << endl;
}
struct Edge { LL to, cost; };
struct edge { LL from, to, cost; };
vector<vector<Edge>>g;
vector<edge>edges;
vector<LL>v;
map<LL, LL>ma;
set<Pll>st;
LL h, w, n, m, k, t, s, p, q, last, cnt, sum, ans, dp[210000],a[210000], b[210000];
string str, ss;
bool f;
char c;
struct HLD {
	LL n;
	LL cnt = 0;
	vector<vector<Edge>>g;
	vector<LL>vid, head, nex,dep,lab,par;
	HLD(vector<vector<Edge>>G) :g(G), n(G.size()), vid(n), head(n), nex(n), dep(n), lab(n), par(n) {
		dfs(0, -1);
		build(0);
	}
	void dfs(LL cur,LL pa) {
		LL mx = 0;
		nex[cur] = -1;
		rep(i, g[cur].size()) {
			Edge e = g[cur][i];
			if (e.to == pa)continue;
			dep[e.to] = dep[cur] + 1;
			par[e.to] = cur;
			if (mx < dep[e.to]) {
				mx = dep[e.to];
				nex[cur] = e.to;
			}
			dfs(e.to, cur);
		}
	}
	void build(LL root) {
		queue<LL>q;
		q.push(root);
		vector<LL>Par(n), Dep(n);
		while (!q.empty()) {
			LL h = q.front();
			for (int i = h; i != -1; i = nex[i]) {
				vid[i] = cnt;
				lab[cnt] = i;
				head[cnt] = vid[h];
				Par[cnt] = vid[par[i]];
				Dep[cnt] = dep[i];
				cnt++;
				rep(j, g[i].size()) {
					LL u = g[i][j].to;
					if (u == nex[i] || u == par[i])continue;
					q.push(u);
				}
			}
			q.pop();
		}
		swap(Par, par);
		swap(Dep, dep);
	}
	LL lca(LL u,LL v) {
		u = vid[u];
		v = vid[v];
		while (head[u] != head[v]) {
			if (dep[head[u]] < dep[head[v]])swap(u, v);
			u = par[head[u]];
		}
		if (dep[u] > dep[v])swap(u, v);
		return  lab[u];
	}
};
int main() {
	cin >> n;
	g.resize(n);
	rep(i, n) {
		LL x, y;
		cin >> x;
		rep(j, x) {
			cin >> y;
			g[y].push_back(Edge{ i,1 });
			g[i].push_back(Edge{ y ,1 });
		}
	}
	HLD hld(g);
	cin >> m;
	rep(i, m) {
		LL x, y;
		cin >> x >> y;
		cout << hld.lca(x, y) << endl;
	}
	return 0;
}


Measured memory_bytes: 34376