// Slower C++ solution:
//include

//------------------------------------------

#include <vector>

#include <list>

#include <map>

#include <set>

#include <deque>

#include <stack>

#include <bitset>

#include <algorithm>

#include <functional>

#include <numeric>

#include <utility>

#include <sstream>

#include <iostream>

#include <iomanip>

#include <cstdio>

#include <cmath>

#include <cstdlib>

#include <cctype>

#include <string>

#include <cstring>

#include <ctime>

#include <climits>

#include <queue>



using namespace std;



//typedef

//------------------------------------------

typedef vector<int> VI;

typedef vector<VI> VVI;

typedef vector<string> VS;

typedef pair<int, int> PII;

typedef long long LL;



//container util

//------------------------------------------

#define ALL(a)  (a).begin(),(a).end()

#define RALL(a) (a).rbegin(), (a).rend()

#define PB push_back

#define MP make_pair

#define SZ(a) int((a).size())

#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)

#define EXIST(s,e) ((s).find(e)!=(s).end())

#define SORT(c) sort((c).begin(),(c).end())



//repetition

//------------------------------------------

#define FOR(i,a,b) for(int i=(a);i<(b);++i)

#define REP(i,n)  FOR(i,0,n)



//constant

//--------------------------------------------

const double EPS = 1e-10;

const double PI  = acos(-1.0);



typedef VVI Graph;

Graph G;

const int N = 100000;

const int LOG_N = 20;

int depth[N];

int parent[LOG_N][N];

void dfs(int node ,int p = -1, int d = 0)

{

  if(depth[node] >= 0) return;

  depth[node] = d;

  parent[0][node] = p;

  for(int i=0;i<SZ(G[node]);++i)

	dfs(G[node][i], node, d+1);

}



void init(int V){

  fill(depth, depth+N, -1);

  dfs(0);

  for(int i=0;i+1<LOG_N;++i){

	for(int v=0;v<V;++v){

	  if(parent[i][v] == -1) parent[i+1][v] = -1;

	  else parent[i+1][v] = parent[i][parent[i][v]];

	}

  }

}



int LCA(int u, int v){

  if(depth[u] > depth[v]) swap(u, v);

  //depth[u] <= depth[v]

  for(int i=0;i<LOG_N;++i)

	if((depth[v] - depth[u]) >> i & 1)

	  v = parent[i][v];

  if(u == v) return u;



  for(int i=LOG_N-1;i>=0;--i){

	if(parent[i][u] != parent[i][v]){

	  u = parent[i][u];

	  v = parent[i][v];

	}

  }

  return parent[0][u];

}



int main(){

  cin.tie(0);

  ios_base::sync_with_stdio(false);



  int V; cin >> V;

  G.assign(V, VI());

  REP(i,V){

	int k; cin >> k;

	REP(j,k){

	  int c; cin >> c;

	  G[i].PB(c);

	}

  }

  init(V);

  int Q; cin >> Q;

  while(Q--){

	int u, v; cin >> u >> v;

	cout << LCA(u,v) << endl;

  }

  

  return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.3867893268917189x):
//include

//------------------------------------------

#include <vector>

#include <list>

#include <map>

#include <set>

#include <deque>

#include <stack>

#include <bitset>

#include <algorithm>

#include <functional>

#include <numeric>

#include <utility>

#include <sstream>

#include <iostream>

#include <iomanip>

#include <cstdio>

#include <cmath>

#include <cstdlib>

#include <cctype>

#include <string>

#include <cstring>

#include <ctime>

#include <climits>

#include <queue>



using namespace std;



//typedef

//------------------------------------------

typedef vector<int> VI;

typedef vector<VI> VVI;

typedef vector<string> VS;

typedef pair<int, int> PII;

typedef long long LL;



//container util

//------------------------------------------

#define ALL(a)  (a).begin(),(a).end()

#define RALL(a) (a).rbegin(), (a).rend()

#define PB push_back

#define MP make_pair

#define SZ(a) int((a).size())

#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)

#define EXIST(s,e) ((s).find(e)!=(s).end())

#define SORT(c) sort((c).begin(),(c).end())



//repetition

//------------------------------------------

#define FOR(i,a,b) for(int i=(a);i<(b);++i)

#define REP(i,n)  FOR(i,0,n)



//constant

//--------------------------------------------

const double EPS = 1e-10;

const double PI  = acos(-1.0);



struct Edge{

  int to, cost;



  Edge(int t, int c = 0): to(t), cost(c)

  {}

  bool operator>(const Edge& rhs) const{

	return cost > rhs.cost;

  }

  bool operator<(const Edge& rhs) const{

	return cost < rhs.cost;

  }



};

using Graph =  vector< vector<Edge> >;





// calc Lowest Common Ancestor

// O(N logN)

struct LCA{

  const Graph& G;

  int N, R;

  int LOG_MAX;

  vector<int> depth;

  vector<vector<int>> parent;



  LCA(const Graph& G_, int N_, int R_):

	G(G_), N(N_), R(R_)

  {

	LOG_MAX = log2(N) + 1;

	depth.assign(N, -1);

	parent.resize(LOG_MAX);

	for(int i=0;i<LOG_MAX;++i)

	  parent[i].resize(N);



	function<void(int,int,int)> dfs = [&](int u, int p, int d){

	  depth[u] = d;

	  parent[0][u] = p;

	  for(const auto& e: G[u])

		if(e.to != p)

		  dfs(e.to, u, d+1);

	};

	dfs(R, -1, 0);

	

	for(int i=0;i+1<LOG_MAX;++i){

	  for(int v=0;v<N;++v){

		if(parent[i][v] == -1) parent[i+1][v] = -1;

		else parent[i+1][v] = parent[i][parent[i][v]];

	  }

	}

  }





  int query(int u, int v){

	if(depth[u] > depth[v]) swap(u, v);

	// assume: depth[u] <= depth[v]

	for(int i=0;i<LOG_MAX;++i)

	  if((depth[v] - depth[u]) >> i & 1)

		v = parent[i][v];

	if(u == v) return u;

	

	for(int i=LOG_MAX-1;i>=0;--i){

	  if(parent[i][u] != parent[i][v]){

		u = parent[i][u];

		v = parent[i][v];

	  }

	}

	return parent[0][u];

  }

};



int main(){

  cin.tie(0);

  ios_base::sync_with_stdio(false);



  int V; cin >> V;

  Graph G(V);

  REP(i,V){

	int k; cin >> k;

	REP(j,k){

	  int c; cin >> c;

	  G[i].PB(c);

	  G[c].PB(i);

	}

  }

  LCA lca(G, V, 0);



  int Q; cin >> Q;

  while(Q--){

	int u, v; cin >> u >> v;

	cout << lca.query(u,v) << endl;

  }

  

  return 0;

}