// Original C++ solution (slower):
#include <iostream>

#include <string>

#include <vector>

#include <queue>

#include <stack>

#include <map>

#include <algorithm>

#include <numeric>

#include <functional>

#include <set>

#include <sstream>

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <cmath>

#include <cctype>

#include <climits>

#include <fstream>

#include <bitset>

#include <time.h>

#include <assert.h>



#define LL long long

#define VI vector<int>

#define VL vector<long long>

#define FOR(i,a,b) for(int i= (a); i<((int)b); ++i)

#define RFOR(i,a) for(int i=(a); i >= 0; --i)

#define FOE(i,a) for(auto i : a)

#define ALL(c) (c).begin(), (c).end()

#define RALL(c) (c).rbegin(), (c).rend()

#define DUMP(x)  cerr << #x << " = " << (x) << endl;

#define SUM(x) std::accumulate(ALL(x), 0LL)

#define MIN(v) *std::min_element(v.begin(), v.end())

#define MAX(v) *std::max_element(v.begin(), v.end())

#define EXIST(v,x) (std::find(v.begin(), v.end(), x) != v.end())

#define BIT(n) (1LL<<(n))

#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

#define EPS 1e-14



using namespace std;



const int MAX_V = 100000;

const int MAX_LOG_V = 20;

// ??\???

vector<int> G[MAX_V]; // ??°???????????£??\???????????¨???

int root = 0; // ?????????????????????

int parent[MAX_LOG_V][MAX_V]; // ??????2^k????????£?????°???????????????(???????????????????????´??????-1??¨??????)

int depth[MAX_V]; // ??????????????±???



void dfs(int v, int p, int d) {

    parent[0][v] = p;

    depth[v] = d;

    for (int i = 0; i < G[v].size(); i++) {

        if (G[v][i] != p) dfs(G[v][i], v, d + 1);

    }

}



// ?????????

void init(int V) {

    // parent[0]??¨depth??????????????????

    dfs(root, -1, 0);

    // parent??????????????????

    for (int k = 0; k + 1 < MAX_LOG_V; k++) {

        for (int v = 0; v < V; v++) {

            if (parent[k][v] < 0) parent[k + 1][v] = -1;

            else parent[k + 1][v] = parent[k][parent[k][v]];

        }

    }

}



// u??¨v???LCA????±???????

int lca(int u, int v) {

    // u??¨v?????±??????????????????????????§????????????

    if (depth[u] > depth[v]) swap(u, v);

    for (int k = 0; k < MAX_LOG_V; k++) {

        if ((depth[v] - depth[u]) >> k & 1) {

            v = parent[k][v];

        }



    }

    if (u == v) return u;

    // ????????¢?´¢??§LCA????±???????

    for (int k = MAX_LOG_V - 1; k >= 0; k--) {

        if (parent[k][u] != parent[k][v]) {

            u = parent[k][u];

            v = parent[k][v];

        }

    }

    return parent[0][u];

}



int main(int argc, char *argv[]) {

    cin.tie(0);

    ios::sync_with_stdio(false);



    int n, k, c;

    cin >> n;

    

    FOR(i, 0, n) {

        cin >> k;

        FOR(j, 0, k) {

            cin >> c;

            G[i].push_back(c);

            G[c].push_back(i);

        }

    }

    init(n);



    int q, u, v;

    cin >> q;

    FOR(i, 0, q) {

        cin >> u >> v;

        cout << lca(u, v) << "\n";

    }



    return 0;

}

// Optimized solution to the same problem (faster):
#include <iostream>

#include <string>

#include <vector>

#include <queue>

#include <stack>

#include <map>

#include <algorithm>

#include <numeric>

#include <functional>

#include <set>

#include <sstream>

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <cmath>

#include <cctype>

#include <climits>

#include <fstream>

#include <bitset>

#include <time.h>

#include <assert.h>



#define LL long long

#define VI vector<int>

#define VL vector<long long>

#define FOR(i,a,b) for(int i= (a); i<((int)b); ++i)

#define RFOR(i,a) for(int i=(a); i >= 0; --i)

#define FOE(i,a) for(auto i : a)

#define ALL(c) (c).begin(), (c).end()

#define RALL(c) (c).rbegin(), (c).rend()

#define DUMP(x)  cerr << #x << " = " << (x) << endl;

#define SUM(x) std::accumulate(ALL(x), 0LL)

#define MIN(v) *std::min_element(v.begin(), v.end())

#define MAX(v) *std::max_element(v.begin(), v.end())

#define EXIST(v,x) (std::find(v.begin(), v.end(), x) != v.end())

#define BIT(n) (1LL<<(n))

#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

#define EPS 1e-14



using namespace std;



class LowestCommonAncestor {



public:

    // ??\???

    vector<vector<int>> graph;  // ??°???????????£??\???????????¨???

    int log_v;                  // log(??????????????°)

    vector<vector<int>> parent; // ??????2^k????????£?????°???????????????(???????????????????????´??????-1??¨??????)

    vector<int> depth;          // ??????????????±???



    LowestCommonAncestor(const vector<vector<int>> &graph):graph(graph) {

        int v = graph.size();

        this->log_v = int(log2(v)) + 1;

        parent = vector<vector<int>>(log_v, vector<int>(v));

        depth = vector<int>(v);

    }



    // ?????????

    void init(int root) {

        // parent[0]??¨depth??????????????????

        dfs(root, -1, 0);



        // parent??????????????????

        for (int k = 0; k + 1 < this->log_v; k++) {

            for (int v = 0; v < this->graph.size(); v++) {

                if (parent[k][v] < 0) parent[k + 1][v] = -1;

                else parent[k + 1][v] = parent[k][parent[k][v]];

            }

        }

    }



    // u??¨v???LCA????±???????

    int lca(int u, int v) {

        // u??¨v?????±??????????????????????????§????????????

        if (depth[u] > depth[v]) {

            swap(u, v);

        }

        for (int k = 0; k < this->log_v; ++k) {

            if ((depth[v] - depth[u]) >> k & 1) {

                v = parent[k][v];

            }



        }

        if (u == v) {

            return u;

        }



        // ????????¢?´¢??§LCA????±???????

        for (int k = this->log_v - 1; k >= 0; --k) {

            if (parent[k][u] != parent[k][v]) {

                u = parent[k][u];

                v = parent[k][v];

            }

        }

        return parent[0][u];

    }



private:

    void dfs(int v, int p, int d) {

        parent[0][v] = p;

        depth[v] = d;

        for (int i = 0; i < graph[v].size(); ++i) {

            if (graph[v][i] != p) {

                dfs(graph[v][i], v, d + 1);

            }

        }

    }

};



vector<vector<int>> graph; // ??°???????????£??\???????????¨???

int main(int argc, char *argv[]) {

    cin.tie(0);

    ios::sync_with_stdio(false);



    int n, k, c;

    cin >> n;

    

    graph = vector<vector<int>>(n);

    FOR(i, 0, n) {

        cin >> k;

        FOR(j, 0, k) {

            cin >> c;

            graph[i].push_back(c);

            graph[c].push_back(i);

        }

    }



    LowestCommonAncestor lca(graph);

    lca.init(0);



    int q, u, v;

    cin >> q;

    FOR(i, 0, q) {

        cin >> u >> v;

        cout << lca.lca(u, v) << "\n";

    }



    return 0;

}

// Fastest known solution for this problem: