Under construction

Still being built. Things may change

Choosing a meeting place (Large)

Interview

Time limit5sMemory limit512 MB

Summary
Each friend moves at their own speed, so run Dijkstra once per friend and pick the city with the smallest worst arrival time.
Level

Medium5 of 10

Topics
Shortest path, Graph
Solved
No attempts yet

Problem

Friends who live in different cities want to gather in one place. The roads are tangled and the friends live far apart, so it is hard to tell how much time to set aside. Find the smallest time it takes for all of them to meet in one city.

The map holds cities and roads. A road here is not a single link between two cities. It is a chain of consecutive links that runs through several cities in order.

Each test case gives the following.

  • NN: the number of cities
  • PP: the number of friends
  • MM: the number of roads

Cities are numbered 11 through NN.

For each friend ii, numbered 11 through PP, the input gives the following.

  • XiX_i: the number of the city the friend starts from
  • ViV_i: the time the friend takes to move a distance of 11

For each road jj, the input gives the following.

  • DjD_j: the distance between neighbouring cities on that road. On one road every pair of neighbouring cities is the same distance DjD_j apart.
  • LjL_j: the number of cities the road runs through
  • Cj,1,Cj,2,…,Cj,LjC_{j,1}, C_{j,2}, \dots, C_{j,L_j}: the numbers of those cities, in order

All friends leave at the same moment. Find the smallest time needed for every friend to gather in one city, and print -1 instead when no city can hold all of them.

A meeting happens only in a city, and a friend who arrives early can wait for the others.

Across all roads, a direct link between the same two cities appears at most once. On reaching a city, a friend can change between the roads that run through that city with no extra time.

Input

The first line holds the number of test cases TT. Each test case has the following form.

N P M
X_1 V_1
X_2 V_2
...
X_P V_P
D_1 L_1 C_{1,1} C_{1,2} ... C_{1,L_1}
D_2 L_2 C_{2,1} C_{2,2} ... C_{2,L_2}
...
D_M L_M C_{M,1} C_{M,2} ... C_{M,L_M}

Constraints

  • 1≤T≤301 \le T \le 30
  • 1≤N≤100001 \le N \le 10000
  • 2≤P≤1002 \le P \le 100
  • 1≤M≤10001 \le M \le 1000
  • 1≤Xi≤N1 \le X_i \le N
  • 1≤Vi≤2001 \le V_i \le 200
  • 1≤Dj≤2001 \le D_j \le 200
  • 2≤Lj≤min⁡(N,150)2 \le L_j \le \min(N, 150)
  • The answer for each test case is at most 21474836472147483647.

Output

For each test case print one line in the form Case #x: y, where xx is the test case number starting from 11 and yy is the answer for that case. When the friends cannot all meet in one city, print -1 in place of yy.

Hint

The picture shows the second test case of the sample input. A square is a city, a smiling face is a friend, the solid line is road 1 with distance 11, and the dashed line is road 2 with distance 22.

Examples1

  1. Example 1

    Input
    3
    2 2 1
    1 1
    2 2
    1 2 1 2
    5 2 2
    1 1
    5 100
    1 3 1 2 3
    2 3 4 2 5
    5 2 2
    1 1
    5 5
    1 2 1 2
    1 3 3 4 5
    
    Expected output
    Case #1: 1
    Case #2: 3
    Case #3: -1