#include <iostream>
#include <algorithm>
#include <utility>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <map>
#include <math.h>
#include <string.h>
#include <iomanip>
#include <numeric>
#include <cstdlib>
#include <cstdint>
#include <cmath>
#include <functional>
#include <limits>
#include <cassert>

using namespace std;

using ll = long long;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vll = vector<vl>;
using vpll = vector<pll>;

void debug_out() { cout << endl; }
 
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cout << H << " ";
  debug_out(T...);
}

#ifdef LOCAL
#define debug(...) cout << "debug: "; debug_out(__VA_ARGS__)
#else
#define debug(...) 
#endif

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define Rep(i, n) for (long long i = 0; i< (long long)(n); i++)
#define RRep(i,n) for (long long i = ((long long)(n)-1); i>=0; i--)
#define all(v) (v).begin(),(v).end()

struct Edge{
    int to;
    int weight;
    Edge(int t, int w) : to(t), weight(w){ }
};

struct edge{
    int from;
    int to;
    int weight;
    edge(int f,int t,int w) : from(f), to(t), weight(w){ }
};


using Graph = vector<vector<Edge>>;
using graph = vector<vector<int>>;
using edges = vector<edge>;

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

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

template <class T>
T gcd(T a, T b){
    if( b==0 ) return a;
    return gcd(b, a%b);
}

template <class T>
T lcm(T a, T b){
    return (a*b)/gcd(a,b);
}

bool isPalindrome(string s){
    string t = s;
    reverse(all(t));
    return s==t;
}

constexpr int INF = 1e9+7;

vector<int> dx = {-1,0,1,0};
vector<int> dy = {0,-1,0,1};

struct LCA{
    int n,log_n;
    graph g;
    vector<bool> used;
    vector<int> depth;
    vector<vector<int>> parent;

    LCA(int n) : n(n), log_n(1), g(n), used(n,false), depth(n,0) { }

    void addEdge(int a,int b){
        g[a].emplace_back(b);
        g[b].emplace_back(a);
    }

    void dfs(int v,int p,int d){
        used[v] = true;
        parent[0][v] = p;
        depth[v] = d;
        for(auto next_v: g[v]){
            if(!used[next_v]) dfs(next_v,v,d+1);
        }
    }

    void init(){
        while((1<<log_n)<n){
            log_n++;
        }
        parent.resize(log_n);
        for(int i=0; i<log_n; i++){
            parent[i].resize(n);
        }
        dfs(0,-1,0);
        for(int k = 0; k<log_n-1; k++){
            for(int v = 0; v<n; v++){
                if(parent[k][v]==-1) parent[k+1][v] = -1;
                else parent[k+1][v] = parent[k][parent[k][v]];
            }
        }
    }

    int lca(int u, int v){
        if(depth[u]>depth[v]) swap(u,v);
        for(int k = 0; k<log_n; k++){
            if((depth[v]-depth[u])>>k & 1){
                v = parent[k][v];
            }
        }
        if(u==v) return u;
        for(int k = log_n-1; k>=0; k--){
            if(parent[k][u]!=parent[k][v]){
                u = parent[k][u];
                v = parent[k][v];
            }
        }
        return parent[0][u];
    }

};

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    LCA l(n);
    rep(i,n){
        int k; cin >> k;
        rep(j,k){
            int c; cin >> c;
            l.addEdge(i,c);
        }
    }
    l.init();
    int q; cin >> q;
    rep(i,q){
        int u,v;
        cin >> u >> v;
        cout << l.lca(u,v) << endl;
    }
}


Measured memory_bytes: 18016