#include <array>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <stack>
#include <vector>

class kth_ancestor {

	using size_type = ::std::uint_least64_t;
	using container_type = ::std::vector<size_type>;
	using iterator = typename container_type::iterator;
	using const_iterator = typename container_type::const_iterator;
	using difference_type = typename container_type::difference_type;

	static size_type msb(size_type c) noexcept {
		static_assert(
			::std::numeric_limits<size_type>::digits == 64,
			"this implementation is specialized for 64 bit unsigned integer");
#ifdef __has_builtin
		return static_cast<size_type>(63 - __builtin_clzll(c));
#else
		static const ::std::array<::std::uint_least8_t, 64> table = {
			0,  1,  2,  7,  3,  13, 8,  27, 4,  33, 14, 36, 9,  49, 28, 19,
			5,  25, 34, 17, 15, 53, 37, 55, 10, 46, 50, 39, 29, 42, 20, 57,
			63, 6,  12, 26, 32, 35, 48, 18, 24, 16, 52, 54, 45, 38, 41, 56,
			62, 11, 31, 47, 23, 51, 44, 40, 61, 30, 22, 43, 60, 21, 59, 58 };
		c |= c >> 1;
		c |= c >> 2;
		c |= c >> 4;
		c |= c >> 8;
		c |= c >> 16;
		c |= c >> 32;
		return static_cast<size_type>(
			table[((c >> 1) + 1) * 0x218A7A392DD9ABFULL >> 58 & 0x3F]);
#endif
		// for 32 bit
		/*
		static const ::std::array<::std::uint_least8_t, 32> table = {
		0,  1, 2,  6,  3,  11, 7,  16, 4,  14, 12, 21, 8,  23, 17, 26,
		31, 5, 10, 15, 13, 20, 22, 25, 30, 9,  19, 24, 29, 18, 28, 27};
		c |= c >> 1;
		c |= c >> 2;
		c |= c >> 4;
		c |= c >> 8;
		c |= c >> 16;
		return static_cast<size_type>(
		table[((c >> 1) + 1) * 0x4653ADF >> 27 & 0x1F]);
		*/
	}

	container_type ladder;
	::std::vector<const_iterator> jump;

public:
	kth_ancestor(const ::std::vector<size_type> &parents) {
		const size_type n = parents.size();
		assert(!parents.empty() && "parents must not be empty");
		::std::vector<const_iterator> v_to_itr(n + 1);
		{
			::std::vector<::std::vector<size_type>> rev(n + 1);
			for (size_type i = static_cast<size_type>(0); i != n; ++i) {
				rev[parents[i]].emplace_back(i);
			}
			::std::vector<size_type> height(n + 1, static_cast<size_type>(0));
			{
				::std::stack<size_type, ::std::vector<size_type>> st(rev[n]);
				while (!st.empty()) {
					const size_type v = st.top();
					if (height[v] == static_cast<size_type>(0)) {
						for (const auto e : rev[v]) {
							st.emplace(e);
						}
						height[v] = static_cast<size_type>(1);
					}
					else {
						st.pop();
						if (height[v] >= height[parents[v]]) {
							height[parents[v]] = height[v] + static_cast<size_type>(1);
						}
					}
				}
			}
			for (auto &v : rev) {
				for (auto &e : v) {
					if (height[v.back()] < height[e]) {
						::std::swap(v.back(), e);
					}
				}
			}
			ladder.resize(n * 3 - height[rev[n].back()], n);
			v_to_itr[n] = ladder.cend() - static_cast<difference_type>(n);
			{
				::std::stack<size_type, ::std::vector<size_type>> st(rev[n]);
				iterator itr = ladder.end() - static_cast<difference_type>(n);
				while (!st.empty()) {
					const size_type v = st.top();
					st.pop();
					if (rev[parents[v]].back() != v) {
						itr -= static_cast<difference_type>(height[v]);
						::std::copy(v_to_itr[parents[v]],
							v_to_itr[parents[v]] +
							static_cast<difference_type>(height[v]),
							itr);
					}
					*--itr = v;
					v_to_itr[v] = itr;
					for (const auto e : rev[v]) {
						st.emplace(e);
					}
				}
			}
		}
		if (n != static_cast<size_type>(1)) {
			jump.resize(
				n * (msb(n - static_cast<size_type>(1)) + static_cast<size_type>(1)));
		}
		{
			::std::vector<size_type> curr(n + 1), next(n + 1);
			::std::copy(parents.cbegin(), parents.cend(), curr.begin());
			curr[n] = n;
			next[n] = n;
			for (size_type p = static_cast<size_type>(0);
				static_cast<size_type>(1) << p < n; ++p) {
				const size_type base = n * p;
				for (size_type i = static_cast<size_type>(0); i != n; ++i) {
					next[i] = curr[curr[i]];
					jump[base + i] = v_to_itr[curr[i]];
				}
				::std::swap(curr, next);
			}
		}
	}

	size_type size() const noexcept { return ladder.back(); }
	size_type get(const size_type x, const size_type k) const noexcept {
		assert(x < size() && "index out of range");
		if (k >= size()) {
			return size();
		}
		else if (k == static_cast<size_type>(0)) {
			return x;
		}
		else {
			const size_type p = msb(k);
			return *(
				jump[size() * p + x] +
				static_cast<difference_type>(k - (static_cast<size_type>(1) << p)));
		}
	}
};

#include <iostream>
#include <vector>

int main() {
	int n;
	::std::cin >> n;
	::std::vector<int> p(n, n);
	for (int i = 0;i != n;++i) {
		int k;
		::std::cin >> k;
		while (k--) {
			int c;
			::std::cin >> c;
			p[c] = i;
		}
	}
	const kth_ancestor ka({ p.cbegin(),p.cend() });
	int q;
	::std::cin >> q;
	const auto depth = [&](const int v) {
		int ok = 0, ng = n;
		while (ng - ok != 1) {
			const int mid = (ok + ng) / 2;
			if (ka.get(v, mid) != n) {
				ok = mid;
			}
			else {
				ng = mid;
			}
		}
		return ok;
	};
	while (q--) {
		int u, v;
		::std::cin >> u >> v;
		const int du = depth(u);
		const int dv = depth(v);
		if (du > dv) {
			u = ka.get(u, du - dv);
		}
		else {
			v = ka.get(v, dv - du);
		}
		int ng = -1, ok = n;
		while (ok - ng != 1) {
			const int mid = (ng + ok) / 2;
			if (ka.get(u, mid) != ka.get(v, mid)) {
				ng = mid;
			}
			else {
				ok = mid;
			}
		}
		::std::cout << ka.get(u, ok) << ::std::endl;
	}
	return 0;
}



Measured memory_bytes: 23212