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

#include <bits/stdc++.h>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int,P> pip;
typedef vector<pip> vip;
const int inf=1<<30;
const ll INF=1ll<<60;
const double pi=acos(-1);
const double eps=1e-9;
const ll mod=1e9+7;
const int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};

class Splay_Tree{
	private:
	int n;
	vi L,R,par,val,date,sub;
	inline void Fix(int k){
		sub[k]=sub[L[k]]+sub[R[k]]+1;
		date[k]=((val[k]+date[L[k]])%mod+date[R[k]])%mod;
	}
	inline void Set(int k,int l,int r,int p,bool B=0){
		if(B) swap(l,r);
		L[k]=l;R[k]=r;par[k]=p;
		if(l!=n) par[l]=k;
		if(r!=n) par[r]=k;
		Fix(k);
	}
	inline int Ch(int v,bool B){return (B?R[v]:L[v]);}
	void Rotate(int v,bool B){
		int p=par[v],u=Ch(v,B);
		Set(v,Ch(u,!B),Ch(v,!B),u,B);
		Set(u,Ch(u,B),v,p,B);
		if(p!=n) (L[p]==v?L[p]:R[p])=u;
	}
	int Splay(int u){
		while(par[u]!=n){
			int v=par[u],w=par[v];
			bool A=(R[v]==u),B=(R[w]==v);
			if(w==n){
				Rotate(v,A);
				continue;
			}
			if(A==B){swap(v,w);swap(A,B);}
			Rotate(v,A);Rotate(w,B);
		}
	}
	public:
	void Init(int N){
		n=N;
		L=R=par=vi(n+1,n);
		val=date=vi(n+1);
		sub=vi(n+1,1);
		sub[n]=0;
	}
	int Find(int v,int k){
		Splay(v);
		while(1){
			int t=sub[L[v]];
			if(t==k) break;
			if(k<t) v=L[v];
			else k-=t+1,v=R[v];
		}
		Splay(v);
		return v;
	}
	void Update(int k,int x){
		k=Find(0,k);
		val[k]=x;Fix(k);
	}
	int Sub(int v){Splay(v);return sub[v];}
	void Merge(int l,int r){
		r=Find(r,0);
		Splay(l);
		Set(r,l,R[r],n);
	}
	int Split(int v,int k){
		if(!k||k==Sub(v)) return n;
		v=Find(v,k);
		par[L[v]]=n;
		Set(v,n,R[v],n);
		return v;
	}
	int Split_id(int v){
		Splay(v);
		return Split(v,sub[L[v]]+1);
	}
	void Open(){
		for(int i=0;i<=n;i++) cout<<i<<':'<<L[i]<<' '<<R[i]<<' '<<par[i]<<endl;
	}
};

class LinkCut_Tree{
	private:
	int n;
	Splay_Tree sp;
	vi par;
	public:
	LinkCut_Tree(int N){
		n=N;
		sp.Init(n);
		par=vi(n+1,n);
	}
	int Expose(int v){
		int u=sp.Split_id(v);
		if(u!=n) par[u]=v;
		while(1){
			u=sp.Find(v,0);
			if(par[u]==n) break;
			int w=sp.Split_id(par[u]);
			if(w!=u) par[w]=par[u];
			sp.Merge(par[u],u);
		}
		return u;
	}
	void Link(int v,int p){
		Expose(p);
		par[v]=n;
		sp.Merge(p,v);
	}
	void Cut(int v){
		Expose(v);
		sp.Split_id(par[v]);
		par[v]=n;
	}
	int LCA(int u,int v){
		int ru=Expose(u),rv=Expose(v);
		if(ru!=rv) return -1;
		int w=par[sp.Find(u,0)];
		if(w==n) return u;
		return  w;
	}
	void Open(){
		sp.Open();
		for(auto i:par) cout<<i<<' ';
		cout<<endl;
	}
};

int n,q;

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin>>n;
	LinkCut_Tree lct(n);
	for(int i=0;i<n;i++){
		int m;
		cin>>m;
		for(int j=0;j<m;j++){
			int v;
			cin>>v;
			lct.Link(v,i);
		}
	}
	cin>>q;
	for(int i=0;i<q;i++){
		int u,v;
		cin>>u>>v;
		cout<<lct.LCA(u,v)<<endl;
	}
}