Under construction

Still being built. Things may change

Recursive circuit

Time limit1sMemory limit256 MB

Summary
Given a self-similar circuit graph, find for each query the smallest critical nesting depth needed to connect two input contacts, or -1 if they cannot be connected.
Level

Medium7 of 10

Topics
Graph, Union-find, Sorting
Solved
No attempts yet

Problem

In the layout of a recursive microcircuit, there are NN contacts in total, and some pairs of contacts are connected directly by conductive paths. The circuit also has SS sub-circuits, each an exact copy of this circuit.

The layout has three types of contacts:

  1. Input contacts of the circuit (KK contacts). These are the only contacts that connect the circuit to external conductive paths.
  2. Input contacts of the nested sub-circuits (S⋅KS \cdot K contacts).
  3. Auxiliary contacts.

All these contacts can be connected with each other by conductive paths without any restrictions.

Signals travel along conductive paths. When a signal reaches a contact, it can follow any path connected to that contact. If an external signal reaches an input contact of a sub-circuit, it can enter the sub-circuit and travel further along its paths. If an internal signal reaches an input contact of a sub-circuit, it can leave the sub-circuit and travel outside the circuit (if there are paths outside, and if the external circuit is itself a sub-circuit of another circuit).

Consider the outermost circuit, with nothing outside it. Determine whether two specified input contacts of the circuit are connected by paths. Two contacts are connected if a signal can reach one from the other, possibly entering various sub-circuits a finite number of times.

Besides connectivity, find how deep the signal must go into the sub-circuits to reach one contact from the other. The outermost circuit has nesting depth 0. Its sub-circuits have depth 1, their sub-circuits have depth 2, and so on. For any signal path, the critical depth is the maximum nesting level among all circuits whose conductive paths the signal travels. Find the minimal critical depth over the paths between two given input contacts of the outermost circuit.

Input

The first line contains four integers: NN (the number of contacts in the layout), KK (the number of input contacts of the circuit), SS (the number of sub-circuits), and MM (the number of conductive paths) (1≤K≤1000001 \le K \le 100000, 0≤S≤10000 \le S \le 1000, K(S+1)≤N≤100000K (S + 1) \le N \le 100000, 0≤M≤1000000 \le M \le 100000).

Each of the following MM lines defines a conductive path by two integers aa and bb, the numbers of the contacts it connects directly (1≤a≠b≤N1 \le a \neq b \le N).

Contacts are numbered from 11 to NN. Input contacts of the circuit are numbered from 11 to KK. Input contacts of the tt-th sub-circuit are numbered from tK+1tK + 1 to tK+KtK + K (for 1≤t≤S1 \le t \le S). The jj-th input contact of the tt-th sub-circuit is the (tK+j)(tK + j)-th contact of the external circuit layout. The remaining contacts, if any, are auxiliary.

The next line contains QQ, the number of queries (1≤Q≤1000001 \le Q \le 100000). Each of the following QQ lines contains two integers uu and vv, the numbers of two input contacts of the outermost circuit (1≤u≠v≤K1 \le u \neq v \le K).

Output

Print QQ integers, one per line. The rr-th number is the nesting depth needed to get from one of the two input contacts of the rr-th query to the other. If the other input contact cannot be reached, print −1-1 instead.

Hint

Examples1

  1. Example 1

    Input
    16 5 2 7
    1 16
    2 16
    6 16
    7 11
    3 12
    4 13
    5 15
    4
    1 2
    2 3
    3 4
    4 5
    
    Expected output
    0
    1
    2
    -1