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)

//{{{
#[allow(unused_macros)]
macro_rules! getl {
    ( $( $t:ty ),* ) => {
        {
            let mut s = String::new();
            std::io::stdin().read_line(&mut s).unwrap();
            let s = s.trim_right();
            let mut ws = s.split_whitespace();
            ($(ws.next().unwrap().parse::<$t>().unwrap()),*)
        }
    };
}

#[allow(unused_macros)]
macro_rules! getl_vec {
    ( $t:ty ) => {{
        let mut s = String::new();
        std::io::stdin().read_line(&mut s).unwrap();
        let s = s.trim_right();
        s.split_whitespace()
            .map(|x| x.parse().unwrap())
            .collect::<Vec<$t>>()
    }};
}
//}}}

fn main() {
    let n = getl!(usize);
    let mut graph: Vec<Vec<usize>> = vec![vec![]; n];
    for i in 0..n {
        let v = getl_vec!(usize);
        graph[i] = v[1..].to_owned();
    }
    let q = getl!(usize);
    let mut queries = vec![];
    for _ in 0..q {
        let (u, v) = getl!(usize, usize);
        queries.push((u, v));
    }

    let lca = Lca::new(0, &graph);

    for (u, v) in queries {
        let ans = lca.lca(u, v);
        println!("{}", ans);
    }
}

struct Lca {
    id: Vec<usize>,
    segtree: SegmentTree<Option<usize>>,
}

impl<'a> Lca {
    fn new(root: usize, graph: &Vec<Vec<usize>>) -> Lca {
        let n = graph.len();
        let mut k = 0;
        let mut id = vec![0; n];
        let mut vs = vec![0; n * 2 - 1];
        let mut depth = vec![0; n];
        Lca::dfs(root, None, 0, &mut k, &mut id, &mut vs, &mut depth, &graph);

        let op = move |i: Option<usize>, j: Option<usize>| -> Option<usize> {
            match (i, j) {
                (Some(i), Some(j)) => {
                    if depth[i] <= depth[j] {
                        Some(i)
                    } else {
                        Some(j)
                    }
                }
                (Some(i), None) => Some(i),
                (None, Some(j)) => Some(j),
                (None, None) => None,
            }
        };
        let segtree = SegmentTree::new(&vs.iter().map(|x| Some(*x)).collect(), Box::new(op), None);

        let lca = Lca { id: id, segtree: segtree };

        lca
    }

    fn lca(&self, u: usize, v: usize) -> usize {
        use std::cmp::{max, min};
        self.segtree
            .query(min(self.id[u], self.id[v]), max(self.id[u], self.id[v]) + 1)
            .unwrap()
    }

    fn dfs(
        v: usize,
        parent: Option<usize>,
        d: usize,
        k: &mut usize,
        id: &mut Vec<usize>,
        vs: &mut Vec<usize>,
        depth: &mut Vec<usize>,
        graph: &Vec<Vec<usize>>,
    ) {
        id[v] = *k;
        vs[*k] = v;
        depth[v] = d;
        *k += 1;

        for &next in graph[v].iter().filter(|x| match parent {
            Some(p) => **x != p,
            None => true,
        }) {
            Lca::dfs(next, Some(v), d + 1, k, id, vs, depth, graph);
            vs[*k] = v;
            *k += 1;
        }
    }
}

struct SegmentTree<T> {
    len: usize,
    data: Vec<T>,
    operator: Box<Fn(T, T) -> T>,
    unit: T,
}

impl<T> SegmentTree<T>
where
    T: Clone + Copy,
{
    fn new(v: &Vec<T>, operator: Box<Fn(T, T) -> T>, unit: T) -> SegmentTree<T> {
        let n = v.len();
        let mut len = 1;
        while len < n {
            len *= 2;
        }

        let mut segtree = SegmentTree {
            len: len,
            data: vec![unit; 2 * len - 1],
            operator: operator,
            unit: unit,
        };

        for i in 0..n {
            segtree.data[i + segtree.len - 1] = v[i];
        }
        for i in (0..(segtree.len - 1)).rev() {
            segtree.data[i] = (segtree.operator)(segtree.data[2 * i + 1], segtree.data[2 * i + 2]);
        }

        segtree
    }

    #[allow(unused)]
    fn update(&mut self, k: usize, a: T) {
        let data = &mut self.data;
        let mut k = k + self.len - 1;
        data[k] = a;
        while k > 0 {
            k = (k - 1) / 2;
            data[k] = (self.operator)(data[k * 2 + 1], data[k * 2 + 2]);
        }
    }

    #[allow(unused)]
    fn get(&self, k: usize) -> T {
        self.data[k + self.len - 1]
    }

    fn query(&self, a: usize, b: usize) -> T {
        self.process_query(a, b, 0, 0, self.len)
    }

    fn process_query(&self, a: usize, b: usize, k: usize, l: usize, r: usize) -> T {
        if r <= a || b <= l {
            return self.unit;
        }

        if a <= l && r <= b {
            self.data[k]
        } else {
            let vl = self.process_query(a, b, k * 2 + 1, l, (l + r) / 2);
            let vr = self.process_query(a, b, k * 2 + 2, (l + r) / 2, r);
            (self.operator)(vl, vr)
        }
    }
}