#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <cstring>
#include <numeric>
#include <algorithm>
#include <functional>
#include <array>
#include <map>
#include <queue>
#include <limits.h>
#include <set>
#include <stack>
#include <random>
#include <complex>
#include <unordered_map>
#define rep(i,s,n) for(int i = (s); (n) > i; i++)
#define REP(i,n) rep(i,0,n)
#define RANGE(x,a,b) ((a) <= (x) && (x) <= (b))
#define DUPLE(a,b,c,d) (RANGE(a,c,d) || RANGE(b,c,d) || RANGE(c,a,b) || RANGE(d,a,b))
#define INCLU(a,b,c,d) (RANGE(a,c,d) && (b,c,d))
#define PW(x) ((x)*(x))
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define MODU 1000000007
#define bitcheck(a,b)   ((a >> b) & 1)
#define bitset(a,b)      ( a |= (1 << b))
#define bitunset(a,b)    (a &= ~(1 << b))
#define MP(a,b) make_pair((a),(b))
#define Manh(a,b) (abs((a).first-(b).first) + abs((a).second - ((b).second))
#define pritnf printf
#define scnaf scanf
#define itn int
#define PI 3.141592653589


#define izryt bool

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template<typename A, size_t N, typename T>
void Fill(A(&array)[N], const T &val) {
	std::fill((T*)array, (T*)(array + N), val);
}
pii Dir[8] = { //?§????
	{ 0 ,1 },{ -1 ,0 },{ 1 ,0 },{ 0 ,-1 },
	{ 1 ,1 },{ 1 ,-1 },{ -1 ,1 },{ -1 ,-1 }
};
class Graph {
public:
	int vn;
	int sumcost = 0;
	vector<vector<pii>> g;

	Graph(int n) {
		vn = n;
		g.resize(n);
	}
	virtual void con(int a, int b, int w) = 0;
	int getWeight(int f, int t) {
		auto itr = lower_bound(ALL(g[f]), make_pair(t, INT_MIN));
		if (itr != g[f].end())
			return itr->second;
		return INT_MIN;
	}
	int Costsum() {
		return sumcost;
	}
};

class BiDGraph : public Graph {//??????
public:
	BiDGraph(int n) : Graph(n) {}

	void con(int a, int b, int w = 1) {
		g[a].push_back({ b,w });
		g[b].push_back({ a, w });
		sumcost++;
	}
};


class Heavy_Light_Decomposition {
public:
	BiDGraph g, compg;
	vector<int> sz,depth,chain, par;

	Heavy_Light_Decomposition(BiDGraph graph, int root):
		g(graph), compg(graph.vn), sz(graph.vn,1), depth(graph.vn), chain(graph.vn),par(graph.vn){
		getsz(root, root);
		HLD(root, -1, root);
	}

	int getsz(int cur, int p) {
		par[cur] = p;
		for (auto itr : g.g[cur])
			if (p != itr.first) 
				depth[itr.first] = depth[cur]+1,sz[cur] += getsz(itr.first, cur);
		return sz[cur];
	}

	void HLD(int cur, int p, int head) {
		chain[cur] = head;
		pii Maxc = {-1,-1};
		for (auto itr : g.g[cur]) 
			if (itr.first != p)
				Maxc = max(Maxc, { sz[itr.first], itr.first });
		

		for (auto itr : g.g[cur]) 
			if (itr.first != p)
				HLD(itr.first, cur, itr.first == Maxc.second ? head : itr.first);
	}



	int lca(int a, int b) {
		if (chain[a] == chain[b]) return depth[a] < depth[b] ? a : b;
		if (depth[chain[a]] > depth[chain[b]])
			return lca(par[chain[a]], b);
		return lca(a, par[chain[b]]);
	}


};

signed main() {
	int n;
	scnaf("%d", &n);
	BiDGraph g(n);
	REP(i, n) {
		int m;
		scanf("%d", &m);
		REP(j, m) {
			int a;
			scnaf("%d", &a);
			g.con(i,a);
		}
	}

	Heavy_Light_Decomposition hld(g, 0);

	int q;
	scanf("%d", &q);
	REP(i, q) {
		int a, b;
		scanf("%d %d", &a, &b);

		printf("%d\n", hld.lca(a,b));
	}

	return 0;
}

Measured memory_bytes: 27312