Problem:
LCA: Lowest Common Ancestor

For a rooted tree, find the lowest common ancestor of two nodes u and v.

The given tree consists of n nodes and every node has a unique ID from 0 to n-1 where 0 is the root.

Input

n
k0 c1 c2 ... ck0
k1 c1 c2 ... ck1
:
kn-1 c1 c2 ... ckn-1
q
u1 v1
u2 v2
:
uq vq

The first line of the input includes an integer n, the number of nodes of the tree.

In the next n lines, the information of node i is given. ki is the number of children of node i, and c1, ... cki are node IDs of 1st, ... kth child of node i.

In the next line, the number of queryies q is given. In the next q lines, pairs of u and v are given as the queries.

Output

For each query, print the LCA of u and v in a line.

Constraints

1 ≤ n ≤ 100000

1 ≤ q ≤ 100000

Sample Input 1

8
3 1 2 3
2 4 5
0
0
0
2 6 7
0
0
4
4 6
4 7
4 3
5 2

Sample Output 1

1
1
0
0

Solution in C++14 (judge verdict: Time Limit Exceeded)

# 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>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
constexpr long long MOD = 1000000000 + 7;
constexpr long long INF = std::numeric_limits<long long>::max();
const double PI = acos(-1);
#define fir first
#define sec second
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; };
typedef pair<LL, Tll> Ptll;
#define rep(i,rept) for(LL i=0;i<rept;i++)
#define Mfor(i,mf) for(LL i=mf-1;i>=0;i--)
LL h, w, n, m, k, s, t, q, ans, sum, last, cnt;
struct Edge { LL to,cost; };
vector<Edge>vec[100010];
string str;
bool f;
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 Seg {
	LL n;
	vector<LL>node, num;
	Seg(LL n_) {
		n = 1; while (n < n_)n *= 2;
		node.resize(n * 2 - 1);
		num.resize(n * 2 - 1);
		rep(i, n)node[n + i - 1] = INF;
		Mfor(i, n - 2)node[i] = min(node[i * 2 + 1], node[i * 2 + 2]);
	}
	void update(LL i, LL x,LL u) {
		i = i + n - 1;
		node[i] = x;
		num[i] = u;
		while (i > 0) {
			i = (i - 1) / 2;
			LL mn = i * 2 + 1;
			if (node[i * 2 + 1] > node[i * 2 + 2])mn = i * 2 + 2;
			node[i] = node[mn];
			num[i] = num[mn];
		}
	}
	Pll find(LL i, LL a, LL b, LL l, LL r) {
		if (b < l || r < a)return Pll(INF, INF);
		if (l <= a&&b <= r)return Pll(node[i], num[i]);
		Pll v1 = find(i * 2 + 1, a, (a + b) / 2, l, r),
			v2 = find(i * 2 + 2, (a + b + 1) / 2, b, l, r);
		return (v1.fir < v2.fir ? v1 : v2);
	}
};
struct vertex {
	LL l, r, deep;
	bool f = 0;
};
vertex v[100010];
Seg seg(200010);
void lca() {
	vector<LL>root;
	LL num = 0, deep = 0;
	root.push_back(0);
	while (root.size() != 0) {
		LL u = root[root.size() - 1];
		if (v[u].f == 0) {
			v[u].l = num;
			v[u].deep = deep;
			v[u].f = 1;
		}
		v[u].r = num;
		seg.update(num,deep,u);
		num++;
		rep(j, vec[u].size()) {
			if (v[vec[u][j].to].f == 0) {
				root.push_back(vec[u][j].to);
				vec[u].erase(vec[u].begin()+j);
				deep++;
				break;
			}
		}
		if (seg.node[seg.n + num - 2] == deep) {
			deep--;
			root.pop_back();
		}
	}
}
int main() {
	cin >> n;
	rep(i, n) {
		cin >> k;
		rep(j, k) {
			cin >> h;
			vec[i].push_back(Edge{ h,1 });
		}
	}
	lca();
	cin >> q;
	rep(i, q) {
		LL x, y;
		cin >> x >> y;
		if (v[x].l > v[y].l)swap(x, y);
		cout << seg.find(0, 0, seg.n - 1, v[x].l, v[y].r).sec << endl;
	}
	return 0;
}