using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using static System.Math;
using MethodImplOptions = System.Runtime.CompilerServices.MethodImplOptions;
using MethodImplAttribute = System.Runtime.CompilerServices.MethodImplAttribute;

class TwoEdgeDecomposition {
  private int n;
  private int[] ord;
  private int[] low;
  private List< int >[] graph;

  private int ord_ = 0;
  private int cmp_ = 0;


  public SortedSet< Tuple< int, int > > bridges { get; }
  public int[] cmp { get; }
  public List< SortedSet< int > > quotient { get; }

  void dfs(int crr, int prv) {
    low[crr] = ord[crr] = ord_++;
    foreach (int nxt in graph[crr]) {
      if (nxt == prv) continue;
      if (ord[nxt] != -1) {
        low[crr] = Min(low[crr], ord[nxt]);
        continue;
      }
      dfs(nxt, crr);
      low[crr] = Min(low[crr], low[nxt]);
      if (ord[crr] < low[nxt]) {
        cmp_++;
        bridges.Add(new Tuple< int, int >(crr, nxt));
        bridges.Add(new Tuple< int, int >(nxt, crr));
      }
    }
    cmp[crr] = cmp_;
    return;
  }

  public TwoEdgeDecomposition(int n) {
    this.n = n;
    ord = Enumerable.Repeat(-1, n).ToArray();
    low = new int[n];
    graph = Enumerable.Repeat(0, n).Select(_ => new List< int >()).ToArray();
    bridges = new SortedSet< Tuple< int, int > >();
    cmp = new int[n];
    quotient = new List< SortedSet< int > >();
  }
  public void insert(int i, int j) {
    graph[i].Add(j);
    graph[j].Add(i);
  }
  public void build() {

    foreach (int i in Enumerable.Range(0, n)) {
      if (ord[i] == -1) dfs(i, i);
    }

    cmp_++;
    foreach (int _ in Enumerable.Range(0, cmp_)) {
      quotient.Add(new SortedSet< int >());
    }
    foreach (int i in Enumerable.Range(0, n)) {
      foreach (int j in graph[i]) {
        if (cmp[i] == cmp[j]) continue;
        quotient[cmp[i]].Add(cmp[j]);
      }
    }
  }
};

class LowestCommonAncestor {
  private int n;
  private int lg;
  private List< int >[] graph;
  private int[] prt;
  private int[] depth;
  private int[,] table;

  private void dfs(int crr) {
    foreach (int nxt in graph[crr]) {
      if (nxt == prt[crr]) continue;
      prt[nxt] = crr;
      depth[nxt] = depth[crr] + 1;
      dfs(nxt);
    }
  }

  public LowestCommonAncestor(int n) {
    this.n = n;
    lg = 30;
    graph = Enumerable.Repeat(0, n).Select(_ => new List< int >()).ToArray();
    prt = new int[n];
    depth = Enumerable.Repeat(0, n).ToArray();
    table = new int[lg + 1, n];
  }
  public void insert(int i, int j) {
    graph[i].Add(j);
    graph[j].Add(i);
  }
  public void build(int root = 0) {
    dfs(root);
    prt[root] = prt[root];

    foreach (int i in Enumerable.Range(0, lg)) {
      foreach (int j in Enumerable.Range(0, n)) {
        table[i, j] = i == 0
          ? prt[j]
          : table[i - 1, table[i - 1, j]];
      }
    }
  }
  public int lca(int u, int v) {
    if (depth[u] > depth[v]) {
      int tmp = u;
      u = v;
      v = tmp;
    }
    int diff = depth[v] - depth[u];
    foreach (int i in Enumerable.Range(0, lg + 1).Reverse()) {
      if (Pow(2, i) < depth[v] - depth[u]) {
        v = table[i, v];
        diff -= (int)Pow(2, i);
        Debug.Assert(diff == depth[v] - depth[u]);
      }
    }
    if (depth[u] < depth[v]) {
      v = prt[v];
    }
    Debug.Assert(depth[u] == depth[v]);
    if (u == v) return u;
    foreach (int i in Enumerable.Range(0, lg + 1).Reverse()) {
      int new_u = table[i, u];
      int new_v = table[i, v];
      if (new_u != new_v) {
        u = new_u;
        v = new_v;
      }
    }
    Debug.Assert(u != v);
    u = prt[u];
    v = prt[v];
    Debug.Assert(u == v);
    return u;
  }
  public int dist(int u, int v) => depth[u] + depth[v] - 2 * depth[lca(u, v)];
}

public static class Program {
  public static void Main() {
    int n = int.Parse(Console.ReadLine());
    var lca = new LowestCommonAncestor(n);
    foreach (int i in Enumerable.Range(0, n)) {
      foreach (int j in Console.ReadLine().Split().Skip(1).Select(int.Parse)) {
        lca.insert(i, j);
      }
    }
    lca.build();

    int q = int.Parse(Console.ReadLine());
    foreach (int _ in Enumerable.Range(0, q)) {
      int[] uv = Console.ReadLine().Split().Select(int.Parse).ToArray();
      int u = uv[0];
      int v = uv[1];
      int ans = lca.lca(u, v);
      Console.WriteLine(ans);
    }
  }
}



Measured memory_bytes: 55292