Acquapia

Time limit1sMemory limit128 MB

Summary
Multiple test cases give several river trees; for each city pair, report whether a route exists and, if so, the unique city where the ship must switch from upstream to downstream.
Level

Medium7 of 10

Topics
Tree, DFS, Implementation, Graph
Solved
No attempts yet

Problem

Acquapia is a small country crossed by several navigable rivers. Every river has its source in the mountains inside Acquapia and eventually flows into the sea; a river never flows into another river. Along its course (but not at its source) a river may split into two or more streams. There is a city at every point where a river begins, splits, or ends, and each city touches at most one river.

Rivers are the main means of transportation in Acquapia. Because the country is at war, ships cannot cross the sea and may travel only along rivers. To move between two cities a ship travels upstream (against the flow) up to some city, performs a stream change there, and then travels downstream (with the flow) to the destination. A stream change — switching from upstream to downstream navigation — is difficult and dangerous, so it should be avoided when possible; when it is unavoidable, it happens at exactly one city on the route. Travelling purely upstream or purely downstream needs no stream change.

Each river forms a tree rooted at its source with edges oriented downstream, so the route between any two connected cities is unique; hence the stream-change city, if any, is uniquely determined.

Given the rivers, the cities, and a list of queries, each a pair of cities XX and YY, answer for every query:

  • Is it possible to navigate from city XX to city YY?
  • If it is, does the ship need a stream change, and if so, at which city?

Input

The input contains several test cases. The first line of each test case has four integers CC, RR, SS and QQ separated by single spaces: the number of cities (2≤C≤1032 \le C \le 10^3), the number of rivers (1≤R≤C/21 \le R \le C/2), the number of river sections (1≤S≤C−11 \le S \le C-1) and the number of queries (1≤Q≤2×1051 \le Q \le 2 \times 10^5). Cities are numbered from 11 to CC.

The second line contains RR distinct integers, the cities that are river sources. Each of the next SS lines contains two integers XX and YY (X≠YX \ne Y), meaning there is a river section flowing from city XX down to city YY. Each of the following QQ lines contains two integers AA and BB (A≠BA \ne B), a query.

The end of the input is the line C=R=S=Q=0C = R = S = Q = 0, which must not be processed.

Output

For each test case, print QQ lines; the ii-th line is the answer to the ii-th query, in input order. Print a single empty line between the outputs of two consecutive test cases.

For a query (A,B)(A, B) print:

  • −1-1 if it is impossible to navigate from AA to BB;
  • 00 if navigation is possible without any stream change;
  • otherwise, the number of the city where the stream change must be made.

Examples1

  1. Example 1

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