QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 256 MB Total points: 100

#15776. Neverland

Statistics

Neverland contains $n$ islands, numbered from $1$ to $n$. Each island has a unique importance value, and based on these importance values, the $n$ islands can be ranked, with ranks represented by integers from $1$ to $n$. Some islands are connected by massive bridges, allowing travel from one island to another. If one can travel from island $a$ to island $b$ by crossing zero or more bridges, we say that island $a$ and island $b$ are connected. There are two types of operations: B x y represents building a new bridge between island $x$ and island $y$. Q x k represents a query asking for the island with the $k$-th smallest importance rank among all islands currently connected to island $x$. Please output the number of that island.

Input

The first line contains two space-separated positive integers $n$ and $m$, representing the number of islands and the number of initially existing bridges, respectively. The next line contains $n$ space-separated integers, describing the importance rank of islands $1$ through $n$ in order. The following $m$ lines each contain two space-separated positive integers $a_i$ and $b_i$, representing an initially existing bridge between island $a_i$ and island $b_i$. The remaining part describes the operations: the first line of this part is a positive integer $q$, representing the total number of operations. The next $q$ lines describe each operation in the format mentioned above, starting with an uppercase letter Q or B, followed by two positive integers not exceeding $n$. The letter and the numbers, as well as the two numbers, are separated by spaces.

For $20\%$ of the data: $n \le 1000, q \le 1000$. For $100\%$ of the data: $n \le 100000, m \le n, q \le 300000$.

Output

For each Q x k operation, output one line containing a single integer representing the number of the queried island. If such an island does not exist, output -1.

Examples

Input 1

5 1
4 3 2 5 1
1 2
7
Q 3 2
Q 2 1
B 2 3
B 1 5
Q 2 1
Q 2 4
Q 2 3

Output 1

-1
2
5
1
2

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.