#include <vector>
#include <utility>
#include <cassert>
#include <stack>

struct HeavyLightDecomposition {
public:
	using value_type = int;
	using const_reference = const value_type &;
	using size_type = std::size_t;
	
	HeavyLightDecomposition(const std::vector<std::vector<size_type>> &g, size_type root_num = 0) {
		build(g, root_num);
	}
	
	size_type size() const noexcept {
		return size_;
	}
	
	size_type lca(size_type x, size_type y) const {
		assert(x < size() && y < size());
		if (node_idx[x] == node_idx[y]) return ver_num[node_idx[x]][std::max(path_idx[x], path_idx[y])];
		std::stack<size_type> stk[2];
		for (size_type i = 0; i < 2; ++i) {
			stk[i].emplace(-1);
			stk[i].emplace(i == 0 ? x : y);
			while (true) {
				size_type top_num = ver_num[node_idx[stk[i].top()]].back();
				size_type par = par_num[top_num];
				if (top_num == par) break;
				stk[i].emplace(par);
			}
		}
		
		size_type lca_pred[2];
		while (node_idx[stk[0].top()] == node_idx[stk[1].top()]) {
			for (size_type i = 0; i < 2; ++i) {
				lca_pred[i] = stk[i].top();
				stk[i].pop();
			}
		}
		return ver_num[node_idx[lca_pred[0]]][std::max(path_idx[lca_pred[0]], path_idx[lca_pred[1]])];
	}
	
private:
	size_type size_;
	std::vector<size_type> node_idx; // node_idx[i] := 頂点 i が属する分解後の列の index
	std::vector<size_type> path_idx; // path_idx[i] := 頂点 i が属する分解後の列での頂点 i の index
	std::vector<std::vector<size_type>> ver_num; // [i][j] := 分解後の列 i の j 番目の要素の分解前の頂点番号
	
	std::vector<size_type> par_num; // par_num[i] := 頂点 i の親の頂点番号
	std::vector<std::vector<size_type>> childs; // childs[i] := 頂点 i の子の頂点番号
	std::vector<value_type> value; // <<<未実装>>>, 頂点や辺の値
	
	void build(const std::vector<std::vector<size_type>> &g, size_type root_num = 0) {
		size_ = g.size();
		node_idx.resize(size());
		path_idx.resize(size());
		par_num.resize(size());
		childs.resize(size());
		
		std::vector<bool> done(size(), 0);
		std::vector<size_type> sub_size(size(), 0);
		
		std::stack<std::pair<size_type, size_type>> stk;
		stk.emplace(root_num, 0);
		par_num[root_num] = root_num;
		done[root_num] = true;
		
		while (!stk.empty()) {
			int u = stk.top().first;
			int idx = stk.top().second;
			stk.pop();
			
			if (idx < g[u].size()) {
				stk.emplace(u, idx + 1);
				
				int v = g[u][idx];
				if (!done[v]) {
					done[v] = true;
					par_num[v] = u;
					childs[u].push_back(v);
					stk.emplace(v, 0);
				}
			}
			else {
				size_type mx_idx, mx_size = 0, size_cnt = 1;
				for (size_type child : childs[u]) {
					size_cnt += sub_size[child];
					if (mx_size < sub_size[child]) {
						mx_size = sub_size[child];
						mx_idx = child;
					}
				}
				
				if (size_cnt == 1) ver_num.emplace_back();
				size_type cur_node_idx = size_cnt == 1 ? ver_num.size() - 1 : node_idx[mx_idx];
				sub_size[u] = size_cnt;
				node_idx[u] = cur_node_idx;
				path_idx[u] = ver_num[node_idx[u]].size();
				ver_num[node_idx[u]].emplace_back(u);
			}
		}
	}
};

#include <cstdio>
int main() {
	int N;
	scanf("%d", &N);
	
	using HLD = HeavyLightDecomposition;
	std::vector<std::vector<HLD::size_type>> g(N);
	for (int i = 0; i < N; ++i) {
		int k;
		scanf("%d", &k);
		for (int j = 0; j < k; ++j) {
			int c;
			scanf("%d", &c);
			g[i].emplace_back(c);
			g[c].emplace_back(i);
		}
	}
	
	HLD hld(g);
	
	// if (1) {
	// 	for (int i = 0; i < hld.ver_num.size(); ++i) {
	// 		printf(": ");
	// 		for (int j = 0; j < hld.ver_num[i].size(); ++j) {
	// 			printf("%d ", hld.ver_num[i][j]);
	// 		}
	// 		puts("");
	// 	}
		
	// 	puts("-- childs information --");
	// 	for (int i = 0; i < N; ++i) {
	// 		if (hld.childs[i].size() == 0) continue;
	// 		printf("i=%d : ", i);
	// 		for (int j : hld.childs[i]) {
	// 			printf("%d ", j);
	// 		}
	// 		puts("");
	// 	}
		
	// 	puts("-- node_index information --");
	// 	for (int i = 0; i < hld.size(); ++i) {
	// 		printf("%d ", hld.node_idx[i]);
	// 	}
	// 	puts("");
	// 	puts("-- path_index information --");
	// 	for (int i = 0; i < hld.size(); ++i) {
	// 		printf("%d ", hld.path_idx[i]);
	// 	}
	// 	puts("");
	// 	puts("-- par_num information --");
	// 	for (int i = 0; i < hld.size(); ++i) {
	// 		printf("%d ", hld.par_num[i]);
	// 	}
	// 	puts("");
	// }
	
	int Q;
	scanf("%d", &Q);
	while (Q--) {
		int u, v;
		scanf("%d %d", &u, &v);
		printf("%d\n", hld.lca(u, v));
	}
}



Measured memory_bytes: 20436