Under construction

Still being built. Things may change

Postcards

Time limit8sMemory limit256 MB

Summary
For each shutdown plan, count the cities that can reach every other city when some roads are closed in one direction, with each plan decoded from earlier answers.
Level

Hard9 of 10

Topics
Graph, Divide and conquer, Union-find, Topological sort
Solved
No attempts yet

Problem

Bithuania has nn cities connected with mm bidirectional roads. Currently, there exists a path between every pair of cities. Each city has one child living in it. Every child would like to send a Christmas postcard to every other child. Unfortunately, the ongoing Christmas period means some roads are being renovated. Because of this, postcards can be sent in only one direction on some roads, and cannot be sent along others at all.

The government of Bithuania has drawn up several plans for shutting down roads. For each plan, in order, find the number of children who can receive a postcard from every other child.

Input

The first line contains three integers nn, mm, qq (2≤n≤30002 \le n \le 3000, 1≤m≤500 0001 \le m \le 500\,000, 1≤q≤500 0001 \le q \le 500\,000). The next mm lines describe the road network: the ii-th line contains two integers uiu_i, viv_i (1≤ui,vi≤n1 \le u_i, v_i \le n, ui≠viu_i \neq v_i), meaning a road between cities uiu_i and viv_i. Roads are numbered 11 through mm in input order. A pair of cities may be connected by more than one road.

Then come qq shutdown plans, numbered 11 through qq. The ii-th plan starts with a line containing an integer rir_i (1≤ri≤1001 \le r_i \le 100), the number of renovated roads. Then rir_i lines follow, each describing one shut down road with three numbers xx, puvp_{uv}, pvup_{vu} (0≤x<m0 \le x < m, puv,pvu∈{0,1}p_{uv}, p_{vu} \in \{0, 1\}, puv+pvu≥1p_{uv} + p_{vu} \ge 1).

Let SiS_i be the sum of the answers to plans 11 through i−1i-1. The ii-th plan refers to road j=((x+Si) mod m)+1j = ((x + S_i) \bmod m) + 1. If puv=1p_{uv} = 1, road jj cannot be used to travel from city uju_j to city vjv_j. If pvu=1p_{vu} = 1, road jj cannot be used to travel from city vjv_j to city uju_j.

For each plan, the values of xx are distinct. The sum of all rir_i does not exceed 500 000500\,000. Because each plan depends on earlier answers, the plans must be processed online.

Output

For every shutdown plan, print one line with the number of children who can receive a postcard from every other child.

Hint

In the first plan, road 4 (connecting cities 1 and 3) is closed completely and road 2 is partially closed (travel from 3 to 4 is not possible). Children in cities 1, 2, and 3 can receive postcards from all other children.

In the second plan, S2=3S_2 = 3. Roads 1, 4, and 3 are partially blocked: travel from 1 to 2, from 1 to 3, and from 2 to 3 is disallowed, respectively. Only the child in city 1 can receive a postcard from all other children.

In the last plan, S3=4S_3 = 4. Road 2 is blocked. No child can receive a postcard from all other children.

Examples1

  1. Example 1

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