#include <cassert>// c
#include <iostream>// io
#include <iomanip>
#include <fstream>
#include <sstream>
#include <vector>// container
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <stack>
#include <algorithm>// other
#include <complex>
#include <numeric>
#include <functional>
#include <random>
#include <regex>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define ALL(c) c.begin(),c.end()
#define REP(i,n) FOR(i,0,n)
#define REPr(i,n) FORr(i,0,n)
#define FOR(i,l,r) for(int i=(int)(l);i<(int)(r);++i)
#define FORr(i,l,r) for(int i=(int)(r)-1;i>=(int)(l);--i)
#define EACH(it,o) for(auto it = (o).begin(); it != (o).end(); ++it)
#define IN(l,v,r) ((l)<=(v) && (v)<(r))
#define UNIQUE(v) v.erase(unique(ALL(v)),v.end())
//debug
#define DUMP(x)  cerr << #x << " = " << (x)
#define LINE()    cerr<< " (L" << __LINE__ << ")"

class range {
private:
	struct Iter{
		int v;
		int operator*(){return v;}
		bool operator!=(Iter& itr) {return v < itr.v;}
		void operator++() {++v;}
	};
	Iter i, n;
public:
	range(int n) : i({0}), n({n}) {}
	range(int i, int n) : i({i}), n({n}) {}
	Iter& begin() {return i;}
	Iter& end() {return n;}
};

//input
template<typename T1,typename T2> istream& operator >> (istream& is,pair<T1,T2>& p){return is>>p.first>>p.second;}
template<typename T1> istream& operator >> (istream& is,tuple<T1>& t){return is >> get<0>(t);}
template<typename T1,typename T2> istream& operator >> (istream& is,tuple<T1,T2>& t){return is >> get<0>(t) >> get<1>(t);}
template<typename T1,typename T2,typename T3> istream& operator >> (istream& is,tuple<T1,T2,T3>& t){return is >>get<0>(t)>>get<1>(t)>>get<2>(t);}
template<typename T1,typename T2,typename T3,typename T4> istream& operator >> (istream& is,tuple<T1,T2,T3,T4>& t){return is >> get<0>(t)>>get<1>(t)>>get<2>(t)>>get<3>(t);}
template<typename T1,typename T2,typename T3,typename T4,typename T5> istream& operator >> (istream& is, const tuple<T1,T2,T3,T4,T5>& t){return is >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t) >> get<4>(t);}
template<typename T1,typename T2,typename T3,typename T4,typename T5,typename T6> istream& operator >> (istream& is, const tuple<T1,T2,T3,T4,T5,T6>& t){return is >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t) >> get<4>(t) >> get<5>(t);}
template<typename T1,typename T2,typename T3,typename T4,typename T5,typename T6,typename T7> istream& operator >> (istream& is, const tuple<T1,T2,T3,T4,T5,T6,T7>& t){return is >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t) >> get<4>(t) >> get<5>(t) >> get<6>(t);}
template<typename T> istream& operator >> (istream& is,vector<T>& as){for(int i:range(as.size()))is >>as[i];return is;}

//output
template<typename T> ostream& operator << (ostream& os, const set<T>& ss){for(auto a:ss){if(a!=ss.begin())os<<" "; os<<a;}return os;}
template<typename T1,typename T2> ostream& operator << (ostream& os, const pair<T1,T2>& p){return os<<p.first<<" "<<p.second;}
template<typename K,typename V> ostream& operator << (ostream& os, const map<K,V>& m){bool isF=true;for(auto& p:m){if(!isF)os<<endl;os<<p;isF=false;}return os;}
template<typename T1> ostream& operator << (ostream& os, const tuple<T1>& t){return os << get<0>(t);}
template<typename T1,typename T2> ostream& operator << (ostream& os, const tuple<T1,T2>& t){return os << get<0>(t)<<" "<<get<1>(t);}
template<typename T1,typename T2,typename T3> ostream& operator << (ostream& os, const tuple<T1,T2,T3>& t){return os << get<0>(t)<<" "<<get<1>(t)<<" "<<get<2>(t);}
template<typename T1,typename T2,typename T3,typename T4> ostream& operator << (ostream& os, const tuple<T1,T2,T3,T4>& t){return os << get<0>(t)<<" "<<get<1>(t)<<" "<<get<2>(t)<<" "<<get<3>(t);}
template<typename T1,typename T2,typename T3,typename T4,typename T5> ostream& operator << (ostream& os, const tuple<T1,T2,T3,T4,T5>& t){return os << get<0>(t)<<" "<<get<1>(t)<<" "<<get<2>(t)<<" "<<get<3>(t)<<" "<<get<4>(t);}
template<typename T1,typename T2,typename T3,typename T4,typename T5,typename T6> ostream& operator << (ostream& os, const tuple<T1,T2,T3,T4,T5,T6>& t){return os << get<0>(t)<<" "<<get<1>(t)<<" "<<get<2>(t)<<" "<<get<3>(t)<<" "<<get<4>(t)<<" "<<get<5>(t);}
template<typename T1,typename T2,typename T3,typename T4,typename T5,typename T6,typename T7> ostream& operator << (ostream& os, const tuple<T1,T2,T3,T4,T5,T6,T7>& t){return os << get<0>(t)<<" "<<get<1>(t)<<" "<<get<2>(t)<<" "<<get<3>(t)<<" "<<get<4>(t)<<" "<<get<5>(t)<<" "<<get<6>(t);}
template<typename T> ostream& operator << (ostream& os, const vector<T>& as){for(int i:range(as.size())){if(i!=0)os<<" "; os<<as[i];}return os;}
template<typename T> ostream& operator << (ostream& os, const vector<vector<T>>& as){for(int i:range(as.size())){if(i!=0)os<<endl; os<<as[i];}return os;}

