#include <bits/stdc++.h>

int ri() {
	int n;
	scanf("%d", &n);
	return n;
}

struct LCT {
#define l ch[0]
#define r ch[1]
	struct Node {
		Node *p;
		Node *ch[2];
		bool rev;
		int64_t sum;
		int64_t val;
		int size;
		void fetch() {
			if (l != LCT::NONE) l->flush();
			if (r != LCT::NONE) r->flush();
			sum = val + l->sum + r->sum;
			size = 1 + l->size + r->size;
		}
		void flush() {
			if (rev) {
				l->rev ^= 1;
				r->rev ^= 1;
				std::swap(l, r);
				rev = false;
			}
		}
		bool is_root() {
			return p == LCT::NONE || (p->l != this && p->r != this);
		}
		void rotate(int dir) {
			Node *new_root = ch[!dir], *par = p;
			ch[!dir] = new_root->ch[dir];
			ch[!dir]->p = this;
			new_root->ch[dir] = this;
			p = new_root;
			fetch(), new_root->fetch();
			new_root->p = par;
			if (par->l == this) par->l = new_root;
			else if (par->r == this) par->r = new_root;
		}
	};
	void splay(Node *node) {
		node->flush();
		while (!node->is_root()) {
			Node *p = node->p;
			if (p->is_root()) {
				p->flush(), node->flush();
				p->rotate(p->l == node);
			} else {
				Node *pp = p->p;
				pp->flush(), p->flush(), node->flush();
				bool flag1 = p->r == node;
				bool flag2 = pp->r == p;
				if (flag1 == flag2) pp->rotate(!flag2);
				p->rotate(!flag1);
				if (flag1 != flag2) pp->rotate(!flag2);
			}
		}
	}
	static Node *nodes, *NONE;
	static int head;
	LCT () {
		if (!head) nodes[head++] = {NONE, {NONE, NONE}, false, 0, 0, 0};
	}
	Node *expose(Node *start) {
		Node *prev = NONE;
		for (Node *cur = start; cur != NONE; cur = cur->p) {
			splay(cur);
			cur->r = prev;
			cur->fetch();
			prev = cur;
		}
		splay(start);
		return prev;
	}
	void link(Node *child, Node *parent) {
		expose(child), expose(parent);
		parent->r = child;
		child->p = parent;
		parent->fetch();
	}
	void cut(Node *child) {
		expose(child);
		Node *parent = child->l;
		parent->p = NONE;
		child->l = NONE;
		child->fetch();
	}
	void reroot(Node *node) {
		expose(node);
		node->rev ^= 1;
		node->flush();
	}
	Node *new_node(int val) {
		return &(nodes[head++] = {NONE, {NONE, NONE}, false, val, val, 1});
	}
	Node *get_root(Node *node) {
		expose(node);
		for (; node->flush(), (node->l != NONE); node = node->l);
		return node;
	}
	Node *lca(Node *a, Node *b) {
		expose(a);
		return expose(b);
	}
	void debug(Node *node, int indent = 0, int dir = 0) {
		if (node == NONE) return;
		for (int i = 0; i < indent; i++) fprintf(stderr, " ");
		if (dir) fprintf(stderr, dir > 0 ? "R " : "L ");
		fprintf(stderr, "%d : val:%lld sz:%d p:%d rev:%d\n", (int) (node - nodes), (long long) node->val, node->size, (int) (node->p - nodes), node->rev);
		
		debug(node->l, indent + 1, -1);
		debug(node->r, indent + 1, 1);
	}
#undef l
#undef r
};
LCT::Node *LCT::nodes = (Node *) malloc(sizeof(Node) * 1000000), *LCT::NONE = nodes;
int LCT::head = 0;

// verify LCT

int main() {
	//*
	int n = ri();
	LCT tree;
	LCT::Node *nodes[n];
	for (int i = 0; i < n; i++) nodes[i] = tree.new_node(i);
	
	for (int i = 0; i < n; i++) {
		int k = ri();
		for (int j = 0; j < k; j++) {
			int t = ri();
			tree.reroot(nodes[t]);
			tree.reroot(nodes[i]);
			tree.link(nodes[t], nodes[i]);
		}
	}
	tree.reroot(nodes[0]);
	int q = ri();
	for (int i = 0; i < q; i++) {
		int a = ri(), b = ri();
		printf("%d\n", (int) tree.lca(nodes[a], nodes[b])->val);
	}
	//*/
	
	/*
	constexpr int n = 100;
	std::set<std::pair<int, int> > hens;
	LCT tree;
	LCT::Node *nodes[n];
	for (int i = 0; i < n; i++) nodes[i] = tree.new_node(i);
	for (; ;) {
		std::string command;
		std::cin >> command;
		if (command == "add") {
			int a = ri(), b = ri();
			if (a > b) std::swap(a, b);
			hens.insert({a, b});
			tree.reroot(nodes[a]);
			tree.link(nodes[a], nodes[b]);
		} else if (command == "remove") {
			int a = ri(), b = ri();
			if (a > b) std::swap(a, b);
			if (!hens.count({a, b})) {
				std::cerr << "not in the hens" << std::endl;
				continue;
			}
			hens.erase({a, b});
			tree.reroot(nodes[b]);
			tree.cut(nodes[a]);
		} else if (command == "root") {
			int x = ri();
			std::cout << (tree.get_root(nodes[x]) - tree.nodes - 1) << std::endl;
			
			
		} else if (command == "reroot") {
			tree.reroot(nodes[ri()]);
		}
	}
	//*/
	
	return 0;
}



Measured memory_bytes: 9496