// clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define forever for (;;)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
template<typename A> using V = std::vector<A>;
template<typename A> using F = std::function<A>;
template<typename A, typename B> using P = std::pair<A, B>;
using pii = P<int, int>;
using vi = V<int>;
using vd = V<double>;
using vs = V<std::string>;
using vpii = V<pii>;
using vvi = V<vi>;
using vvpii = V<vpii>;
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979;
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
constexpr bool odd(const int n) { return n & 1; }
constexpr bool even(const int n) { return ~n & 1; }
template<typename T> std::istream &operator>>(std::istream &is, std::vector<T> &v) { for (T &x : v) is >> x; return is; }
template<typename A, typename B> std::istream &operator>>(std::istream &is, std::pair<A, B> &p) { is >> p.first; is >> p.second; return is; }
using namespace std;
// clang-format on

using Weight = int;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(const int s = 0, const int d = 0, const Weight w = 0) : src(s), dst(d), weight(w) {}
};

using Edges = std::vector<Edge>;
using Array = std::vector<Weight>;
using Matrix = std::vector<Array>;

class Graph {
  std::vector<Edges> g;
  using iterator = std::vector<Edges>::iterator;
  using const_iterator = std::vector<Edges>::const_iterator;

public:
  Graph(const int size = 0) : g(size) {}
  size_t size() const { return g.size(); }
  const Edges &operator[](const int i) const { return g[i]; }
  void addArc(const int src, const int dst, const Weight w = 1) { g[src].emplace_back(src, dst, w); }
  void addEdge(const int node1, const int node2, const Weight w = 1) {
    addArc(node1, node2, w);
    addArc(node2, node1, w);
  }
  iterator begin() { return g.begin(); }
  const_iterator begin() const { return g.begin(); }
  iterator end() { return g.end(); }
  const_iterator end() const { return g.end(); }
};

class LCA {
  int n = 0;
  int log2_n = 0;
  std::vector<std::vector<int>> _parent;
  std::vector<int> _depth;

public:
  LCA() {}

  LCA(const Graph &g, int root = 0) : n(g.size()), log2_n(log2(n) + 1), _parent(log2_n, std::vector<int>(n)), _depth(n) {
    dfs(g, root, -1, 0);
    rep(k, log2_n - 1) rep(v, g.size()) _parent[k + 1][v] = _parent[k][v] < 0 ? -1 : _parent[k][_parent[k][v]];
  }

  void dfs(const Graph &g, int v, int p, int d) {
    _parent[0][v] = p;
    _depth[v] = d;
    for (auto &e : g[v]) {
      if (e.dst != p) dfs(g, e.dst, v, d + 1);
    }
  }

  int get(int u, int v) {
    if (_depth[u] > _depth[v]) std::swap(u, v);
    rep(k, log2_n) if ((_depth[v] - _depth[u]) >> k & 1) v = _parent[k][v];
    if (u == v) return u;
    for (int k = log2_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 depth(int v) { return _depth[v]; }
};

main {
  int n;
  cin >> n;
  Graph g(n);
  rep(i, n) {
    int k;
    cin >> k;
    while (k--) {
      int c;
      cin >> c;
      g.addEdge(i, c);
    }
  }
  LCA lca(g, 0);
  int q;
  cin >> q;
  while (q--) {
    int u, v;
    cin >> u >> v;
    cout << lca.get(u, v) << endl;
  }
}

Measured memory_bytes: 29492