PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SRD0000102. Bữa tiệc của Bitaro (Bitaro's Party)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Bữa tiệc của Bitaro (Bitaro's Party)

Bitaro's Party

Source: JOI

Version: Phuoc Hung OJ Extended

Problem Statement

There are NN towns numbered 11 through NN in descending order of altitude. There are MM directed canals; a canal Si→EiS_i\to E_i flows from a higher town to a lower town, so Si<EiS_i<E_i. Travel is possible only along the direction of a canal. No directed canal occurs twice.

Bitaro holds QQ parties. Party jj takes place in town TjT_j. Exactly YjY_j friends are busy, living in towns Cj,1,…,Cj,YjC_{j,1},\ldots,C_{j,Y_j}. There is one friend in each town. A busy friend cannot be a starting attendee, but another friend's route may pass through that friend's town.

Every non-busy friend who can reach TjT_j using canals attends. Each such friend chooses a path with the largest possible number of canals to TjT_j (a friend already at TjT_j may use zero canals). Report the maximum number of canals used by any attendee, or -1 if nobody can attend.

Input

  • The first line contains N,M,QN,M,Q.
  • Each of the next MM lines contains Si,EiS_i,E_i, a directed canal from SiS_i to EiE_i.
  • Each of the next QQ lines starts with Tj,YjT_j,Y_j, followed by exactly YjY_j town indices Cj,1,…,Cj,YjC_{j,1},\ldots,C_{j,Y_j} in strictly increasing order. If Yj=0Y_j=0, no additional indices appear on that line.

Output

For each party, output the largest number of canals used by an attendee, or -1 if there are no attendees. Print one integer per line.

Subtasks

  • Subtask 1 (7 points): 1≤N≤10001\le N\le1000, 0≤M≤20000\le M\le2000, Q=1Q=1; 1≤Si<Ei≤N1\le S_i<E_i\le N with no repeated edges; 1≤Tj≤N1\le T_j\le N, 0≤Yj≤N0\le Y_j\le N, strictly increasing Cj,k∈[1,N]C_{j,k}\in[1,N]; ∑Yj≤105\sum Y_j\le10^5.
  • Subtask 2 (7 points): 1≤N≤1051\le N\le10^5, 0≤M≤2⋅1050\le M\le2\cdot10^5, Q=1Q=1; 1≤Si<Ei≤N1\le S_i<E_i\le N with no repeated edges; 1≤Tj≤N1\le T_j\le N, 0≤Yj≤N0\le Y_j\le N, strictly increasing Cj,k∈[1,N]C_{j,k}\in[1,N]; ∑Yj≤105\sum Y_j\le10^5.
  • Subtask 3 (86 points): 1≤N≤1051\le N\le10^5, 0≤M≤2⋅1050\le M\le2\cdot10^5, 1≤Q≤1051\le Q\le10^5; 1≤Si<Ei≤N1\le S_i<E_i\le N with no repeated edges; 1≤Tj≤N1\le T_j\le N, 0≤Yj≤N0\le Y_j\le N, strictly increasing Cj,k∈[1,N]C_{j,k}\in[1,N]; ∑j=1QYj≤105\sum_{j=1}^{Q}Y_j\le10^5.

Examples

Input

5 6 3
1 2
2 4
3 4
1 3
3 5
4 5
4 1 1
5 2 2 3
2 3 1 4 5

Output

1
3
0

Explanation

For the party at town 4, the friend from town 1 is busy. The longest valid arrival path, from town 2 or 3, uses one canal. For the party at town 5, the friends from towns 2 and 3 are busy, while the friend from town 1 can follow 1→2→4→51\to2\to4\to5 (three canals). For the party at town 2, towns 1, 4 and 5 are banned; only the friend living at town 2 can attend, using zero canals.