#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;    using vvi = vector<vi>;
using vb = vector<bool>;   using vvb = vector<vb>;
using vl = vector<ll>;     using vvl = vector<vl>;
using vd = vector<double>; using vvd = vector<vd>;

#define REP(i,n) for(auto i = 0 * n; i < (n); ++i)
#define ALL(c) (c).begin(), (c).end()
#define FOR(i,s,e) for (ll i = s; i < (ll)e; i++)
#define TEN(x) ((ll)1e##x)

template<class T = size_t>
class HL_Decomposition{
#define PB push_back
	typedef vector<T> vt;
	T n;
public:
	template<class G>
	HL_Decomposition(const G & es, T root = 0) : n(es.size()), v2cmp(n), v2line(n), parent(n), depth(n, 0) {
		// to[v] : HL????§£????????????????????????????????????????????§?????? v ????¬??????\?????????????????????????????? (????????????n)
		vt to(n, n);

		// to[v] ????§????
		function<T(T, T)> dfs = [&](T c, T p) {
			parent[c] = p;
			// s  : c ????????¨????????¨?????¨????????????
			// ma : ???????????§?????§?????¨?????¨????????????
			T s = 1, ma = 0;
			for (auto e : es[c]) {
				if (e.to != p) {
					depth[e.to] = depth[c] + 1;
					T x = dfs(e.to, c);
					s += x;
					if (ma < x) ma = x, to[c] = e.to;
				}
			}
			return s;
		};
		dfs(root, n);

		// HL????§£
		queue<T> q({ root });
		while (!q.empty()) {
			T i = component.size();
			component.PB({});
			for (T v = q.front(); v != n; v = to[v]) {
				v2cmp[v] = i;
				v2line[v] = component[i].size();
				component[i].PB(v);
				for (auto e : es[v]) {
					if (e.to != parent[v] && e.to != to[v]) q.push(e.to);
				}
			}
			q.pop();
		}
	}

	void for_each(T u, T v, function<void(T, T)> f) const {
		if (v2cmp[u] != v2cmp[v]) {
			if (v2cmp[u] > v2cmp[v]) swap(u, v);
			f(component[v2cmp[v]][0], v);
			for_each(u, parent[component[v2cmp[v]][0]], f);
		} else {
			if (depth[u] > depth[v]) swap(u, v);
			f(u, v);
		}
	}

	T lca(T u, T v) const {
		if (v2cmp[u] == v2cmp[v]) return depth[u] < depth[v] ? u : v;
		if (v2cmp[u] > v2cmp[v]) swap(u, v);
		return lca(u, parent[component[v2cmp[v]][0]]);
	}
	T head_vertex(T i_cmp) const { return component[i_cmp][0]; }
	T parent_vertex(T i_cmp) const { return parent[head_vertex(i_cmp)]; }
	T parent_component(T i_cmp) const { return v2cmp[parent_vertex(i_cmp)]; }

	// ????????????????????? i ??? ????????????????????? j ????\???? ??? i < j

	// v2cmp[v]     : ?????? v ???????±?????????????????????????????????????????????????
	// v2line[v]    : ?????? v ???????±???????????????????????????????????????§?????????????????????
	// parent[v]    : ?????? v ???????????????????????????????????? ????????????????????´?????? n ???
	// depth[v]     : ?????? v ?????±???(??????0)
	// component[i] : i??????????????????????????????????±??????????????????????????????????????????????
	vt v2cmp, v2line, parent, depth;
	vector<vt> component;
};

struct Edge{ size_t to; };

int main() {
#ifdef INPUT_FROM_FILE
	ifstream cin("sample.in");
	ofstream cout("sample.out");
#endif
	cin.tie(0);
	ios_base::sync_with_stdio(false);
	cout << fixed << setprecision(50);

    size_t n; cin >> n;
    vector<vector<Edge>> g(n);
	REP(i, n) {
		size_t k; cin >> k;
		REP(j, k) {
			size_t c; cin >> c;
			g[i].push_back({ c });
		}
	}
    HL_Decomposition<> hl(g);
 
    size_t q; cin >> q;
	REP(i, q) {
		size_t u, v; cin >> u >> v;
		cout << hl.lca(u, v) << "\n";
    }

	return 0;
}

Measured memory_bytes: 24068