#include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <numeric>
#include <list>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <tuple>
#include <deque>
#include <complex>

using namespace std;

/*
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
*/

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<long long, long long> pll;
typedef vector<pll> vpll;

typedef long double ld;
typedef vector<ld> vld;

typedef vector<bool> vb;

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define reps(i, n) for (ll i = 1; i <= (n); i++)
#define rrep(i, n) for (ll i = (n) - 1; i >= 0; i--)
#define rreps(i, n) for (ll i = (n); i >= 1; i--)
#define all(v) (v).begin(), (v).end()

template <class T> void chmin(T& a, T b) { a = min(a, b);}
template <class T> void chmax(T& a, T b) { a = max(a, b);}

constexpr int INF = 1 << 30;
constexpr ll INFL = 1LL << 60;
constexpr ll MOD = 1000000007;
constexpr ld EPS = 1e-12;
ld PI = acos(-1.0);

struct Graph {
    ll n;
    vvll graph;
    vll depth;
    vll par;

    Graph() {
        read_graph();
        init();
    }

    void read_graph() {
        cin >> n;
        graph = vvll(n);
        for(ll i = 0; i < n; i++) {
            ll k;
            cin >> k;
            for(ll j = 0; j < k; j++) {
                ll c;
                cin >> c;
                graph[i].push_back(c);
            }
        }
    }

    void init() {
        depth = vll(n);
        par = vll(n, -1);
        dfs(0, 0);
    }

    void dfs(ll x, ll d) {
        depth[x] = d;
        for(auto& i : graph[x]) {
            if(i == par[x]) continue;
            par[i] = x;
            dfs(i, d + 1);
        }
    }
};

struct LCA {
    Graph g;
    vvll par;

    LCA(Graph& g) : g(g) {
        set_par();
    };

    void set_par() {
        ll max_d = 0;
        ll n = g.n;
        for(ll i = 0; i < n; i++) chmax(max_d, g.depth[i]);
        ll log_d = 0;
        while((1 << log_d) < max_d) log_d++;
        par = vvll(log_d + 1, vll(n, -1));
        par[0] = g.par;
        for(ll i = 1; i <= log_d; i++) {
            for(ll j = 0; j < n; j++) {
                if(par[i - 1][j] != -1) {
                    par[i][j] = par[i - 1][par[i - 1][j]];
                }
            }
        }
    }

    ll get_lca(ll u, ll v) {
        if(g.depth[u] < g.depth[v]) swap(u, v);
        ll log_d = par.size();
        for(ll i = 0; i < log_d; i++) {
            if((g.depth[u] - g.depth[v]) & (1 << i)) u = par[i][u];
        }
        if(u == v) return u;
        for(ll i = log_d - 1; i >= 0; i--) {
            if(par[i][u] == par[i][v]) continue;
            u = par[i][u], v = par[i][v];
        }
        return par[0][u];
    }
};

void solve() {
    Graph g;
    LCA l(g);
    ll q;
    cin >> q;
    vll ans;
    for(ll i = 0; i < q; i++) {
        ll u, v;
        cin >> u >> v;
        ans.push_back(l.get_lca(u, v));
    }
    for(auto i : ans) cout << i << endl;
    return;
}

int main() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    solve();
}


Measured memory_bytes: 36960