Under construction

Still being built. Things may change

Finding the Ship

Time limit3sMemory limit1024 MB

Summary
Given a graph and queries (v, k), find the single point the ship must occupy after k steps on a shortest path from s to v, or report impossible or ambiguous.
Level

Medium7 of 10

Topics
BFS, Graph, Shortest path, Tree
Solved
No attempts yet

Problem

The Guardians of the Galaxy received a distress signal from a ship that Thanos destroyed. They must find the ship as soon as possible and rescue Thor, who is on board.

The ship moves in an unusual way. There are nn distinct points in the universe, numbered from 1 to nn, and the ship moves between them. There are also mm wormholes, where the ii-th wormhole connects points aia_i and bib_i. A wormhole may connect a point to itself, and two points may be connected by more than one wormhole. Traveling through any single wormhole takes exactly one hour.

Initially the ship is at point ss. The Guardians think Thor tried to steer the ship toward one of the points but had to stop along the way. They have qq guesses. In the ii-th guess, Thor started at point ss and followed one of the shortest paths to point viv_i. After passing through wormholes kik_i times, the ship stopped and did not move any further. A shortest path is a path that uses the smallest possible number of wormholes.

Input

The first line contains three integers nn, mm, and ss, the number of points, the number of wormholes, and the starting point (1≤n,m≤500001 \le n, m \le 50000; 1≤s≤n1 \le s \le n).

Each of the next mm lines contains two integers aia_i and bib_i, the points joined by the ii-th wormhole (1≤ai,bi≤n1 \le a_i, b_i \le n).

The next line contains a single integer qq, the number of guesses (1≤q≤500001 \le q \le 50000).

Each of the next qq lines contains two integers viv_i and kik_i, the guessed destination and the number of wormholes Thor passed through (1≤vi≤n1 \le v_i \le n; 1≤ki≤m1 \le k_i \le m).

Output

For each guess, print one line with one of the following results.

  • If the situation in the guess is impossible (for example, there is no path from ss to viv_i), print 00.
  • If the situation is possible and more than one point could hold the ship at that moment, print −1-1.
  • Otherwise, print the number of the single point where the ship must be according to the guess.

Examples1

  1. Example 1

    Input
    7 7 1
    1 2
    1 3
    2 4
    3 4
    1 5
    2 6
    4 7
    5
    4 1
    6 1
    7 1
    7 2
    5 2
    
    Expected output
    -1
    2
    -1
    4
    0