#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
using namespace std;
using ll = long long;
using vec = vector<ll>;
using vect = vector<double>;
using Graph = vector<vector<ll>>;
#define loop(i, n) for (ll i = 0; i < n; i++)
#define Loop(i, m, n) for (ll i = m; i < n; i++)
#define pool(i, n) for (ll i = n; i >= 0; i--)
#define Pool(i, m, n) for (ll i = n; i >= m; i--)
#define mod 1000000007ll
#define setbit bitset<8>
#define flagcount __builtin_popcount
#define flag(x) (1 << x)
#define flagadd(bit, x) bit |= flag(x)
#define flagpop(bit, x) bit &= ~flag(x)
#define flagon(bit, i) bit &flag(i)
#define flagoff(bit, i) !(bit & (1 << i))
#define all(v) v.begin(), v.end()
#define low2way(v, x) lower_bound(all(v), x)
#define high2way(v, x) upper_bound(all(v), x)
#define count2way(v, x) high2way(v, x) - low2way(v, x)
#define lower(v, x) low2way(v, x) - v.begin()       //1番左が0、もし見つから無いならｎを出力
#define higher(v, x) high2way(v, x) - v.begin() - 1 //1番左が0、もし見つからないならn-1を出力（注意）
#define putout(a) cout << a << endl
#define putout2(a, b) \
    putout(a);        \
    putout(b)
#define putout3(a, b, c) \
    putout(a);           \
    putout(b);           \
    putout(c)
#define putout4(a, b, c, d) \
    putout(a);              \
    putout(b);              \
    putout(c);              \
    putout(d)
#define putout5(a, b, c, d, e) \
    putout(a);                 \
    putout(b);                 \
    putout(c);                 \
    putout(d);                 \
    putout(e)
#define Gput(a, b) G[a].push_back(b)
#define cin1(a) cin >> a
#define cin2(a, b) cin >> a >> b
#define cin3(a, b, c) cin >> a >> b >> c
#define cin4(a, b, c, d) cin >> a >> b >> c >> d
#define cin5(a, b, c, d, e) cin >> a >> b >> c >> d >> e
#define Sum(v) accumulate(all(v), 0ll)
#define gcd(x, y) __gcd(x, y)
ll ctoi(char c)
{
    if (c >= '0' && c <= '9')
    {
        return c - '0';
    }
    return 0;
}
template <typename T>
T lcm(T x, T y)
{
    T z = gcd(x, y);
    return x * y / z;
}
template <typename T>
bool primejudge(T n)
{
    if (n < 2)
        return false;
    else if (n == 2)
        return true;
    else if (n % 2 == 0)
        return false;
    double sqrtn = sqrt(n);
    for (T i = 3; i < sqrtn + 1; i++)
    {
        if (n % i == 0)
        {
            return false;
        }
        i++;
    }
    return true;
}
template <typename T>
T modinv(T a, T m)
{
    T b = m, u = 1, v = 0;
    while (b)
    {
        T t = a / b;
        a -= t * b;
        swap(a, b);
        u -= t * v;
        swap(u, v);
    }
    u %= m;
    if (u < 0)
        u += m;
    return u;
}
//場合によって使い分ける
//const ll dx[4]={1,0,-1,0};
//const ll dy[4]={0,1,0,-1};
const ll dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
//多次元配列の宣言法
//vector<vector<ll>> field(h, vector<ll>(w));
template <class T>
inline void chmax(T &a, T b)
{
    if (a < b)
        a = b;
}
template <class T>
inline void chmin(T &a, T b)
{
    if (a > b)
        a = b;
}
/*
ライブラリをここに置いてコメントを削除
*/
struct lca_Edge
{
    long long to;
};
using lca_Graph = vector<vector<lca_Edge>>;

/* LCA(G, root): 木 G に対する根を root として Lowest Common Ancestor を求める構造体
    query(u,v): u と v の LCA を求める。計算量 O(logn)
    前処理: O(nlogn)時間, O(nlogn)空間
*/
struct LCA
{
    vector<vector<ll>> parent; // parent[k][u]:= u の 2^k 先の親
    vector<ll> dist;           // root からの距離
    LCA(const lca_Graph &G, ll root = 0) { init(G, root); }

    // 初期化
    void init(const lca_Graph &G, ll root = 0)
    {
        ll V = G.size();
        ll K = 1;
        while ((1 << K) < V)
            K++;
        parent.assign(K, vector<ll>(V, -1));
        dist.assign(V, -1);
        lca_dfs(G, root, -1, 0);
        for (int k = 0; k + 1 < K; 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]];
                }
            }
        }
    }

    // 根からの距離と1つ先の頂点を求める
    void lca_dfs(const lca_Graph &G, ll v, ll p, ll d)
    {
        parent[0][v] = p;
        dist[v] = d;
        for (auto e : G[v])
        {
            if (e.to != p)
                lca_dfs(G, e.to, v, d + 1);
        }
    }

    ll query(ll u, ll v)
    {
        if (dist[u] < dist[v])
            swap(u, v); // u の方が深いとする
        ll K = parent.size();
        // LCA までの距離を同じにする
        for (ll k = 0; k < K; k++)
        {
            if ((dist[u] - dist[v]) >> k & 1)
            {
                u = parent[k][u];
            }
        }
        // 二分探索で LCA を求める
        if (u == v)
            return u;
        for (ll k = K - 1; k >= 0; k--)
        {
            if (parent[k][u] != parent[k][v])
            {
                u = parent[k][u];
                v = parent[k][v];
            }
        }
        return parent[0][u];
    }
    ll get_dist(ll u, ll v)
    {
        return dist[u] + dist[v] - 2 * dist[query(u, v)];
    }

    bool is_on_path(ll u, ll v, ll a)
    {
        return get_dist(u, a) + get_dist(a, v) == get_dist(u, v);
    }
};

int main()
{
    cout << fixed << setprecision(30);
    ll n;
    cin >> n;
    lca_Graph G(n);
    loop(i, n)
    {
        ll k;
        cin >> k;
        loop(j, k)
        {
            ll x;
            cin >> x;
            lca_Edge e;
            e.to = x;
            G[i].push_back(e);
        }
    }
    ll q;
    cin >> q;
    LCA tree(G);
    loop(i, q)
    {
        ll u, v;
        cin >> u >> v;
        ll par = tree.query(u, v);
        putout(par);
    }
    return 0;
}


Measured memory_bytes: 26200