This page is still under construction.

Parts of this page are still being built. What you see may change.

Celebration

Time limit2sMemory limit1024 MB

Summary
For each parade, count the cities that can lie on some path from s to t after adding up to two temporary one-way roads.
Level

Hard8 of 10

Topics
Graph, Topological sort, Tree
Solved
No attempts yet

Problem

rpeng: I raised the TL to 2 seconds to avoid fast I/O nonsense. I was able to get AC (very close though) using cin/cout.

There are nn cities and mm one-directional roads in a country. The cities are numbered 1,2,…,n1, 2, \dots, n. If it is possible to reach city yy via roads from city xx, we say city yy is reachable from city xx, denoted x⇒yx \Rightarrow y. The roads in the country satisfy the following property: for three cities x,y,zx,y,z, if x⇒zx \Rightarrow z and y⇒zy \Rightarrow z, then x⇒yx \Rightarrow y or y⇒xy \Rightarrow x.

There will be qq parades during the celebration. The ii-th parade starts in city sis_i and ends in city tit_i after visiting some other cities. A city may be visited multiple times during a parade. To make the parades more interesting, for each parade we build kk (0≤k≤2)(0 \le k \le 2) one-way roads for the exclusive use of that parade. Other parades may not use the roads built for a specific parade.

Now we want to know the number of cities that could be visited during a parade. Note that roads built for the exclusive use of a parade do not have to satisfy the property satisfied by the roads originally in the country.

Input

The first line contains four integers n,m,q,kn, m, q, k, the number of cities, the number of roads, the number of parades, and the number of roads built for each parade.

The following mm lines each contain two integers u,vu, v, meaning there is a one-way road u→vu \rightarrow v.

The next qq lines each contain two integers si,tis_i, t_i, the origin and the destination of the parade. On the same line, there are kk pairs of integers a,ba, b, meaning there is a temporary one-way road a→ba \rightarrow b for the exclusive use of that parade.

If the original roads are treated as bidirectional, all cities are mutually reachable.

Output

For each parade, output the answer on its own line. The answer is the number of cities that could be visited. If the destination cannot be reached from the origin, output 0.

Constraints

For all test cases, 1≤n,q≤3×1051 \le n, q \le 3 \times 10^5, n−1≤m≤6×105n-1 \le m \le 6 \times 10^5, and 0≤k≤20 \le k \le 2.

Hints

In parade 1, the origin is city 1 and the destination is city 4. The temporary road is 5→15 \rightarrow 1. The cities that may be visited are 1, 2, 4, and 5.

In parade 2, the origin is city 2 and the destination is city 3. The temporary road is 5→35 \rightarrow 3. The cities that may be visited are 2, 3, 4, and 5.

In parade 3, the origin is city 1 and the destination is city 2. The temporary road is 5→25 \rightarrow 2. The cities that may be visited are 1, 2, 4, and 5.

In parade 4, the origin is city 3 and the destination is city 4. The temporary road is 5→15 \rightarrow 1. City 4 cannot be reached from city 3.

Examples1

  1. Example 1

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