#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std;

template <class T>
using lp_queue=priority_queue<T, deque<T>, greater<T>>;

using Weight=long long;
static const Weight INF=1LL<<57;

struct Edge {
    size_t src, dst;
    Weight weight;
    Edge() {}
    Edge(size_t src, size_t dst, Weight weight=1):
        src(src), dst(dst), weight(weight)
    {}
};

bool operator<(const Edge &e, const Edge &f) {
    if (e.weight != f.weight) {
        return e.weight > f.weight;
    } else {
        return e.src!=f.src? e.src<f.src : e.dst<f.dst;
    }
}

using Edges=vector<Edge>;
using Vertex=vector<Edge>;
using Graph=vector<Vertex>;

void join(Graph &g, size_t s, size_t d, Weight w=1) {
    // non-directed
    g[s].push_back(Edge(s, d, w));
    g[d].push_back(Edge(d, s, w));
}

void connect(Graph &g, size_t s, size_t d, Weight w=1) {
    // directed
    g[s].push_back(Edge(s, d, w));
}

vector<size_t> get_parents(const Graph &g, size_t root=0) {
    size_t V=g.size();
    vector<size_t> parents(V);
    for (size_t i=0; i<V; ++i)
        for (const Edge &e: g[i])
            parents[e.dst] = e.src;

    return parents;
}

vector<vector<size_t>> get_ancestors(
    const Graph &g, const vector<size_t> &parents
) {
    size_t V=g.size();
    vector<vector<size_t>> ancestors(V);
    for (size_t i=0; i<V; ++i)
        ancestors[i].push_back(parents[i]);

    for (size_t j=0; (1u<<j)<V; ++j)
        for (size_t i=0; i<V; ++i) {
            if (j < ancestors[i].size() && ~ancestors[i][j]) {
                size_t parent=ancestors[i][j];
                ancestors[i].push_back(ancestors[parent][j]);
            }
        }

    return ancestors;
}

vector<size_t> get_depth(const Graph &g, size_t root=0) {
    size_t V=g.size();
    vector<size_t> depth(V);

    queue<size_t> q; q.push(root);
    while (!q.empty()) {
        size_t v=q.front(); q.pop();
        for (const Edge &e: g[v]) {
            depth[e.dst] = depth[v] + 1;
            q.push(e.dst);
        }
    }

    return depth;
}

size_t get_kth_ancestor(
    size_t v, size_t k, const vector<vector<size_t>> &ancestors
) {
    for (size_t i=0; k; (++i, k>>=1))
        if (k & 1)
            v = ancestors[v][i];

    return v;
}

size_t lcancestor(
    size_t u, size_t v, const vector<vector<size_t>> &ancestors,
    vector<size_t> &depth
) {
    if (depth[u] > depth[v])
        swap(u, v);

    v = get_kth_ancestor(v, depth[v]-depth[u], ancestors);

    if (u == v) return u;
    for (size_t i=ancestors[0].size(); i--;)
        if (ancestors[u][i] != ancestors[v][i]) {
            u = get_kth_ancestor(u, 1<<i, ancestors);
            v = get_kth_ancestor(v, 1<<i, ancestors);
        }

    return ancestors[u][0];
}

int main() {
    size_t V;
    scanf("%zu", &V);

    Graph g(V);
    for (size_t i=0; i<V; ++i) {
        size_t k;
        scanf("%zu", &k);
        for (size_t j=0; j<k; ++j) {
            size_t c;
            scanf("%zu", &c);

            connect(g, i, c);
        }
    }

    vector<size_t> depth=get_depth(g, 0), parents=get_parents(g);
    vector<vector<size_t>> ancestors=get_ancestors(g, parents);

    size_t q;
    scanf("%zu", &q);

    for (size_t i=0; i<q; ++i) {
        size_t u, v;
        scanf("%zu %zu", &u, &v);

        printf("%zd\n", lcancestor(u, v, ancestors, depth));
    }

    return 0;
}

Measured memory_bytes: 39464