#include "bits/stdc++.h"
using namespace std;
typedef long long ll;

// verify : https://onlinejudge.u-aizu.ac.jp/problems/DSL_2_A
template<class Monoid> struct SegmentTree {
private:
    using Func = std::function<Monoid(Monoid, Monoid)>;
    Func F;
    Monoid UNITY;
    int n;
    std::vector<Monoid> node;
public:
    SegmentTree() {}

    // val で埋める
    SegmentTree(int m, const Monoid &val, const Func f, const Monoid &unity) {
        F = f;
        UNITY = unity;
        n = 1; while (n < m) n <<= 1;
        node.resize(n * 2 - 1, UNITY);
        if (val != UNITY) {
            for (int i = 0; i < m; i++) node[i] = val;
            build();
        }
    }

    SegmentTree(const std::vector<Monoid>& v, const Func f, const Monoid &unity) {
        F = f;
        UNITY = unity;
        int sz = v.size();
        n = 1; while (n < sz) n <<= 1;
        node.resize(n * 2 - 1, UNITY);
        for (int i = 0; i < sz; i++) node[i + n - 1] = v[i];
        build();
    }

    // 最後に build が必要
    void set(int k, const Monoid &x) {
        node[n + k - 1] = x;
    }

    void build() {
        for (int i = n - 2; i >= 0; i--) node[i] = F(node[2 * i + 1], node[2 * i + 2]);
    }

    void update_query(int x, const Monoid &val) {
        if (x >= n || x < 0) return;
        x += n - 1;
        node[x] = val;
        while (x > 0) {
            x = (x - 1) >> 1;
            node[x] = F(node[2 * x + 1], node[2 * x + 2]);
        }
    }

    /*
    // 再帰のためお蔵
    Monoid query(int a, int b, int k = 0, int l = 0, int r = -1) {
        if (r < 0) r = n;
        if (r <= a || b <= l) return UNITY;
        if (a <= l && r <= b) return node[k];
        Monoid vl = query(a, b, 2 * k + 1, l, (r - l) / 2 + l);
        Monoid vr = query(a, b, 2 * k + 2, (r - l) / 2 + l, r);
        return F(vl, vr);
    }
    */

    // [a,b)
    Monoid get_query(int a, int b) {
        Monoid L = UNITY, R = UNITY;
        for (a += n, b += n; a < b; a >>= 1, b >>= 1) {
            if (a & 1) L = F(L, node[a++ - 1]);
            if (b & 1) R = F(node[--b - 1], R);
        }
        return F(L, R);
    }

    Monoid operator[](int x)const {
        return node[n + x - 1];
    }

    int size() {
        return n;
    }

    void print() {
        for (int i = 0; i < n; i++) {
            std::cout << i << "\t: " << node[n + i - 1] << std::endl;
        }
    }
};

// 最近共通祖先(EulerTour)
// verify : https://onlinejudge.u-aizu.ac.jp/problems/GRL_5_C
// <O(N),O(lonN)>
struct LowestCommonAncestor {
private:
    int V;
    const int INF = 1e9 + 6;
    std::vector< int > ord, dep, id;
    SegmentTree< std::pair< int, int > > st; // (value,index)
    void dfs(int u, int p, int k, const std::vector< std::vector< int > > &Tree) {
        id[u] = (int)ord.size();
        ord.push_back(u);
        dep[u] = k;
        for (auto v : Tree[u]) {
            if (v != p) {
                dfs(v, u, k + 1, Tree);
                ord.push_back(u);
            }
        }
    }

public:

    // Tree以外を与えるとバグる
    LowestCommonAncestor(const std::vector< std::vector< int > > &G, int root = 0) {
        build(G, root);
    }

    void build(const std::vector< std::vector< int > > &G, int root = 0) {
        V = G.size();
        dep.resize(V);
        id.resize(V, -1);
        ord.reserve(2 * V - 2);
        dfs(root, -1, 0, G);
        st = SegmentTree< std::pair< int, int > >(2 * V - 2, {INF, -1}, [](std::pair< int, int > a, std::pair< int, int > b) { return std::min(a, b); }, {INF, -1});
        for (int i = 0; i < 2 * V - 2; i++) {
            st.set(i, {dep[ord[i]], i});
        }
        st.build();
    }

    // u, vのlca O(logN)
    int lca(int u, int v) {
        return ord[st.get_query(std::min(id[u], id[v]), std::max(id[u], id[v]) + 1).second];
    }

    // u, vの距離 O(logN)
    int dist(int u, int v) {
        int lca_ = lca(u, v);
        return dep[u] + dep[v] - 2 * dep[lca_];
    }

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

int main() {
    int n; cin >> n;
    vector<vector<int>> 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[i].push_back(c);
            G[c].push_back(i);
        }
    }
    LowestCommonAncestor lca(G, 0);
    int q; cin >> q;
    for (int _ = 0; _ < q; _++) {
        int x, y; cin >> x >> y;
        cout << lca.lca(x, y) << endl;
    }
    return 0;
}


Measured memory_bytes: 17056