#include <iostream>
#include <vector>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <algorithm>
#include <string>
#include <math.h>
#include <limits.h>
#include <stack>
#include <complex>
#include <stdlib.h>
#include <stdio.h>
#include <functional>
#include <cfloat>
#include <math.h>
#include <numeric>
#include <string.h>
#include <sys/time.h>
#include <random>


#define fs first
#define sc second

using namespace std;

typedef long long ll;
typedef unsigned int uint;
typedef pair<ll, ll> P;


class Graph{
    struct edge{
        int vertice;
        ll weight;
    };

    vector<vector<edge>> g;
    unsigned int n;

public:
    Graph(unsigned int n): n{n}{
        g.resize(n);
    }

    void add_edge(int u, int v){
        g[u].emplace_back(edge{v, 1});
    };

    void add_edge(int u, int v, ll w){
        g[u].emplace_back(edge{v, w});
    }

    vector<edge>& get_edge(int v){
        return g[v];
    }

    unsigned int get_num_vertices(){
        return n;
    }

    unsigned int size(){
        return n;
    }

    vector<edge>& operator[](int v){
        return get_edge(v);
    }
};


class LCA{
    Graph g{0};
    int root;
    vector<vector<int>> parent;
    vector<int> depth;
    int n, log_n;

public:
    LCA(Graph& g2){
        g = g2;
        n = g2.size();
        root = 0;
        log_n = 2;
        int tmp_n = 1;
        while(tmp_n < n){
            tmp_n *= 2;
            log_n++;
        }
        init(n);
    }

    LCA(Graph& g2, int root): root(root){
        g = g2;
        int n = g2.size();
        init(n);
    }

    void dfs(int v, int p, int d){
        parent[0][v] = p;
        depth[v] = d;
        for(auto u: g[v]){
            if(u.vertice != p){
                dfs(u.vertice, v, d+1);
            }
        }
    }

    void init(int n){
        parent = vector<vector<int>>(log_n, vector<int>(n, 0));
        depth = vector<int>(n, 0);

        dfs(root, -1, 0);
        for(int k = 0; k + 1 < log_n; k++){
            for(int v = 0; v < n; v++){
                if(parent[k][v] < 0) parent[k+1][v] = -1;
                else parent[k+1][v] = parent[k][parent[k][v]];
            }
        }
    }

    /**
     *
     * @param u
     * @param v
     * @return 頂点uと頂点vのLCA
     */
    int lca(int u, int v){
        if(depth[u] > depth[v]){
            swap(u, v);
        }
//        cout << u << " " << v << endl;

        for(int k = 0; k < log_n; k++) {
            if ((depth[v] - depth[u]) >> k & 1) {
                v = parent[k][v];
            }
        }
        if(u == v){
            return u;
        }
        for(int k = log_n-1; k >= 0; k--){
            if(parent[k][u] != parent[k][v]){
                u = parent[k][u];
                v = parent[k][v];
            }
        }

        return parent[0][u];
    }

    int getDepth(int v){
        return depth[v];
    }

};


int main(){
    uint n, q;   cin >> n;
    Graph g{n};
    for(int i = 0; i < n; i++){
        int k;  cin >> k;
        for(int j = 0; j < k; j++){
            int c;  cin >> c;
            g.add_edge(i, c);
            g.add_edge(c, i);
        }
    }
//    for(int i = 1; i < n; i++){
//        int p;   cin >> p;
//        g.add_edge(i, p);
//        g.add_edge(p, i);
//    }

    LCA lca(g);
    cin >> q;
    for(int i = 0; i < q; i++){
        int u, v;   cin >> u >> v;
        cout << lca.lca(u, v) << endl;
    }

    return 0;
}


Measured memory_bytes: 27576