#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;
}

Measured memory_bytes: 23892