#include<iostream>
#include<climits>
#include<utility>

struct min_monoid {
    long long val;
    min_monoid() : val(LLONG_MAX) {}
    min_monoid(long long val) : val(val) {}
    bool operator==(const min_monoid &rhs) const { return val == rhs.val; }
    bool operator!=(const min_monoid &rhs) const { return std::rel_ops::operator!=(*this, rhs); }
    min_monoid &operator+=(const min_monoid &rhs) {
        val = std::min(val, rhs.val);
        return *this;
    }
    min_monoid operator+(const min_monoid rhs) const { return min_monoid(*this) += rhs; }
    friend std::istream &operator>>(std::istream &is, min_monoid &rhs) {
        long long v;
        is >> v;
        rhs = min_monoid(v);
        return is;
    }
    friend std::ostream &operator<<(std::ostream &os, const min_monoid &rhs) { return os << rhs.val; }
};

#include<vector>

template<typename T>
struct segment_tree {
    int n;
    T ZERO;
    std::vector<T> data;
    int get_size(int n_) const {
        int ret = 1;
        while (ret < n_) { ret <<= 1; }
        return ret;
    }
    segment_tree(int n_, T ZERO = T()) : n(get_size(n_)), ZERO(ZERO), data(n << 1, ZERO) {}
    segment_tree(const std::vector<T> &src, T ZERO = T()) : n(get_size(src.size())), ZERO(ZERO), data(n << 1) {
        std::copy(src.begin(), src.end(), data.begin() + n);
        for (int i = n - 1; i > 0; i--) { data[i] = data[i << 1] + data[(i << 1) + 1]; }
    }
    void set(int i, T x) {
        i += n;
        data[i] = x;
        while (i > 1) {
            i >>= 1;
            data[i] = data[i << 1] + data[(i << 1) + 1];
        }
    }
    T operator[](int i) const { return data[i + n]; }
    T get_sum(int l, int r) const {
        T ret = ZERO;
        for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
            if (l & 1) { ret += data[l], l++; }
            if (r & 1) { ret += data[r - 1]; }
        }
        return ret;
    }
};

#include<vector>
#include<functional>

struct heavy_light_decomposition {
    int n;
    std::vector<std::vector<int>> graph;
    std::vector<int> par, size, in, out, head;
    int idx;
    heavy_light_decomposition(int n)
        : n(n), graph(n), par(n), size(n), in(n), out(n), head(n) {}
    void add_edge(int u, int v) {
        graph[u].push_back(v);
        graph[v].push_back(u);
    }
    void dfs_size(int v, int p) {
        size[v] = 1;
        par[v] = p;
        for (int i = 0; i < graph[v].size(); i++) {
            int &u = graph[v][i];
            if (u == p) { continue; }
            dfs_size(u, v);
            size[v] += size[u];
            if (size[u] > size[graph[v][0]]) { std::swap(u, graph[v][0]); }
        }
    }
    void dfs_hld(int v) {
        in[v] = idx++;
        for (int i = 0; i < graph[v].size(); i++) {
            int u = graph[v][i];
            if (u == par[v]) { continue; }
            head[u] = (i == 0 ? head[v] : u);
            dfs_hld(u);
        }
        out[v] = idx;
    }
    std::vector<int> build(int root) {
        idx = 0;
        dfs_size(root, -1);
        head[root] = root;
        dfs_hld(root);
        std::vector<int> ret(n);
        for (int i = 0; i < n; i++) { ret[in[i]] = i; }
        return ret;
    }
    int get_lca(int u, int v) {
        while (true) {
            if (in[u] > in[v]) { std::swap(u, v); }
            if (head[u] == head[v]) { return u; }
            v = par[head[v]];
        }
    }
    void path_query(int u, int v, const std::function<void(int, int)> &f) {
        while (true) {
            if (in[u] > in[v]) { std::swap(u, v); }
            f(std::max(in[head[v]], in[u]), in[v] + 1);
            if (head[u] == head[v]) { return; }
            v = par[head[v]];
        }
    }
    void subtree_query(int v, const std::function<void(int, int)> &f) {
        f(in[v], out[v]);
    }
};

#include<bits/stdc++.h>
using namespace std;

int main() {
    int n;
    cin >> n;
    heavy_light_decomposition hld(n);
    for (int i = 0; i < n; i++) {
        int k;
        cin >> k;
        for (int j = 0; j < k; j++) {
            int c;
            cin >> c;
            hld.add_edge(i, c);
        }
    }
    vector<int> v = hld.build(0);
    vector<min_monoid> src(n);
    for (int i = 0; i < n; i++) { src[i] = v[i]; }
    segment_tree<min_monoid> st(src);
    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int u, v;
        cin >> u >> v;
        int a = hld.get_lca(u, v);
        int b = INT_MAX;
        hld.path_query(u, v, [&](int l, int r) { b = min(b, (int) st.get_sum(l, r).val); });
        assert(a == b);
        cout << a << endl;
    }
}


Measured memory_bytes: 17224