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

using i64 = int64_t;

#define rep(i, x, y) for (i64 i = i64(x), i##_max_for_repmacro = i64(y); i < i##_max_for_repmacro; ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif

const int inf = 1.01e9;
const i64 inf64 = 4.01e18;
const double eps = 1e-9;

template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "(" << p.first << ", " << p.second << ")";
    return os;
}

template <typename T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "[";
    for (const auto &v : vec) {
        os << v << ",";
    }
    os << "]";
    return os;
}

template <typename T>
bool chmin(T &a, const T &b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

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

template<typename T, size_t size> void fill(T (&ary)[size], const T& val){
    fill_n((T*)ary, size, val);
}

/*
 * 構築: O(nlog(n))
 * LCAの計算: O(log(n))
 */
class lca_solver {
public:
    vector<vector<int>> tree; // tree[i] = 頂点iに隣接する頂点のリスト
    int root;
    // if root,parent is -1.
    vector<int> parent;
    vector<int> depth;
    vector<vector<int>> parent_pow2;
    lca_solver(vector<vector<int>> &tree_, int root_)
            : tree(tree_), root(root_) {
        calc_parent_and_depth();
        calc_parent_pow2();
    };
    int lca(int u, int v) {
        // make sure depth(u) > depth(v).
        if (depth[u] < depth[v]) swap(u, v);
        for (size_t k = 0; k < parent_pow2.size(); k++) {
            if (((depth[u] - depth[v]) >> k) & 1) {
                u = parent_pow2[k][u];
            }
        }
        if (u == v) return u;
        for (int k = (int)parent_pow2.size() - 1; k >= 0; k--) {
            if (parent_pow2[k][u] != parent_pow2[k][v]) {
                u = parent_pow2[k][u];
                v = parent_pow2[k][v];
            }
        }
        return parent_pow2[0][u];
    }
    int dist(int u, int v) {
        return depth[u] + depth[v] - depth[lca(u, v)] * 2;
    }

private:
    void calc_parent_and_depth() {
        parent = vector<int>(tree.size(), -1);
        depth = vector<int>(tree.size(), -1);
        sub_calc_parent_and_depth(root, -1, 0);
    }
    void sub_calc_parent_and_depth(int cur, int par, int dep) {
        parent[cur] = par;
        depth[cur] = dep;
        for (int child : tree[cur]) {
            if (child != par) {
                sub_calc_parent_and_depth(child, cur, dep + 1);
            }
        }
    }
    void calc_parent_pow2() {
        // parent_pow2[k][i] = 2^k parent of node i.
        parent_pow2 = vector<vector<int>>(ceil(log(tree.size()) / log(2) + 1),
                                          vector<int>(tree.size(), -1));
        parent_pow2[0] = parent;
        for (size_t k = 0; k + 1 < parent_pow2.size(); k++) {
            for (size_t v = 0; v < tree.size(); v++) {
                if (parent_pow2[k][v] >= 0) {
                    parent_pow2[k + 1][v] = parent_pow2[k][parent_pow2[k][v]];
                }
            }
        }
    }
};

void solve() {
    //const i64 mod = 1'000'000'007;

    i64 n;
    cin >> n;

    vector<vector<int>> tree(n);
    rep(i,0,n){
        i64 k;
        cin >> k;

        rep(j,0,k){
            int c;
            cin >> c;
            tree[i].emplace_back(c);
            tree[c].emplace_back(i);
        }
    }

    lca_solver ls(tree,0);

    i64 q;
    cin >> q;
    rep(i,0,q){
        int u,v;
        cin >> u >> v;
        cout << ls.lca(u,v) << endl;
    }
}

int main() {
    std::cin.tie(0);
    std::ios::sync_with_stdio(false);
    cout.setf(ios::fixed);
    cout.precision(16);
    solve();
    return 0;
}



Measured memory_bytes: 25200