#define _CRT_SECURE_NO_WARNINGS
//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
#define all(c) (c).begin(), (c).end()
#define loop(i,a,b) for(ll i=a; i<ll(b); i++)
#define rep(i,b) loop(i,0,b)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
template<class T> ostream & operator<<(ostream & os, vector<T> const &);
template<int n, class...T> typename enable_if<(n>=sizeof...(T))>::type _ot(ostream &, tuple<T...> const &){}
template<int n, class...T> typename enable_if<(n< sizeof...(T))>::type _ot(ostream & os, tuple<T...> const & t){ os << (n==0?"":" ") << get<n>(t); _ot<n+1>(os, t); }
template<class...T> ostream & operator<<(ostream & os, tuple<T...> const & t){ _ot<0>(os, t); return os; }
template<class T, class U> ostream & operator<<(ostream & os, pair<T,U> const & p){ return os << "(" << p.first << ", " << p.second << ") "; }
template<class T> ostream & operator<<(ostream & os, vector<T> const & v){ rep(i,v.size()) os << v[i] << (i+1==(int)v.size()?"":" "); return os; }
#ifdef DEBUG
#define dump(...) (cerr<<#__VA_ARGS__<<" = "<<mt(__VA_ARGS__)<<" ["<<__LINE__<<"]"<<endl)
#else
#define dump(...)
#endif

typedef int Weight;
struct Edge {
    int src, dst;
    Weight weight;
    Edge(int src_, int dst_, Weight weight_) :
        src(src_), dst(dst_), weight(weight_) { }
};
bool operator < (const Edge &e, const Edge &f) {
    return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
        e.src != f.src ? e.src < f.src : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Weight> Array;
typedef vector<Array> Matrix;

int const MAX_V = 1000010;

typedef int dat_t;
dat_t const inf = (1LL<<31)-1;
dat_t const init = (1LL<<31)-1;
struct segment_tree {
    vector<pair<dat_t,int>> dat;
    int n;
    segment_tree(int n_){
        n = 1;
        while(n < n_) n<<=1;
        dat.resize(n*2);
        for(int i=n; i<n+n; i++){
            dat[i] = make_pair(init,i); // -infをdat[i]にするとvectorからのコンストラクタになる
        }
        for(int i=n-1; i>=1; i--){
            dat[i] = min(dat[i<<1], dat[i<<1|1]);
        }
    }
    // bottom-up
    void set(int k, dat_t x){
        int i = n+k; // leaf
        dat[i] = make_pair(x,k);
        while(i != 1){ // 1 is root
            dat[i>>1] = min(dat[i], dat[i^1]);
            i>>=1;
        }
    }
    // top-down
    pair<dat_t,int> get(int a, int b, int k, int l, int r){
        if(r <= a || b <= l) return make_pair(inf,0);
        if(a <= l && r <= b) return dat[k];
        pair<dat_t,int> v1 = get(a,b,k<<1,l,(l+r)/2);
        pair<dat_t,int> v2 = get(a,b,k<<1|1,(l+r)/2,r);
        return min(v1,v2);
    }
    pair<dat_t,int> get(int a, int b){
        return get(a,b,1,0,n);
    }
};

struct LowestCommonAncestor {
    int n;
    Graph g;
    unique_ptr<int[]> ord, depth, id;
    unique_ptr<segment_tree> rmq;
    LowestCommonAncestor(int n_)
        : n(n_), g(n), ord(new int[n*2]), depth(new int[n*2]), id(new int[n]), rmq(new segment_tree(n*2)) {
    }
    void add_edge(int a, int b){
        g[a].eb(a,b,0);
    }
    void compile(int root = 0){
        int k = 0;
        dfs(root,-1,0,k);
        for(int i=0;i<k;i++){
            rmq->set(i,depth[i]);
        }
    }
    int ask(int u, int v){
        int a = id[u], b = id[v];
        return ord[rmq->get(min(a,b), max(a,b)+1).second];
    }
    void dfs(int v, int p, int d, int & k){
        id[v] = k;
        ord[k] = v;
        depth[k] = d;
        k++;
        for(auto & e : g[v]){
            if(e.dst != p){
                dfs(e.dst, v, d+1, k);
                ord[k] = v;
                depth[k] = d;
                k++;
            }
        }
    }
};

int main(){
    int n;
    cin >> n;
    LowestCommonAncestor g(n);
    rep(i,n){
        int k; cin >> k;
        for(int j=0;j<k;j++){
            int to; cin >> to;
            g.add_edge(i,to);
            g.add_edge(to,i);
        }
    }
    g.compile();
    int q;
    cin >> q;
    for(int i=0;i<q;i++){
        int u,v;
        cin >> u >> v;
        cout << g.ask(u,v) << endl;
    }
}

Measured memory_bytes: 15448