10811081 SUSTech Online Judge
Problem 1081 --Minimum Cycle Length

## 1081: Minimum Cycle Length

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 276  Solved: 143
[Submit][Status][Web Board]

## Description

We have k integers from 1 to k. Now give you n sequences. The ith element of the sequence can change to the (i+1)th element (the last one cannot be changed). The element can change from one sequence to another sequence. There are q queries. Each query will give you an integer x. Please find the minimum change steps for changing x to itself.

Note: The change steps should be positive. If an integer cannot change to itself, print -1.

## Input

The first line will be an integer T. (1 <= T <= 50) T is the number of test cases.

For each test case, the first line will be three integers k, n and q. (1 <= k, n <= 200, q <= 10000)

Then there will be n lines. Each line begins with an integer t (1 <= t <= 200). t is the length of this sequence. Then t integers in the line is the elements in this sequence.

Then there is an integer q. q is the number of queries.

For each query, there will be an integer x.

## Output

For each query, print the step problem required.

## Sample Input

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


## Sample Output

2
-1
2
3
2


## HINT

For 1, 1->3 (in sequence 1)
3->1 (in sequence 3) the answer is 2.

For 2, 2 can be changed to 4, and 4 can be
changed to 6. But 6 and 4 cannot change to 2.

[Submit][Status]