#include "bits/stdc++.h"
using namespace std;

//?????????
#pragma region MACRO 
#define putans(x)  std::cerr << "[ answer ]: " ; cout << (x) << endl
#define dputans(x) std::cerr << "[ answer ]: "; cout << setprecision(40) << (double)(x) << endl
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define RREP(i,n,a) for(int i=(int)(n-1); i>= a; i--)
#define rep(i,n) REP(i,0,n)
#define rrep(i,n) RREP(i,n,0)
#define all(a) begin((a)),end((a))
#define mp make_pair
#define exist(container, n) ((container).find((n)) != (container).end())
#define equals(a,b) (fabs((a)-(b)) < EPS)
#ifdef _DEBUG //???????????????????????????????????????????????????
std::ifstream ifs("data.txt");
#define put ifs >>
#else //?????£????????????????????§?????????????????????
#define put cin >>
#endif
#pragma endregion
//???????????°??????????????´
#pragma region CODING_SUPPORT
#ifdef _DEBUG
#define dbg(var0) { std::cerr << ( #var0 ) << "=" << ( var0 ) << endl; }
#define dbg2(var0, var1) { std::cerr << ( #var0 ) << "=" << ( var0 ) << ", "; dbg(var1); }
#define dbg3(var0, var1, var2) { std::cerr << ( #var0 ) << "=" << ( var0 ) << ", "; dbg2(var1, var2); }
#define dbgArray(a,n) {std::cerr << (#a) << "=";  rep(i,n){std::cerr <<(a[i])<<",";} cerr<<endl;} 
#else
#define dbg(var0) {}
#define dbg2(var0, var1) {}
#define dbg3(var0, var1, var2) {}
#define dbgArray(a,n) {}
#endif 
#pragma endregion 
//typedef????????????????????????????¶????????????§?????????
#pragma region TYPE_DEF
typedef long long ll;
typedef pair<int, int> pii; typedef pair<string, string> pss; typedef pair<int, string>pis;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
#pragma endregion
//??????????????°(???????????????????????§??????)
#pragma region CONST_VAL
#define PI (2*acos(0.0))
#define EPS (1e-10)
#define MOD (ll)(1e9 + 7)
#define INF (ll)(2*1e9)
#pragma endregion

const static int MAX_V = 100000;

vector<int> G[MAX_V];
int root = 0;

int vs[MAX_V * 2 - 1];
int depth[MAX_V * 2 - 1];
int id[MAX_V];

//?????????????????????????????????????±?????????????????????°???????????¨
//int double ??¨??????numeric???????????£?????????????????????
//n ????´???°
//invalid_value ???????????°???????????????a????????????marge(a,invalidvalue) = a, marge(invalid_value, a) = a?????¨??????????????????
class SegmentTreeInt {
public:
	SegmentTreeInt(int n, int invalid_value);
	~SegmentTreeInt();
	void SetElem(int k, int val);
	int GetCount();
	int quary(int a, int b);
	int GetElem(int k);

	//?????????????????¢??°???????????????????????????
	//invalid_value??????????????£?????°????????????????????????
	bool IsInvalidValue(int value) {
		return value == mInvalidValue;
	}
	//a??¨b?????? a???b????±?????????¢??°???????????§?¨?????????????????????§?±???????
	int Marge(int a, int b) {
		if (IsInvalidValue(a))return b;
		if (IsInvalidValue(b)) return a;
		if (depth[a] >= depth[b]) return b;
		return a;
	}
private:
	vector<int>
		mTree;
	int mCount;
	int mInvalidValue;
	void Update(int k, int val);
	int InternalQuary(int a, int b, int k, int l, int r);
};

//?????´??????????????????(??????????????¨???????????´??????)
SegmentTreeInt::SegmentTreeInt(int n, int invalid_value) {
	int bin_n = 1;
	while (bin_n < n) bin_n *= 2;
	mTree.resize(2 * bin_n);
	mInvalidValue = invalid_value;
	mCount = bin_n;
	for (int i = 0; i < 2 * bin_n - 1; i++) {
		mTree[i] = invalid_value;
	}
}
SegmentTreeInt::~SegmentTreeInt() {
}
void SegmentTreeInt::SetElem(int k, int val) { Update(k, val); }
int SegmentTreeInt::GetElem(int k) { return mTree[mCount - 1 + k]; }
int SegmentTreeInt::GetCount() { return mCount; }
//????????´??°
void SegmentTreeInt::Update(int k, int val) {
	k += mCount - 1;
	mTree[k] = val;
	while (k > 0) {
		k = ( k - 1 ) / 2;
		mTree[k] = Marge(mTree[2 * k + 1], mTree[2 * k + 2]);
	}
}
//??????[a,b]?????????O(ln(Count))??§?±??????? b < GetCount??¨???????????¨
//ex. a = 0 , b = 2 ?????? 0,1,2?????????
int SegmentTreeInt::quary(int a, int b) {
	return InternalQuary(a, b + 1, 0, 0, mCount);
}
//?????¨?????????
int SegmentTreeInt::InternalQuary(int a, int b, int k, int l, int r) {
	if (r <= a || b <= l)return mInvalidValue;
	if (a <= l && r <= b)return mTree[k];
	else {
		int vl = InternalQuary(a, b, k * 2 + 1, l, ( l + r ) / 2);
		int vr = InternalQuary(a, b, k * 2 + 2, ( l + r ) / 2, r);
		return Marge(vl, vr);
	}
}
//???????????§?????´??????

SegmentTreeInt rmq(2*MAX_V-1,INF);

void dfs(int v, int p, int d, int &k) {
	id[v] = k;
	vs[k] = v;
	depth[k++] = d;
	rep(i, G[v].size()) {
		if (G[v][i] != p) {
			dfs(G[v][i], v, d + 1, k);
			vs[k] = v;
			depth[k++] = d;
		}
	}
}
//
void init(int V) {
	int k = 0;
	dfs(root,-1,0,k);
	rep(i, V * 2) {
		rmq.SetElem(i,i);
	}
}

int LCA(int u, int v) {
	return vs[rmq.quary(min(id[u], id[v]), max(id[u], id[v]))];
}

int main() {
	int n; put n;
	rep(i, n) {
		int k; put k;
		rep(j, k) {
			int c; put c;
			G[i].push_back(c);
		}
	}
	int q; put q;
	init(n);
	rep(i, q) {
		int u, v; put u >> v;
		cout << LCA(u,v) << endl;
	}
END:

	return 0;
}

Measured memory_bytes: 19892