Under construction

Still being built. Things may change

Doomsday Protocol

Time limit2sMemory limit1024 MB

Summary
For each start station and tunnel budget, find the station nearest the shop that the agents can reach using at most k tunnels.
Level

Medium7 of 10

Topics
Tree, Graph, DFS, Binary search
Solved
No attempts yet

Statement

Under the Doomsday Protocol, the surviving Kingsman agents must reach a certain wine shop. They may use the London Underground and the secret Kingsman tunnels. The Underground network is a tree, where vertices are stations and edges are the tracks between them. Stations are numbered from 11 to nn, and the wine shop is at station 11. All tracks have the same length. Agents may be watched, and moving on the Underground can reveal their location, so they must travel on the Underground only away from the shop. In other words, they can ride from a station to an adjacent one only if the edge distance from the shop to the first station is less than the distance from the shop to the second. Each Kingsman tunnel connects two stations such that one can be reached from the other by moving on the Underground only away from the shop.

Merlin is working through scenarios for activating the Doomsday Protocol. Each scenario is defined by the station where the surviving agents start and the maximum number of Kingsman tunnels they may use. On the Underground they may travel any number of times, but only in the direction away from the shop. For each scenario, Merlin needs the station closest to the shop among those the agents can reach, since they must walk the rest of the way.

Input

The first line contains one integer nn, the number of stations (2≤n≤200 0002 \le n \le 200\,000). Each of the next n−1n - 1 lines contains two integers aia_i and bib_i, the stations connected by an Underground track (1≤ai,bi≤n1 \le a_i, b_i \le n).

The next line contains one integer mm, the total number of Kingsman tunnels (0≤m≤200 0000 \le m \le 200\,000). Each of the next mm lines contains two integers uiu_i and viv_i, the stations connected by a tunnel (1≤ui,vi≤n1 \le u_i, v_i \le n, ui≠viu_i \neq v_i). It is guaranteed that one can reach uiu_i from viv_i by moving only away from the shop.

The next line contains one integer qq, the number of scenarios (0≤q≤200 0000 \le q \le 200\,000). Each of the next qq lines contains two integers sis_i and kik_i, the station where the agents start and the number of Kingsman tunnels they may use (1≤si≤n1 \le s_i \le n, 0≤ki≤m0 \le k_i \le m).

Output

For each scenario, print one integer on its own line: the number of the station closest to the shop among those the agents can reach.

Examples1

  1. Example 1

    Input
    5
    1 2
    2 3
    2 4
    1 5
    2
    3 1
    4 2
    4
    2 0
    4 1
    4 2
    5 2
    
    Expected output
    2
    2
    1
    5