This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/library/5/GRL/all/GRL_5_C"
#include "../../../src/template/template.hpp"
#include "../../../src/graph/graph_template.hpp"
#include "../../../src/tree/lowest_common_ancestor.hpp"
int main(void) {
int n;
cin >> n;
Graph<int> g(n);
rep(i, 0, n) {
int k;
cin >> k;
rep(j, 0, k) {
int c;
cin >> c;
g.add_edge(i, c);
}
}
LowestCommonAncestor<int> tree(g);
int q;
cin >> q;
while(q--) {
int u, v;
cin >> u >> v;
cout << tree.lca(u, v) << '\n';
}
}
#line 1 "verify/aizu_online_judge/grl/lowest_common_ancestor.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/library/5/GRL/all/GRL_5_C"
#line 2 "src/template/template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<long long, long long>;
#define rep(i, a, b) for(long long i = (a); i < (b); ++i)
#define rrep(i, a, b) for(long long i = (a); i >= (b); --i)
constexpr long long inf = 4e18;
struct SetupIO {
SetupIO() {
ios::sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(30);
}
} setup_io;
#line 3 "src/graph/graph_template.hpp"
template <typename T>
struct Edge {
int from, to;
T cost;
int idx;
Edge()
: from(-1), to(-1), cost(-1), idx(-1) {}
Edge(const int from, const int to, const T& cost = 1, const int idx = -1)
: from(from), to(to), cost(cost), idx(idx) {}
operator int() const {
return to;
}
};
template <typename T>
struct Graph {
Graph(const int N)
: n(N), es(0), g(N) {}
int size() const {
return n;
}
int edge_size() const {
return es;
}
void add_edge(const int from, const int to, const T& cost = 1) {
assert(0 <= from and from < n);
assert(0 <= to and to < n);
g[from].emplace_back(from, to, cost, es);
g[to].emplace_back(to, from, cost, es++);
}
void add_directed_edge(const int from, const int to, const T& cost = 1) {
assert(0 <= from and from < n);
assert(0 <= to and to < n);
g[from].emplace_back(from, to, cost, es++);
}
inline vector<Edge<T>>& operator[](const int& k) {
assert(0 <= k and k < n);
return g[k];
}
inline const vector<Edge<T>>& operator[](const int& k) const {
assert(0 <= k and k < n);
return g[k];
}
private:
int n, es;
vector<vector<Edge<T>>> g;
};
template <typename T>
using Edges = vector<Edge<T>>;
#line 4 "src/tree/lowest_common_ancestor.hpp"
template <typename T>
struct LowestCommonAncestor {
LowestCommonAncestor(const Graph<T>& g, const int root = 0) {
assert(0 <= root and root < g.size());
init(g, root);
}
int depth(const int v) const {
assert(0 <= v and v < n);
return dep[v];
}
T cost(const int v) const {
assert(0 <= v and v < n);
return co[v];
}
int parent(const int v) const {
assert(0 <= v and v < n);
return par[0][v];
}
int la(int v, int x) const {
assert(0 <= v and v < n);
assert(x >= 0);
if(x > dep[v]) return -1;
for(int i = 0; x > 0; ++i) {
if(x & 1) v = par[i][v];
x >>= 1;
}
return v;
}
int lca(int u, int v) const {
assert(0 <= u and u < n and 0 <= v and v < n);
if(dep[u] > dep[v]) swap(u, v);
v = la(v, dep[v] - dep[u]);
if(u == v) return u;
for(int i = (int)par.size() - 1; i >= 0; --i) {
if(par[i][u] != par[i][v]) {
u = par[i][u];
v = par[i][v];
}
}
return par[0][u];
}
int dist(const int u, const int v) const {
assert(0 <= u and u < n and 0 <= v and v < n);
return dep[u] + dep[v] - 2 * dep[lca(u, v)];
}
T length(const int u, const int v) const {
assert(0 <= u and u < n and 0 <= v and v < n);
return co[u] + co[v] - 2 * co[lca(u, v)];
}
private:
int n;
vector<vector<int>> par;
vector<int> dep;
vector<T> co;
void init(const Graph<T>& g, const int root = 0) {
n = g.size();
int h = 1;
while((1 << h) < n) ++h;
par.assign(h, vector<int>(n, -1));
dep.assign(n, -1);
co.assign(n, -1);
dfs(g, root, -1, 0, 0);
for(int i = 0; i + 1 < (int)par.size(); ++i) {
for(int v = 0; v < n; ++v) {
if(par[i][v] != -1) {
par[i + 1][v] = par[i][par[i][v]];
}
}
}
}
void dfs(const Graph<T>& g, const int v, const int p, const int d, const T& c) {
par[0][v] = p;
dep[v] = d;
co[v] = c;
for(const Edge<T>& e : g[v]) {
if(e.to == p) continue;
dfs(g, e.to, v, d + 1, c + e.cost);
}
}
};
#line 5 "verify/aizu_online_judge/grl/lowest_common_ancestor.test.cpp"
int main(void) {
int n;
cin >> n;
Graph<int> g(n);
rep(i, 0, n) {
int k;
cin >> k;
rep(j, 0, k) {
int c;
cin >> c;
g.add_edge(i, c);
}
}
LowestCommonAncestor<int> tree(g);
int q;
cin >> q;
while(q--) {
int u, v;
cin >> u >> v;
cout << tree.lca(u, v) << '\n';
}
}