// values
template<typename T> inline T INF(){assert(false);};
template<> inline int INF<int>(){return 1<<28;};
template<> inline ll INF<ll>(){return 1LL<<58;};
template<> inline double INF<double>(){return 1e16;};
template<> inline long double INF<long double>(){return 1e16;};

template<class T> inline T EPS(){assert(false);};
template<> inline int EPS<int>(){return 1;};
template<> inline ll EPS<ll>(){return 1LL;};
template<> inline double EPS<double>(){return 1e-8;};
template<> inline long double EPS<long double>(){return 1e-8;};

// min{2^r | n < 2^r}
template<typename T> T upper_pow2(T n){
	T res=1;while(res<n)res<<=1;return res;
}
// max{d | 2^d  <= n}
template<typename T> T msb(T n){
	int d=30;while((1<<d)>n)d--;return d;
}

template<typename T,typename U> T pmod(T v,U M){return (v%M+M)%M;}


template<typename T> class Monoid{
public:
	virtual T e()=0,c(T,T)=0;
};

class MiniMonoid:public Monoid<int>{
public:
	vector<int>& vs;
	MiniMonoid(vector<int>& vs):vs(vs){};
	int e(){return -1;}
	int c(int a,int b){
		if(b==-1)return a;if(a==-1)return b;
		return min(vs[a],vs[b])==vs[a]?a:b;// left most
	}
};

template<typename T> class SegmentTree{
public:
	const int size,n2;vector<T> dat;Monoid<T>& m;
	SegmentTree(int n,Monoid<T>& m):size(n),n2(upper_pow2(size)),m(m){
		dat=vector<T>(n2*2,m.e());
	}
	inline T get(int v){return dat[v+n2];}
	void set(int v,T a){
		v+=n2;dat[v]=a;
		while(v!=1){
			int parent=v/=2,chl=parent*2,chr=parent*2+1;
			dat[parent]=m.c(dat[chl],dat[chr]);
		}
	}
	T query(int a,int b){return query(a,b,1,0,n2);} //[a,b)
private:
	T query(int a,int b,int v,int l,int r){ //[a,b) on v:[l,r)
		if(r<=a || b<=l)return m.e();//out of range
		if(a<=l && r<=b)return dat[v];
		T lv=query(a,b,v*2,l,(l+r)/2),rv=query(a,b,v*2+1,(l+r)/2,r);
		return m.c(lv,rv);
	}
};

template<typename T> class SegmentTreei{
public:
	vector<T> vs;MiniMonoid mm;SegmentTree<T> seg;
	SegmentTreei(int n):vs(n),mm(vs),seg(n,mm){}
	void set(int v,T a){ vs[v]=a;seg.set(v,v);}
};

typedef vector<vector<int>> Graph;
class EularTourLCA{
	public:
	Graph& g;int r;
	vector<int> id,depth,vs;
	SegmentTreei<int> rmq;
	EularTourLCA(Graph & g,int r):g(g),r(r),rmq(SegmentTreei<int>(2*g.size()-1)){
		id=vector<int>(g.size());
		depth=vector<int>(2*g.size()-1);
		vs=vector<int>(2*g.size()-1);
		int k=0;dfs(-1,r,0,k);
		REP(i,vs.size())rmq.set(i,depth[i]);
	};
	void dfs(int p,int s,int d,int& k){
		id[s]=k;vs[k]=s;depth[k++]=d;
		for(int t:g[s])if(t!=p){
			dfs(s,t,d+1,k);
			vs[k]=s;
			depth[k++]=d;
		}
	}
	int lca(int u,int v){ return vs[rmq.seg.query(min(id[u],id[v]),max(id[u],id[v])+1)];}
};

class Main{
	public:
	void run(){
		int n;cin >> n;
		Graph g(n);
		REP(i,n){
			int k;cin >> k;
			REP(j,k){
				int t;cin >> t;
				g[i].push_back(t); g[t].push_back(i);
			}
		}

		EularTourLCA lca(g,0);
		int q;cin >>q;
		REP(i,q){
			int u,v;cin >> u >> v;
			cout << lca.lca(u,v) <<endl;
		}
	}
};

int main(){
	cout <<fixed<<setprecision(20);
	cin.tie(0);
	ios::sync_with_stdio(false);
	Main().run();
	return 0;
}

Measured memory_bytes: 14196