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.IOException;
import java.io.InputStream;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.NoSuchElementException;

public class Main {
	public static void main(String[] args) {
		FastScanner sc = new FastScanner();
		int n = sc.nextInt();
		Graph g = new Graph(n);
		for (int i = 0; i < n; i++) {
			int k = sc.nextInt();
			for (int j = 0; j < k; j++) {
				int c = sc.nextInt();
				g.add(i, c);
			}
		}

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

}

class LCA {
	private final int logN;
	private final int n;
	private final Graph graph;
	private final int[][] par;
	public final int[] depth;

	public LCA(Graph g, int root) {
		this.graph = g;
		this.n = g.size();
		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) {
		// dfs(root, -1, 0);
		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 (Graph.Path p : graph.get(now)) {
				if (depth[p.to] > depth[now] + 1) {
					depth[p.to] = depth[now] + 1;
					queue.add(p.to);
					par[0][p.to] = 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];
	}
}

class FastScanner {
	public static String debug = null;

	private final InputStream in = System.in;
	private int ptr = 0;
	private int buflen = 0;
	private byte[] buffer = new byte[1024];
	private boolean eos = false;

	private boolean hasNextByte() {
		if (ptr < buflen) {
			return true;
		} else {
			ptr = 0;
			try {
				if (debug != null) {
					buflen = debug.length();
					buffer = debug.getBytes();
					debug = "";
					eos = true;
				} else {
					buflen = in.read(buffer);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (buflen < 0) {
				eos = true;
				return false;
			} else if (buflen == 0) {
				return false;
			}
		}
		return true;
	}

	private int readByte() {
		if (hasNextByte())
			return buffer[ptr++];
		else
			return -1;
	}

	private static boolean isPrintableChar(int c) {
		return 33 <= c && c <= 126;
	}

	private void skipUnprintable() {
		while (hasNextByte() && !isPrintableChar(buffer[ptr]))
			ptr++;
	}

	public boolean isEOS() {
		return this.eos;
	}

	public boolean hasNext() {
		skipUnprintable();
		return hasNextByte();
	}

	public String next() {
		if (!hasNext())
			throw new NoSuchElementException();
		StringBuilder sb = new StringBuilder();
		int b = readByte();
		while (isPrintableChar(b)) {
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}

	public long nextLong() {
		if (!hasNext())
			throw new NoSuchElementException();
		long n = 0;
		boolean minus = false;
		int b = readByte();
		if (b == '-') {
			minus = true;
			b = readByte();
		}
		if (b < '0' || '9' < b) {
			throw new NumberFormatException();
		}
		while (true) {
			if ('0' <= b && b <= '9') {
				n *= 10;
				n += b - '0';
			} else if (b == -1 || !isPrintableChar(b)) {
				return minus ? -n : n;
			} else {
				throw new NumberFormatException();
			}
			b = readByte();
		}
	}

	public int nextInt() {
		return (int) nextLong();
	}

	public long[] nextLongList(int n) {
		return nextLongTable(n, 1)[0];
	}

	public int[] nextIntList(int n) {
		return nextIntTable(n, 1)[0];
	}

	public long[][] nextLongTable(int n, int m) {
		long[][] ret = new long[n][m];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				ret[i][j] = nextLong();
			}
		}
		return ret;
	}

	public int[][] nextIntTable(int n, int m) {
		int[][] ret = new int[n][m];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				ret[i][j] = nextInt();
			}
		}
		return ret;
	}
}

class Graph {
	public static class Path implements Comparable<Path> {
		public final int from;
		public final int to;
		public final long cost;

		public Path(int from, int to, long cost) {
			this.from = from;
			this.to = to;
			this.cost = cost;
		}

		@Override
		public int compareTo(Path o) {
			return Long.compare(this.cost, o.cost);
		}
	}

	private final List<List<Path>> edgeList;
	private final int size;

	public Graph(int n) {
		this.size = n;
		this.edgeList = new ArrayList<List<Path>>();
		for (int i = 0; i < n; i++) {
			this.edgeList.add(new ArrayList<Path>());
		}
	}

	public void add(int from, int to, long cost) {
		this.edgeList.get(from).add(new Path(from, to, cost));
	}

	public void addBoth(int v1, int v2, long cost) {
		this.add(v1, v2, cost);
		this.add(v2, v1, cost);
	}

	public void add(int from, int to) {
		this.edgeList.get(from).add(new Path(from, to, 1));
	}

	public void addBoth(int v1, int v2) {
		this.add(v1, v2, 1);
		this.add(v2, v1, 1);
	}

	public List<Path> get(int from) {
		return this.edgeList.get(from);
	}

	public int size() {
		return this.size;
	}
}