/* O((N + Q) log^2 N) 解 */ #include using namespace std; using int64 = long long; //const int mod = 1e9 + 7; const int mod = 998244353; const int64 infll = (1LL << 62) - 1; const int inf = (1 << 30) - 1; struct IoSetup { IoSetup() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); } } iosetup; template< typename T1, typename T2 > ostream &operator<<(ostream &os, const pair< T1, T2 > &p) { os << p.first << " " << p.second; return os; } template< typename T1, typename T2 > istream &operator>>(istream &is, pair< T1, T2 > &p) { is >> p.first >> p.second; return is; } template< typename T > ostream &operator<<(ostream &os, const vector< T > &v) { for(int i = 0; i < (int) v.size(); i++) { os << v[i] << (i + 1 != v.size() ? " " : ""); } return os; } template< typename T > istream &operator>>(istream &is, vector< T > &v) { for(T &in: v) is >> in; return is; } template< typename T1, typename T2 > inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } template< typename T1, typename T2 > inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); } template< typename T = int64 > vector< T > make_v(size_t a) { return vector< T >(a); } template< typename T, typename... Ts > auto make_v(size_t a, Ts... ts) { return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...)); } template< typename T, typename V > typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) { t = v; } template< typename T, typename V > typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) { for(auto &e: t) fill_v(e, v); } template< typename F > struct FixPoint : F { FixPoint(F &&f) : F(forward< F >(f)) {} template< typename... Args > decltype(auto) operator()(Args &&... args) const { return F::operator()(*this, forward< Args >(args)...); } }; template< typename F > inline decltype(auto) MFP(F &&f) { return FixPoint< F >{forward< F >(f)}; } /** * @brief 何でもできるが遅いLCT */ template< typename T, template< typename > typename E > struct SlowLinkCutTree { private: struct Node { Node *l, *r, *p; T info; E< T > sum; bool is_root() const { return not p or (p->l != this and p->r != this); } explicit Node(const T &info) : info(info), l(nullptr), r(nullptr), p(nullptr) {} }; public: using NP = Node *; private: const E< T > e; void rotr(NP t) { NP x = t->p, y = x->p; if((x->l = t->r)) t->r->p = x; t->r = x, x->p = t; update(x), update(t); if((t->p = y)) { if(y->l == x) y->l = t; if(y->r == x) y->r = t; } } void rotl(NP t) { NP x = t->p, y = x->p; if((x->r = t->l)) t->l->p = x; t->l = x, x->p = t; update(x), update(t); if((t->p = y)) { if(y->l == x) y->l = t; if(y->r == x) y->r = t; } } public: SlowLinkCutTree() : e{E< T >()} {} const E< T > &get_sum(NP t) { return t ? t->sum : e; } void update(NP t) { t->sum.update(get_sum(t->l), t->info, get_sum(t->r)); } void splay(NP t) { while(not t->is_root()) { NP q = t->p; if(q->is_root()) { if(q->l == t) rotr(t); else rotl(t); } else { NP r = q->p; if(r->l == q) { if(q->l == t) rotr(q), rotr(t); else rotl(t), rotr(t); } else { if(q->r == t) rotl(q), rotl(t); else rotr(t), rotl(t); } } } } NP expose(NP t) { NP rp = nullptr; for(NP cur = t; cur; cur = cur->p) { splay(cur); if(cur->r) cur->sum.add(cur->r->sum); cur->r = rp; if(cur->r) cur->sum.erase(cur->r->sum); update(cur); rp = cur; } splay(t); return rp; } void link(NP child, NP parent) { expose(parent); expose(child); child->p = parent; parent->r = child; update(parent); } void cut(NP child) { expose(child); NP parent = child->l; child->l = nullptr; parent->p = nullptr; update(child); } NP alloc(const T &info) { NP t = new Node(info); update(t); return t; } bool is_connected(NP u, NP v) { expose(u), expose(v); return u == v or u->p; } vector< NP > build(vector< T > &vs) { vector< NP > nodes(vs.size()); for(int i = 0; i < (int) vs.size(); i++) { nodes[i] = alloc(vs[i]); } return nodes; } NP lca(NP u, NP v) { if(not is_connected(u, v)) return nullptr; expose(u); return expose(v); } void set_key(NP t, const T &v) { expose(t); t->info = move(v); update(t); } const E< T > &query(NP u) { expose(u); return get_sum(u); } E< T > query_subtree(NP u) { expose(u); NP l = u->l; u->l = nullptr; update(u); auto ret = u->sum; u->l = l; update(u); return ret; } }; using T = int; using pi = pair< T, T >; // Heavy-edge の情報 struct Key { T v, idx; Key(T idx, T v) : v(v), idx(idx) {} }; template< typename Key > struct Sum { T p_ans, p_light, p_left; T c_ans, c_light, c_left; set< pi > que; // 単位元(キーの値はアクセスしないので未初期化でもよい Sum() : p_ans(-1), p_light(-1), p_left(-1), c_ans(-1), c_light(-1), c_left(-1) { que.emplace(-1, -1); } void update(const Sum &p, const Key &t, const Sum &c) { p_left = ~p.p_left ? p.p_left : t.v; if(p.p_light < t.v) { if(que.rbegin()->first < c.p_left) { p_light = c.p_light; p_ans = c.p_ans; } else if(~que.rbegin()->first) { p_light = ~c.p_left ? inf : que.rbegin()->first; p_ans = que.rbegin()->second; } else { p_light = -1; p_ans = t.idx; } } else { p_light = inf; p_ans = p.p_ans; } c_left = ~c.c_left ? c.c_left : t.v; if(c.c_light < t.v) { if(que.rbegin()->first < p.c_left) { c_light = p.c_light; c_ans = p.c_ans; } else if(~que.rbegin()->first) { c_light = ~p.c_left ? inf : que.rbegin()->first; c_ans = que.rbegin()->second; } else { c_light = -1; c_ans = t.idx; } } else { c_light = inf; c_ans = c.c_ans; } } void add(const Sum &ch) { que.emplace(ch.p_left, ch.p_ans); } void erase(const Sum &ch) { que.erase(make_pair(ch.p_left, ch.p_ans)); } }; using LCT = SlowLinkCutTree< Key, Sum >; /** * @brief Scanner(高速入力) */ struct Scanner { public: explicit Scanner(FILE *fp) : fp(fp) {} template< typename T, typename... E > void read(T &t, E &... e) { read_single(t); read(e...); } private: static constexpr size_t line_size = 1 << 16; static constexpr size_t int_digits = 20; char line[line_size + 1] = {}; FILE *fp = nullptr; char *st = line; char *ed = line; void read() {} static inline bool is_space(char c) { return c <= ' '; } void reread() { ptrdiff_t len = ed - st; memmove(line, st, len); char *tmp = line + len; ed = tmp + fread(tmp, 1, line_size - len, fp); *ed = 0; st = line; } void skip_space() { while(true) { if(st == ed) reread(); while(*st && is_space(*st)) ++st; if(st != ed) return; } } template< typename T, enable_if_t< is_integral< T >::value, int > = 0 > void read_single(T &s) { skip_space(); if(st + int_digits >= ed) reread(); bool neg = false; if(is_signed< T >::value && *st == '-') { neg = true; ++st; } typename make_unsigned< T >::type y = *st++ - '0'; while(*st >= '0') { y = 10 * y + *st++ - '0'; } s = (neg ? -y : y); } template< typename T, enable_if_t< is_same< T, string >::value, int > = 0 > void read_single(T &s) { s = ""; skip_space(); while(true) { char *base = st; while(*st && !is_space(*st)) ++st; s += string(base, st); if(st != ed) return; reread(); } } template< typename T > void read_single(vector< T > &s) { for(auto &d: s) read(d); } }; /** * @brief Printer(高速出力) */ struct Printer { public: explicit Printer(FILE *fp) : fp(fp) {} ~Printer() { flush(); } template< bool f = false, typename T, typename... E > void write(const T &t, const E &... e) { if(f) write_single(' '); write_single(t); write< true >(e...); } template< typename... T > void writeln(const T &...t) { write(t...); write_single('\n'); } void flush() { fwrite(line, 1, st - line, fp); st = line; } private: FILE *fp = nullptr; static constexpr size_t line_size = 1 << 16; static constexpr size_t int_digits = 20; char line[line_size + 1] = {}; char small[32] = {}; char *st = line; template< bool f = false > void write() {} void write_single(const char &t) { if(st + 1 >= line + line_size) flush(); *st++ = t; } template< typename T, enable_if_t< is_integral< T >::value, int > = 0 > void write_single(T s) { if(st + int_digits >= line + line_size) flush(); if(s == 0) { write_single('0'); return; } if(s < 0) { write_single('-'); s = -s; } char *mp = small + sizeof(small); typename make_unsigned< T >::type y = s; size_t len = 0; while(y > 0) { *--mp = y % 10 + '0'; y /= 10; ++len; } memmove(st, mp, len); st += len; } void write_single(const string &s) { for(auto &c: s) write_single(c); } void write_single(const char *s) { while(*s != 0) write_single(*s++); } template< typename T > void write_single(const vector< T > &s) { for(size_t i = 0; i < s.size(); i++) { if(i) write_single(' '); write_single(s[i]); } } }; int main() { Scanner in(stdin); Printer out(stdout); int N; in.read(N); vector< int > A(N); LCT lct; vector< LCT::NP > vs(N); for(int i = 0; i < N; i++) { A[i] = i; vs[i] = lct.alloc(Key(i + 1, A[i])); } vector< vector< int > > g(N); for(int i = 1; i < N; i++) { int a, b; in.read(a, b); --a, --b; g[a].emplace_back(b); g[b].emplace_back(a); } MFP([&](auto dfs, int idx, int par) -> void { for(auto to: g[idx]) { if(to != par) { lct.link(vs[to], vs[idx]); dfs(to, idx); } } })(0, -1); int Q; in.read(Q); int x = 0; while(Q--) { int u, v; in.read(u, v); --u, --v; (u += x) %= N; (v += x) %= N; swap(A[u], A[v]); lct.set_key(vs[v], Key(v + 1, A[v])); lct.set_key(vs[u], Key(u + 1, A[u])); out.writeln(x = lct.query(vs[u]).c_ans); } }