#P7500. Metro Passenger Flow

    ID: 20695 Type: Default 1000ms 256MiB

Metro Passenger Flow

Metro Passenger Flow

In Tianqiong City, the metro system consists of MM lines and NN stations. Every station is served by at least one line, and each metro line has a list of distinct stations. Two adjacent stations in a line are connected by an undirected edge. Note that lines may intersect at a station, so a station can be served by multiple lines.

There are PP passengers. The jj-th passenger wants to travel from station sjs_j to station tjt_j (it is guaranteed that sjtjs_j \neq t_j). If the two stations lie on the same metro line, the passenger does not need to transfer. Otherwise, they take several transfers. Regardless of the chosen path, the passenger flow contributed by one journey is determined by the minimum number of transfers.

The passenger flow is defined as follows:

[ \text{Passenger Flow} = \text{Entry Flow} + \text{Transfer Flow} ]

Here, the entry flow is 11 (i.e. the passenger count) and the transfer flow is the number of transfers. In other words, if the minimum number of transfers from ss to tt is trans\mathrm{trans}, then the journey uses (trans+1)(\mathrm{trans}+1) metro lines and contributes a passenger flow of (trans+1)(\mathrm{trans}+1).

Note that if there is no route from ss to tt, the passenger will take a bus and the passenger flow is 00.

Your task is to compute the passenger flow contributed by each passenger according to the rules above.

It is guaranteed that every station is served by at least one line.

inputFormat

The input is given in the following format:

N M P
k1 s1,1 s1,2 ... s1,k1
k2 s2,1 s2,2 ... s2,k2
... 
kM sM,1 sM,2 ... sM,kM
s1 t1
s2 t2
...
 sP tP

Where:

  • $N$ ($1 \leq N$) is the total number of stations.
  • $M$ ($1 \leq M$) is the number of metro lines.
  • $P$ ($1 \leq P$) is the number of passengers.
  • For each $i$ ($1 \leq i \leq M$), $k_i$ denotes the number of stations on metro line $i$, followed by $k_i$ distinct station numbers.
  • Each of the next $P$ lines contains two integers $s_j$ and $t_j$, the start and destination stations for the $j$-th passenger.

outputFormat

Output $P$ lines. The $j$-th line should contain a single integer — the passenger flow for the $j$-th passenger. If a passenger is unable to travel from the start to the destination station by metro, output 0 for that passenger.

sample

4 3 1
2 1 2
2 2 3
2 3 4
1 4
3

</p>