QOJ.ac

QOJ

حد الوقت: 10 s حد الذاكرة: 1024 MB مجموع النقاط: 100

#18825. Tree and Query 22

الإحصائيات

There is a tree consisting of $N$ vertices. The vertices are numbered from $1$ to $N$. Vertex $i$ has a number $a_i$ written on it. Initially, $a_i = i$.

Define $f(u, v)$ as the sequence formed by the numbers $a_{p_0}, a_{p_1}, \ldots, a_{p_t}$ on the path $p_0 = u, p_1, p_2, \ldots ,p_t = v$ from $u$ to $v$, in order.

You must process the following queries:

  • u v: Swap $a_u$ and $a_v$. Then, output $w$ that maximizes $f(u, w)$. Sequences are compared lexicographically.

Note that the queries are given online. See the Input section for details.

Input

The first line contains the size of the tree $N$. ($2 \le N \le 200{,}000$)

The next $N-1$ lines contain two integers $u, v$, meaning there is an edge connecting vertices $u$ and $v$. ($1 \le u, v \le N$)

The next line contains the number of queries $Q$. ($1 \le Q \le 200{,}000$)

The next $Q$ lines contain two integers $x, y$. ($1 \le x, y \le n$, $x \ne y$)

To obtain $u, v$ from the integers $x, y$, proceed as follows. Let $last$ be the answer to the previous query (if it is the first query, $last = 0$). Then $(u, v) = (((x + N - 1 + last) \text{ mod } N) + 1, ((y + N - 1 + last) \text{ mod } N) + 1)$ holds.

Output

Print the answers to the queries over $Q$ lines.

Examples

Input 1

3
1 2
2 3
4
3 1
3 2
2 1
1 3

Output 1

1
3
3
3

Input 2

10
9 8
10 3
2 3
10 9
1 10
6 4
4 1
1 5
6 7
15
8 10
2 8
9 8
8 2
9 1
2 8
1 4
4 1
6 2
6 9
7 8
4 2
4 3
10 2
1 9

Output 2

2
7
5
8
5
5
5
8
7
8
2
7
5
7
5

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.