Celebration

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

문제

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, which is denoted by xyx \Rightarrow y. The roads in the country satisfy the following property: for three cities x,y,zx,y,z, if xzx \Rightarrow z and yzy \Rightarrow z, then xyx \Rightarrow y or yxy \Rightarrow x.

Now there will be qq parades throughout the celebration. The ii-th parade starts in city s_is\_i and ends in city t_it\_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 will build kk (0k2)(0 \le k \le 2) one-way roads for the exclusive use of that parade, and other parades may not go through the roads built for the specific parade.

Now we want to know the number of cities that COULD BE visited during a parade. Notice 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.

입력

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

In the following mm lines, each line contains two integers u,vu,v denoting there is a one-way road uvu \rightarrow v.

In the next qq lines, each line contains two integers s_i,t_is\_i,t\_i denoting the origin and the destination of the parade. Then on the same line, there will be kk pairs of integers a,ba,b denoting there exists a temporary one-way road aba \rightarrow b for the exclusive use of the parade.

It is guaranteed if we treat the one-way roads originally in the country as bidirectional, the cities are mutually reachable.

출력

For each query, output an integer in a line denoting the answer. If it is not possible to reach the destination from the origin, output 0.

제한

For all test cases, 1n,q3×1051 \le n, q \le 3 \times 10^5n1m6×105n-1 \le m \le 6 \times 10^50k20 \le k \le 2.

힌트

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

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

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

In parade 4, the origin is city 3 and the destination is city 4. The temporary road is 515 \rightarrow 1. We cannot reach city 4 from city 3.