Recursive circuit

아직 제출이 없습니다시간 제한1초메모리 제한256 MB

문제

In the layout of a recursive microcircuit, there is a total of NN contact points, with some pairs of contacts connected directly with conductive paths. In addition, there is a total of SS sub-circuits within the circuit, each being an exact copy of the circuit in question.

There are three types of contacts in the circuit layout:

  1. Input contacts of the circuit (KK contacts). These are the only contacts connecting the circuit to external conductive paths.
  2. Input contacts of the nested sub-circuits (SKS \cdot K contacts).
  3. Auxillary 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 of the paths connected to this contact. If an external signal reaches an input contact of the sub-circuit, it can enter the sub-circuit and travel further along its paths. If an internal signal reaches an input contact of the 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 most external circuit, with nothing else on the outside. Determine whether two specified input contacts of the circuit are connected with paths. The contacts are connected if a signal can reach one contact from the other, possibly entering a number of various sub-circuits a finite number of times.

Apart from the fact of being connected, find out how deep the signal must go into the sub-circuits to reach one contact from the other. The external circuit has a nesting depth of 0; for its sub-circuits, the nesting depth is 1, and their sub-circuits, in turn, have a nesting depth of 2, etc. For an arbitrary path of the signal, the critical depth can be defined, which is the maximum nesting level among all circuits, whose conductive paths are travelled by the signal.

Determine the minimal value of the critical depth for the path between two given input contacts of the external circuit.

입력

The first line contains four integers: NN --- the number of contacts in the circuit layout, KK --- the number of input contacts of the circuit, SS --- the number of sub-circuits in the circuit, MM --- the number of conductive paths in the circuit layout (1K100,0001 \le K \le 100\\,000, 0S1,0000 \le S \le 1\\,000, K(S+1)N100,000K (S + 1) \le N \le 100\\,000, 0M100,0000 \le M \le 100\\,000).

The following MM lines define the conductive paths in the  circuit layout. Each path is defined by two integers aa and bb --- the numbers of contacts in the layout directly connected by the path (1abN1 \le a \neq b \le N).

The contacts in the circuit layout are numbered in numerical order from 11 to NN. The input contacts are numbered from 11 to KK. The input contacts of the ttth sub-circuit are numbered from tK+1t K + 1 to tK+Kt K + K (for 1tS1 \le t \le S). The jjth input contact on the ttth circuit layout is the (tK+j)(t K + j)th contact on the external circuit layout. The remaining contacts, if such exist, are auxillary.

The following line contains an integer QQ --- the number of queries (1Q100,0001 \le Q \le 100\\,000). Each of the remaining QQ lines contain one query that needs an answer. Each query is defined by two integers uu and vv --- the numbers of the input contacts of the external circuit (1uvK1 \le u \neq v \le K).

출력

In the output file, print QQ integers, one number per line. Every rrth number must be an answer to the rrth query: the nesting depth necessary to get from one of the input contacts to another. If there is no way to reach the other input contact, print the number 1-1 instead of the depth value.

힌트