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 (judge verdict: Memory Limit Exceeded)

#include <stdio.h>
#include <stdlib.h>

#define MAX_NODE (100000)

int* node[MAX_NODE];
int* route[MAX_NODE];
int* r_size[MAX_NODE];
int n_size[MAX_NODE] = {0};
int parent[MAX_NODE] = {0};
int so_list[MAX_NODE];
int si_list[MAX_NODE];
int so;
int si;

int solve(int source, int sink, int root) {
	int node, so_visit, si_visit;
	int i, j;
	if(source == sink) return source;
	so = si = 0;
	so_visit = si_visit = 0;
	
	// sourceからrootへ行く際に通るnodeを登録
	node = source;
	while(node != root) {
		so_list[so++] = node;
		node = parent[node];
	}
	route[source] = (int*)malloc(so*sizeof(int));
	memcpy(route[source], so_list, sizeof(so*sizeof(int)));
	r_size[node] = so;
	
	// sinkからrootへ行く際に通るnodeを登録
	node = sink;
	while(node != root) {
		si_list[si++] = node;
		node = parent[node];
	}
	route[node] = (int*)malloc(si*sizeof(int));
	memcpy(route[node], si_list, sizeof(si*sizeof(int)));
	r_size[node] = si;

	// ルートから順番に追ってみて、同じ親じゃなくなったら
	// ひとつ前のノードを結果とする
	node = root;
	for(i=si-1, j=so-1; i>= 0 && j>=0; i--, j--) {
		if(so_list[j] != si_list[i]) {
			return node;
		}

		node = so_list[j];
	}
	
	return node;
	
}

int main(void) {
	int i, j, x, m, n, t, q;
	int source, sink;
	int is_root[MAX_NODE] = {0};
	int root;
	
	scanf("%d\n", &n);
	for(i=0;i<n;i++) {
		scanf("%d", &m);

		for(j=0;j<m;j++) {
			scanf("%d", &t);
			is_root[t] = -1;
			parent[t] = i;
		}
	}
	
	// ルート
	root = 0;
	for(i=0;i<n;i++) {
		route[i] = NULL;
		r_size[i] = 0;
		if(is_root[i] == 0) {
			root = i;
			break;
		}
	}
	
	scanf("%d\n", &q);
	for(i=0;i<q;i++) {
		scanf("%d %d\n" ,&source, &sink);
		printf("%d\n", solve(source, sink, root));
	}

	return 0;
}