#P11693. [JRKSJ ExR] 构造字符串使得

[JRKSJ ExR] 构造字符串使得

Problem Description

You are given an undirected graph with nn vertices and mm edges. A game piece initially is at vertex xx. This is a two-player game: the first player and the second player take turns moving the piece. Each move, the player may move the piece to any vertex that is directly connected by an edge to the vertex where the piece is currently located. It is guaranteed that every vertex is connected to at least one edge.

There is a variable vv with initial value 00. After each move, let tt be the index of the vertex where the piece is currently located, and then set vv to max(v,t)\max(v,t). In other words, vv is the maximum vertex index that the piece has moved to, and the initial position xx is not counted at the beginning.

The two players will move the piece a total of kk times. The first player wants the final value of vv to be as large as possible, while the second player wants the final value of vv to be as small as possible.

There are qq queries. Each query gives x,kx,k and asks: if a game of exactly kk moves starts from xx, and both players use optimal strategies, what will the final value of vv be?

Input Format

The first line contains three integers n,m,qn,m,q.

The next mm lines each contain two integers describing an edge in the graph.

The next qq lines each contain two integers x,kx,k.

Output Format

Output qq lines. Each line contains one integer, the final value of vv in that game.

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

Hint

Sample Explanation

The graph in the sample is shown above.

For the first query, clearly the first player cannot force the second player to move to vertex 55, so the answer is 4\le 4. The first player can achieve this by moving to vertex 44 on the first move.

For the second query, the largest-index vertex that the first player can reach in one move is 33, so the answer is 33.

Constraints

This problem uses bundled testdata.

Subtask\text{Subtask} nn\le qq\le Special Property Score
11 55 77
22 8080 1414
33 700700 1717
44 2×1052\times 10^5 5050 2020
55 2×1052\times 10^5 55
66 3737

Special property: the graph is guaranteed to be a single chain (a path).

For all testdata, it is guaranteed that 2n2×1052\le n\le 2\times 10^5, 1m5×1051\le m\le 5\times 10^5, 1q2×1051\le q\le 2\times 10^5, and 1x,kn1\le x,k\le n.

It is guaranteed that the given graph has no multiple edges and no self-loops. It is also guaranteed that for any vertex uu, there exists at least one vertex vv such that there is an edge between uu and vv.

Translated by ChatGPT 5