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

use std::collections::VecDeque;

fn main() {
    let mut sc = Scanner::new();
    let n = sc.read::<usize>();
    let mut graph = (0..n).map(|_| Vec::new()).collect::<Vec<_>>();
    for i in 0..n {
        let k = sc.read::<usize>();
        for _ in 0..k {
            let c: usize = sc.read();
            graph[i].push(c);
            graph[c].push(i);
        }
    }

    let lca = LowestCommonAncestor::new(&graph);

    let q: usize = sc.read();
    for _ in 0..q {
        let u: usize = sc.read();
        let v: usize = sc.read();
        println!("{}", lca.get_lca(u, v));
    }
}

struct LowestCommonAncestor {
    graph: Vec<Vec<usize>>,
    parent: Vec<Vec<i32>>,
    depth: Vec<usize>,
    root: usize,
    log_v: usize,
}

impl LowestCommonAncestor {
    fn new(graph: &Vec<Vec<usize>>) -> LowestCommonAncestor {
        let v = graph.len();
        let root = 0;
        let graph = graph.clone();
        let mut depth = vec![0; v];

        let mut log_v = 1;
        let mut i = 1;
        while i <= v {
            i *= 2;
            log_v += 1;
        }
        let mut parent: Vec<Vec<i32>> = vec![vec![0; v]; log_v];

        let mut depth_vis = vec![false; v];
        let mut stack = VecDeque::new();
        stack.push_front(root);
        parent[0][root] = -1;
        depth[root] = 0;
        depth_vis[root] = true;
        while !stack.is_empty() {
            let v = stack.pop_front().unwrap();
            stack.push_front(v);
            for u in &graph[v] {
                let u = *u;
                if depth_vis[u] { continue; }
                parent[0][u] = v as i32;
                depth[u] = depth[v] + 1;
                depth_vis[u] = true;
                stack.push_front(u);
            }

            let head = stack.pop_front().unwrap();
            if head != v { stack.push_front(head); }
        }

        for k in 0..(log_v - 1) {
            for u in 0..v {
                parent[k + 1][u] = if parent[k][u] < 0 {
                    -1
                } else {
                    parent[k][parent[k][u] as usize]
                };
            }
        }

        LowestCommonAncestor { graph: graph, parent: parent, depth: depth, root: root, log_v: log_v }
    }

    fn get_lca(&self, u: usize, v: usize) -> usize {
        let (mut u, mut v) = if self.depth[u] <= self.depth[v] { (u, v) } else { (v, u) };
        for k in 0..self.log_v {
            if ((self.depth[v] - self.depth[u]) & (1 << k)) != 0 {
                v = self.parent[k][v] as usize;
            }
        }
        if u == v { return u; }

        for k in (0..self.log_v).rev() {
            if self.parent[k][u] != self.parent[k][v] {
                u = self.parent[k][u] as usize;
                v = self.parent[k][v] as usize;
            }
        }
        return self.parent[0][u] as usize;
    }

    fn get_dist(&self, u: usize, v: usize) -> usize {
        let lca = self.get_lca(u, v);
        self.depth[u] + self.depth[v] - self.depth[lca] * 2
    }
}

struct Scanner {
    ptr: usize,
    length: usize,
    buf: Vec<u8>,
    small_cache: Vec<u8>,
}

impl Scanner {
    fn new() -> Scanner {
        Scanner { ptr: 0, length: 0, buf: vec![0; 1024], small_cache: vec![0; 1024] }
    }

    fn load(&mut self) {
        use std::io::Read;
        let mut s = std::io::stdin();
        self.length = s.read(&mut self.buf).unwrap();
    }

    fn byte(&mut self) -> u8 {
        if self.ptr >= self.length {
            self.ptr = 0;
            self.load();
            if self.length == 0 {
                self.buf[0] = b'\n';
                self.length = 1;
            }
        }

        self.ptr += 1;
        return self.buf[self.ptr - 1];
    }

    fn is_space(b: u8) -> bool { b == b'\n' || b == b'\r' || b == b'\t' || b == b' ' }

    fn read<T>(&mut self) -> T where T: std::str::FromStr, T::Err: std::fmt::Debug, {
        let mut b = self.byte();
        while Scanner::is_space(b) {
            b = self.byte();
        }

        for pos in 0..self.small_cache.len() {
            self.small_cache[pos] = b;
            b = self.byte();
            if Scanner::is_space(b) {
                return String::from_utf8_lossy(&self.small_cache[0..(pos + 1)]).parse().unwrap();
            }
        }

        let mut v = self.small_cache.clone();
        while !Scanner::is_space(b) {
            v.push(b);
            b = self.byte();
        }
        return String::from_utf8_lossy(&v).parse().unwrap();
    }
}