#include <bits/stdc++.h>
using namespace std;

#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define REP(i, n) for(int i = 0; i < (n); ++i)
#define REPr(i, n) for(int i = (n)-1; i >= 0; --i)
#define FORq(i, m, n) for(int i = (m); i <= (n); ++i)
#define FORqr(i, m, n) for(int i = (n); i >= (m); --i)
#define MP make_pair
#define SIN(x, S) (S.count(x) != 0)
#define M0(x) memset(x, 0, sizeof(x))
#define FILL(x, y) memset(x, y, sizeof(x))
#define MM(x) memset(x, -1, sizeof(x))
#define ALL(x) (x).begin(), (x).end()
#define DB(x) cerr << #x << " = " << x << endl
#define DB2(x, y)                                                              \
    cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")\n";
#define DEBUG                                                                  \
    int x12345;                                                                \
    cin >> x12345;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef long long ll;
typedef long long integer;
///////////////////////////////////////////////
const long long MOD = 1e9 + 7;
template <class T> inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

template <class T> inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

///////////////////////////////////////////////
/// 🍈( '-' 🍈 |AC|

template <typename C>
struct Vertex{
    C cost;
    bool done;
    int from;
    vector<int> edges_to; //to vertex number
	vector<C> edges_cost; // edge cost

    bool operator<(const Vertex& another) const { // priority --> min
        return another.cost < cost; // order(cost)
    }
	
	bool operator==(const Vertex& another) const {
        return another.cost == cost; // order(cost)
    }
};

template <typename C>
struct Graph{
    vector<Vertex<C>> vertex;
    Graph(int n){
        vertex.resize(n);
    }

    void resize(int n){
        vertex.resize(n);
    }

    void addedgedir(int x,int y){
        vertex[x].edges_to.push_back(y);
    }
    void addedge(int x, int y){
        vertex[x].edges_to.push_back(y);
        vertex[y].edges_to.push_back(x);
    }

    void caddedgedir(int x, int y, C c){
        vertex[x].edges_to.push_back(y);  
        vertex[x].edges_cost.push_back(c);  
    }

    void caddedge(int x, int y, C c){
        vertex[x].edges_to.push_back(y);  
        vertex[x].edges_cost.push_back(c);  
        vertex[y].edges_to.push_back(x);  
        vertex[y].edges_cost.push_back(c); 
    }

    void dijkstra(int start){
        int N = vertex.size();
        priority_queue<C> Q;
        
        REP(i,N){
            vertex[i].done = false;
            vertex[i].cost = -1;
        }
        
        vertex[start].cost = 0; //start
        Q.push(start);
        
        while (!Q.empty()){
            int now = Q.top(); Q.pop();
            vertex[now].done = true;
            REP(i,vertex[now].edges_to.size()){
                int nto = vertex[now].edges_to[i];
                C ncost = vertex[now].cost + vertex[now].edges_cost[i];
                if (((vertex[nto].cost) < 0) or (ncost < vertex[nto].cost)){  //not visited or required refresh
                    vertex[nto].cost = ncost;
                    vertex[nto].from = now;
                    Q.push(nto);
                }	
            }
        }
    }

};

template<typename C>
struct DoublingLowestCommonAncestor{
    const Graph<int> &g;
    int N;
    vector<int> depth;
    const int LOG; // default = 32
    vector<vector<int>> bpar; //bpar[k][v] vが2^k回上にいったときのvertex
    DoublingLowestCommonAncestor(const Graph<int> &g) : g(g),depth(g.vertex.size()) ,LOG(32){
        bpar.assign(LOG,vector< int > (g.vertex.size(), -1) );
    }

    void dfs(int v,int p, int d){
        // v .. now vertex, p .. parent , d .. depth
        bpar[0][v] = p;
        depth[v] = d;
        int Enum = g.vertex[v].edges_to.size();
        for(int i = 0; i < Enum; i++){
            for (int j = 0; j < g.vertex[v].edges_to.size(); j++){
                if (g.vertex[v].edges_to[j] != p) dfs(g.vertex[v].edges_to[j],v,d+1);
            }
        }
    }

    void bfs(int root){
        queue<int> Q;
        vector<bool> vis;
        vis.assign(g.vertex.size(),false);
        Q.push(root);
        while(!Q.empty()){
            int now = Q.front();
            Q.pop();
            vis[now] = true;
            REP(i,g.vertex[now].edges_to.size()){
                int next = g.vertex[now].edges_to[i];
                if (vis[next]) continue;
                bpar[0][next] = now;
                depth[next] = depth[now] + 1;
                Q.push(next);
            }
        }
    }

    void build(int root){
        int GS = g.vertex.size();
        //dfs(root,-1,0);
        bfs(root);
        for(int k = 0; k + 1 < LOG; k++){
            for(int i = 0; i < GS; i++){
                if (bpar[k][i] == -1) bpar[k+1][i] = -1;
                else bpar[k+1][i] = bpar[k][bpar[k][i]];
            }
        }
    }

    int lca(int u, int v){
        if (depth[u] > depth[v]) swap(u,v);
        for(int i = LOG - 1; i >= 0; i--){
            if (((depth[v] - depth[u]) >> i) & 1) v = bpar[i][v]; 
        }
        if (u == v) return u;
        for(int i = LOG - 1; i >= 0; i--){
            if (bpar[i][u] != bpar[i][v]){
                u = bpar[i][u];
                v = bpar[i][v];
            }
        }
        return bpar[0][u];
    }
};

int main() {
    clock_t t1 = clock();
    int n;
    scanf("%d\n",&n);
    Graph<int> G(n);
    REP(i,n){
        int k; scanf("%d",&k);
        REP(j,k){
            int c; scanf("%d",&c);
            
            G.addedgedir(i,c);
        }
    }
    clock_t t2 = clock();
    DoublingLowestCommonAncestor<Graph<int>> LCA(G);
    
    LCA.build(0);
    clock_t t3 = clock();

    int q; scanf("%d",&q);
    REP(i,q){
        int u,v; scanf("%d%d",&u,&v);
        printf("%d\n",LCA.lca(u,v));
    }
    clock_t t4 = clock();
    const double time1 = static_cast<double>(t2 - t1) / CLOCKS_PER_SEC * 1000.0;
    const double time2 = static_cast<double>(t3 - t2) / CLOCKS_PER_SEC * 1000.0;
    const double time3 = static_cast<double>(t4 - t3) / CLOCKS_PER_SEC * 1000.0;

    //cout << time1 << "ms \n";
    //cout << time2 << "ms \n";
    //cout << time3 << "ms \n";


}


Measured memory_bytes: 25616