#line 1 "test/aoj_GRL_5_C_segment_tree.test.cpp"
#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_5_C"
#include <cstdint>
#include <cstdio>
#include <vector>
#line 1 "utility/macro/stack_extend.cpp"
/**
* @brief スタック拡張マクロ(魔法)
* @author えびちゃん
* @see http://sigma425.hatenablog.com/entry/2016/03/26/221844
*/
#include <cstdlib>
#define BEGIN_STACK_EXTEND(size) \
void* stack_extend_memory_ = malloc(size); \
void* stack_extend_origin_memory_; \
char* stack_extend_dummy_memory_ = (char*)alloca((1+(int)(((long long)stack_extend_memory_)&127))*16); \
*stack_extend_dummy_memory_ = 0; \
asm volatile ("mov %%rsp, %%rbx\n\tmov %%rax, %%rsp":"=b"(stack_extend_origin_memory_):"a"((char*)stack_extend_memory_+(size)-1024));
#define END_STACK_EXTEND \
asm volatile ("mov %%rax, %%rsp"::"a"(stack_extend_origin_memory_)); \
free(stack_extend_memory_);
#line 1 "Graph/adjacency_list.cpp"
/**
* @brief 重みつきグラフの隣接リスト
* @author えびちゃん
*/
#include <cstddef>
#include <algorithm>
#include <type_traits>
#line 13 "Graph/adjacency_list.cpp"
template <typename WeightType>
class weighted_edge {
public:
using size_type = size_t;
using weight_type = WeightType;
protected:
size_type M_src, M_dst;
weight_type M_weight;
public:
weighted_edge() = default;
weighted_edge(weighted_edge const&) = default;
weighted_edge(weighted_edge&&) = default;
weighted_edge(size_type s, size_type d, weight_type w):
M_src(s), M_dst(d), M_weight(w)
{}
weighted_edge& operator =(weighted_edge const&) = default;
weighted_edge& operator =(weighted_edge&&) = default;
bool operator <(weighted_edge const& other) const {
if (M_weight < other.M_weight) return true;
if (other.M_weight < M_weight) return false;
if (M_src != other.M_src) return M_src < other.M_src;
return M_dst < other.M_dst;
}
size_type source() const { return M_src; }
size_type target() const { return M_dst; }
weight_type weight() const { return M_weight; }
};
struct directed_tag {};
struct undirected_tag {};
template <typename Edge, typename Directedness>
class adjacency_list {
public:
using size_type = size_t;
using edge_type = Edge;
using weight_type = typename Edge::weight_type;
private:
std::vector<std::vector<edge_type>> M_g;
public:
adjacency_list() = default;
adjacency_list(adjacency_list const&) = default;
adjacency_list(adjacency_list&&) = default;
explicit adjacency_list(size_type n): M_g(n) {}
template <typename... Args>
void emplace(size_type src, size_type dst, Args... args) {
M_g[src].emplace_back(src, dst, args...);
if (std::is_same<Directedness, undirected_tag>::value)
M_g[dst].emplace_back(dst, src, args...);
}
void sort_by_index() {
auto cmp = [](auto const& e1, auto const& e2) {
return e1.target() < e2.target();
};
for (auto v: M_g) std::sort(v.begin(), v.end(), cmp);
}
size_type size() const { return M_g.size(); }
auto const& operator [](size_type i) const { return M_g[i]; }
};
#line 1 "Graph/lowest_common_ancestor_segment_tree.cpp"
/**
* @brief 最深共通祖先 (segment tree)
* @author えびちゃん
*/
#line 11 "Graph/lowest_common_ancestor_segment_tree.cpp"
#include <tuple>
#include <utility>
#line 14 "Graph/lowest_common_ancestor_segment_tree.cpp"
#line 1 "integer/bit.cpp"
/**
* @brief ビット演算
* @author えびちゃん
*/
// XXX integral promotion 関連の注意をあまりしていません
#include <climits>
#include <type_traits>
template <typename Tp>
constexpr auto countl_zero(Tp n)
-> typename std::enable_if<std::is_unsigned<Tp>::value, int>::type
{
using value_type = typename std::make_unsigned<Tp>::type;
int bits = (sizeof(value_type) * CHAR_BIT);
if (n == 0) return bits;
int res = 0;
for (int i = bits / 2; i > 0; i /= 2) {
value_type mask = ((static_cast<value_type>(1) << i) - 1) << i;
if (n & mask) n >>= i;
else res += i;
}
return res;
}
template <typename Tp>
constexpr auto countl_one(Tp n)
-> typename std::enable_if<std::is_unsigned<Tp>::value, int>::type
{
using value_type = typename std::make_unsigned<Tp>::type;
return countl_zero(static_cast<value_type>(~n));
}
template <typename Tp>
constexpr auto countr_zero(Tp n)
-> typename std::enable_if<std::is_unsigned<Tp>::value, int>::type
{
using value_type = typename std::make_unsigned<Tp>::type;
int bits = (sizeof(value_type) * CHAR_BIT);
if (n == 0) return bits;
int res = 0;
for (int i = bits / 2; i > 0; i /= 2) {
value_type mask = ((static_cast<value_type>(1) << i) - 1);
if (!(n & mask)) res += i, n >>= i;
}
return res;
}
template <typename Tp>
constexpr auto countr_one(Tp n)
-> typename std::enable_if<std::is_unsigned<Tp>::value, int>::type
{
using value_type = typename std::make_unsigned<Tp>::type;
return countr_zero(static_cast<value_type>(~n));
}
constexpr unsigned long long half_mask[] = {
0x5555555555555555uLL, 0x3333333333333333uLL, 0x0F0F0F0F0F0F0F0FuLL,
0x00FF00FF00FF00FFuLL, 0x0000FFFF0000FFFFuLL, 0x00000000FFFFFFFFuLL
};
template <typename Tp>
constexpr auto popcount(Tp n)
-> typename std::enable_if<std::is_unsigned<Tp>::value, int>::type
{
int bits = static_cast<int>((sizeof n) * CHAR_BIT);
for (int i = 0, j = 1; j < bits; ++i, j *= 2) {
if (j <= 8) n = (n & half_mask[i]) + ((n >> j) & half_mask[i]);
else n += n >> j;
}
return n & 0xFF;
}
template <typename Tp>
constexpr auto parity(Tp n)
-> typename std::enable_if<std::is_unsigned<Tp>::value, int>::type
{ return popcount(n) & 1; }
template <typename Tp>
int clz(Tp n) { return countl_zero(static_cast<typename std::make_unsigned<Tp>::type>(n)); }
template <typename Tp>
int ctz(Tp n) { return countr_zero(static_cast<typename std::make_unsigned<Tp>::type>(n)); }
template <typename Tp>
int ilog2(Tp n) {
return (CHAR_BIT * sizeof(Tp) - 1) - clz(static_cast<typename std::make_unsigned<Tp>::type>(n));
}
template <typename Tp>
bool is_pow2(Tp n) { return (n > 0) && ((n & (n-1)) == 0); }
template <typename Tp>
Tp floor2(Tp n) { return is_pow2(n)? n: static_cast<Tp>(1) << ilog2(n); }
template <typename Tp>
Tp ceil2(Tp n) { return is_pow2(n)? n: static_cast<Tp>(2) << ilog2(n); }
template <typename Tp>
constexpr auto reverse(Tp n)
-> typename std::enable_if<std::is_unsigned<Tp>::value, Tp>::type
{
int bits = static_cast<int>((sizeof n) * CHAR_BIT);
for (int i = 0, j = 1; j < bits; ++i, j *= 2) {
n = ((n & half_mask[i]) << j) | ((n >> j) & half_mask[i]);
}
return n;
}
#line 1 "utility/literals.cpp"
/**
* @brief ユーザ定義リテラル
* @author えびちゃん
*/
#line 11 "utility/literals.cpp"
constexpr intmax_t operator ""_jd(unsigned long long n) { return n; }
constexpr uintmax_t operator ""_ju(unsigned long long n) { return n; }
constexpr size_t operator ""_zu(unsigned long long n) { return n; }
constexpr ptrdiff_t operator ""_td(unsigned long long n) { return n; }
constexpr int8_t operator ""_i8(unsigned long long n) { return n; }
constexpr int16_t operator ""_i16(unsigned long long n) { return n; }
constexpr int32_t operator ""_i32(unsigned long long n) { return n; }
constexpr int64_t operator ""_i64(unsigned long long n) { return n; }
constexpr uint8_t operator ""_u8(unsigned long long n) { return n; }
constexpr uint16_t operator ""_u16(unsigned long long n) { return n; }
constexpr uint32_t operator ""_u32(unsigned long long n) { return n; }
constexpr uint64_t operator ""_u64(unsigned long long n) { return n; }
#line 17 "Graph/lowest_common_ancestor_segment_tree.cpp"
class lowest_common_ancestor {
public:
using size_type = size_t;
private:
std::vector<size_type> M_e;
std::vector<std::pair<size_type, size_type>> M_st;
template <typename Tree>
void M_euler_tour(Tree const& g, size_type v, size_type p, size_type d = 0) {
M_e[v] = M_st.size();
M_st.emplace_back(d, v);
for (auto const& e: g[v]) {
size_type u = e.target();
if (u == p) continue;
M_euler_tour(g, u, v, d+1);
M_st.emplace_back(d, v);
}
}
void M_build_rmq() {
// segment tree
size_type n = M_st.size();
std::vector<std::pair<size_type, size_type>> tmp(n);
M_st.insert(M_st.begin(), tmp.begin(), tmp.end());
for (size_type i = n; i--;)
M_st[i] = std::min(M_st[i<<1|0], M_st[i<<1|1]);
}
public:
lowest_common_ancestor() = default;
lowest_common_ancestor(lowest_common_ancestor const&) = default;
lowest_common_ancestor(lowest_common_ancestor&&) = default;
template <typename Tree>
lowest_common_ancestor(Tree const& g, size_type r) {
size_type n = g.size();
M_e.resize(n);
M_st.reserve(n+n-2);
M_euler_tour(g, r, -1);
M_build_rmq();
}
lowest_common_ancestor& operator =(lowest_common_ancestor const&) = default;
lowest_common_ancestor& operator =(lowest_common_ancestor&&) = default;
size_type operator ()(size_type u, size_type v) const {
if (u == v) return u;
size_type l, r;
std::tie(l, r) = std::minmax(M_e[u], M_e[v]);
size_type n = M_st.size() >> 1;
l += n;
r += n+1;
std::pair<size_type, size_type> res{n, n};
while (l < r) {
if (l & 1) res = std::min(res, M_st[l++]);
if (r & 1) res = std::min(M_st[--r], res);
l >>= 1;
r >>= 1;
}
return res.second;
}
};
#line 10 "test/aoj_GRL_5_C_segment_tree.test.cpp"
int main() {
BEGIN_STACK_EXTEND(128*1024*1024);
size_t n;
scanf("%zu", &n);
adjacency_list<weighted_edge<int>, undirected_tag> g(n);
for (size_t i = 0; i < n; ++i) {
size_t k;
scanf("%zu", &k);
for (size_t j = 0; j < k; ++j) {
size_t c;
scanf("%zu", &c);
g.emplace(i, c, 1);
}
}
lowest_common_ancestor g_lca(g, 0);
size_t q;
scanf("%zu", &q);
for (size_t i = 0; i < q; ++i) {
size_t u, v;
scanf("%zu %zu", &u, &v);
printf("%zu\n", g_lca(u, v));
}
END_STACK_EXTEND;
}