#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cwchar>
#include <cwctype>
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <array>
#include <atomic>
#include <chrono>
#include <codecvt>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <shared_mutex>

#ifdef DEBUG
#include "libs/debug.cpp"
#define DEBUG 1
#else
#define DEBUG 0
#endif

#define rep(i, n) repr((i), 0, (n))
#define repr(i, a, t) reps((i), (a), (t), 1)
#define reps(i, a, t, s) for (long long (i) = (a); (i) < (long long)(t); (i) += (s))
#define rrep(i, n) rrepr((i), (n), 0)
#define rrepr(i, a, t) rreps((i), (a), (t), 1)
#define rreps(i, a, t, s) for (long long (i) = (a) - 1; (i) >= (long long)(t); (i) -= (s))
#define each(v, c) for (auto &&(v) : (c))
#define all(c) (c).begin(), (c).end()

using namespace std;

// コンテナのメンバ関数を使わないと O(N) おぼえたらけす
template<typename T>
typename set<T>::iterator lower_bound(typename set<T>::iterator l, typename set<T>::iterator r, const T &v);
template<typename T>
typename set<T>::iterator upper_bound(typename set<T>::iterator l, typename set<T>::iterator r, const T &v);
template<typename S, typename T>
typename map<S, T>::iterator lower_bound(typename map<S, T>::iterator l, typename map<S, T>::iterator r, const T &v);
template<typename S, typename T>
typename map<S, T>::iterator upper_bound(typename map<S, T>::iterator l, typename map<S, T>::iterator r, const T &v);

using ll = long long;
using ull = unsigned long long;
using ld = long double;
const long long MOD = 1e9 + 7;
const long long INF = 1e18;

/**
 * LCA ダブリング版
 * 構築 O(NlogN)、クエリ O(logN)
 */
class LCA {
 private:
  vector<vector<ll>> graph;
  ll root;
  // 何回ダブリングするか
  ll height;
  // depths[v]: v の root からの距離
  vector<ll> depths;
  // parents[k][v]: 親に 2^k たどった頂点
  vector<vector<ll>> parents;

  void _init() {
    vector<bool> seen(graph.size(), false);
    _init_dfs(root, -1, 0, seen);

    // parents を初期化
    rep (k, height - 1) {
      rep (v, graph.size()) {
        if (parents[k][v] < 0) {
          // 親がなければ -1
          parents[k + 1][v] = -1;
        } else {
          parents[k + 1][v] = parents[k][parents[k][v]];
        }
      }
    }
  }

  // depths と parents[0] を初期化
  void _init_dfs(ll v, ll p, ll d, vector<bool> &seen) {
    parents[0][v] = p;
    depths[v] = d;
    seen[v] = true;
    each (u, graph[v]) {
      if (!seen[u]) {
        _init_dfs(u, v, d + 1, seen);
      }
    }
  }

 public:
  LCA(const vector<vector<ll>> &graph, ll root) : graph(graph), root(root) {
    height = 0;
    ll sz = graph.size();
    while (sz > 0) {
      sz /= 2;
      height += 1;
    }

    depths = vector<ll>(graph.size(), -1);
    parents = vector<vector<ll>>(height, vector<ll>(graph.size(), -1));

    _init();
  };

  /**
   * v と u の LCA
   * @param v
   * @param u
   */
  auto lca(ll v, ll u) -> ll {
    if (depths[u] > depths[v]) {
      swap(v, u);
    }
    rep (k, height) {
      if ((depths[v] - depths[u]) >> k & 1) {
        v = parents[k][v];
      }
    }
    if (v == u) return v;

    rrep (k, height) {
      if (parents[k][v] != parents[k][u]) {
        v = parents[k][v];
        u = parents[k][u];
      }
    }
    return parents[0][v];
  }

  /**
   * 2 頂点間の距離
   * @param v
   * @param u
   */
  ll distance(ll v, ll u) {
    return depths[v] + depths[u] - depths[lca(v, u)] * 2;
  }
};

int main() {
  cout << fixed << setprecision(10);
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  std::ifstream ifs;
  if (DEBUG) {
    ifs.open("src/_in.txt");
    cin.rdbuf(ifs.rdbuf());
  }

  ll N;
  cin >> N;
  vector<vector<ll>> graph(N, vector<ll>());
  rep (v, N) {
    ll k;
    cin >> k;
    vector<ll> children;
    rep (i, k) {
      ll v0;
      cin >> v0;
      children.push_back(v0);
    }
    each (u, children) {
      graph[v].push_back(u);
      graph[u].push_back(v);
    }
  }
  ll Q;
  cin >> Q;
  vector<tuple<ll, ll>> VU(Q);
  rep (i, Q) {
    cin >> get<0>(VU[i]) >> get<1>(VU[i]);
  }

  auto lca = LCA(graph, 0ll);

  each (vu, VU) {
    ll v, u;
    tie(v, u) = vu;
    cout << lca.lca(v, u) << "\n";
  }
}


Measured memory_bytes: 32996