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 JAVA (judge verdict: Accepted)


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.List;
import java.util.StringTokenizer;
import java.util.stream.Stream;

public class Main {

	public static void main(String[] args) throws IOException {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskX solver = new TaskX();
		solver.solve(1, in, out);
		out.close();
	}

	static int INF = 1 << 30;
	static int MOD = 1000000007;
	static int[] mh4 = { 0, -1, 1, 0 };
	static int[] mw4 = { -1, 0, 0, 1 };
	static int[] mh8 = { -1, -1, -1, 0, 0, 1, 1, 1 };
	static int[] mw8 = { -1, 0, 1, -1, 1, -1, 0, 1 };

	@SuppressWarnings("unchecked")
	static class TaskX {
		public void solve(int testNumber, InputReader in, PrintWriter out) {

			int n = in.nextInt();
			List<Integer>[] g = new ArrayList[n];
			g = Stream.generate(ArrayList::new).limit(n).toArray(List[]::new);
			for (int i = 0; i < n; i++) {
				int m = in.nextInt();
				for (int j = 0; j < m; j++) {
					g[i].add(in.nextInt());
				}
			}

			LCA lca = new LCA(g, 0);
			int q = in.nextInt();
			for (int i = 0; i < q; i++) {
				int u = in.nextInt();
				int v = in.nextInt();
				int l = lca.lca(u, v);
				out.println(l);
			}

		}
	}

	public static class LCA {

		private final int logN;
		private final int n;
		private final List<Integer>[] graph;
		private final int[][] par;
		public final int[] depth;

		public LCA(List<Integer>[] g, int root) {
			this.graph = g;
			this.n = g.length;
			this.logN = Integer.numberOfTrailingZeros(Integer.highestOneBit(n - 1)) + 1;
			this.par = new int[logN][n];
			this.depth = new int[n];

			init(root);
		}

		private void init(int root) {
			bfs(root);
			for (int k = 0; k < logN - 1; k++) {
				for (int v = 0; v < n; v++) {
					if (par[k][v] < 0)
						par[k + 1][v] = -1;
					else
						par[k + 1][v] = par[k][par[k][v]];
				}
			}
		}

		private void bfs(int v) {
			Arrays.fill(depth, Integer.MAX_VALUE);
			ArrayDeque<Integer> queue = new ArrayDeque<Integer>();
			queue.add(v);
			depth[v] = 0;
			par[0][v] = -1;
			while (!queue.isEmpty()) {
				int now = queue.poll();
				for (int p : graph[now]) {
					if (depth[p] > depth[now] + 1) {
						depth[p] = depth[now] + 1;
						queue.add(p);
						par[0][p] = now;
					}
				}
			}
		}

		public int lca(int u, int v) {
			if (depth[u] > depth[v]) {
				int tmp = u;
				u = v;
				v = tmp;
			}
			for (int k = 0; k < logN; k++) {
				if (((depth[v] - depth[u]) >> k & 1) == 1)
					v = par[k][v];
			}
			if (u == v)
				return u;

			for (int k = logN - 1; k >= 0; k--) {
				if (par[k][u] != par[k][v]) {
					u = par[k][u];
					v = par[k][v];
				}
			}
			return par[0][u];
		}
	}

	static class InputReader {
		BufferedReader in;
		StringTokenizer tok;

		public String nextString() {
			while (!tok.hasMoreTokens()) {
				try {
					tok = new StringTokenizer(in.readLine(), " ");
				} catch (IOException e) {
					throw new InputMismatchException();
				}
			}
			return tok.nextToken();
		}

		public int nextInt() {
			return Integer.parseInt(nextString());
		}

		public long nextLong() {
			return Long.parseLong(nextString());
		}

		public double nextDouble() {
			return Double.parseDouble(nextString());
		}

		public int[] nextIntArray(int n) {
			int[] res = new int[n];
			for (int i = 0; i < n; i++) {
				res[i] = nextInt();
			}
			return res;
		}

		public long[] nextLongArray(int n) {
			long[] res = new long[n];
			for (int i = 0; i < n; i++) {
				res[i] = nextLong();
			}
			return res;
		}

		public InputReader(InputStream inputStream) {
			in = new BufferedReader(new InputStreamReader(inputStream));
			tok = new StringTokenizer("");
		}
	}

}