시간 제한메모리 제한제출정답맞힌 사람정답 비율
5 초 512 MB45121226.667%

문제

Tom is taking metros in the city to go from station to station.

The metro system in the city works like this:

  • There are N metro lines in the city: line 1, line 2, ..., line N.
  • For each metro i, there are SNi stations. Let's assume they are Si,1,Si,2, ... , Si,SNi. These stations are ordered from one end point to the other end point. The metro is running in both directions. In other words, the metro is going from Si,1 -> Si,2 -> ... -> Si,SNi, and Si,SNi -> Si,SNi-1 -> ... -> Si,1. You can take the metro from any station and get off at any station. It takes a certain time to travel from one station to the next station. It takes Timei,1 minutes to travel from Si,1 to Si,2, Timei,2 minutes to travel from Si,2 to Si,3, etc. It takes the same time in the other direction.
  • There are M transfer tunnels. Each transfer tunnel connects two stations of different metro lines. It takes a certain amount of time to travel through a tunnel in either direction. You can get off the metro at one end of the tunnel and walk through the tunnel to the station at the another end.
  • When you arrive at a metro station of line i, you need to wait Wi minutes for the next metro.

Now, you are going to travel from one station to another. Find out the shortest time you need.

입력

The first line of the input gives the number of test cases, T. T test cases follow.

Each test case starts with an integer N, the number of metro lines. N metros descriptions follow. Each metro description starts with two integers SNi and Wi, the number of stations and the expected waiting time in minutes. The next line consists of SNi-1 integers, Timei,1, Timei,2, ..., Timei,SNi-1, describing the travel time between stations.

After the metro descriptions, there is an integer M, the number of tunnels. M lines follow to describe the tunnels. Each tunnel description consists of 5 integers, m1i, s1i, m2i, s2i, ti which means the tunnel is connecting stations Sm1i,s1i and station Sm2i,s2i. The walking time of the tunnel is ti.

The next line contains an integer Q, the number of queries. Each of the next Q lines consists of 4 integers, x1, y1, x2, y2, which mean you are going to travel from station Sx1,y1 to station Sx2,y2.

Limits

  • 1 ≤ T ≤ 100.
  • 1 ≤ Wi ≤ 100.
  • 1 ≤ Timei,j ≤ 100.
  • 1 ≤ m1iN.
  • 1 ≤ s1iSNm1i.
  • 1 ≤ m2iN.
  • 1 ≤ s2iSNm2i.
  • m1i and m2i will be different.
  • 1 ≤ ti ≤ 100.
  • 1 ≤ Q ≤ 10.
  • 1 ≤ x1N.
  • 1 ≤ y1SNx1.
  • 1 ≤ x2N.
  • 1 ≤ y2SNy2.
  • Station Sx1,y1 and station Sx2,y2 will be different.
  • 1 ≤ N ≤ 100.
  • 0 ≤ M ≤ 100.
  • 2 ≤ SNi ≤ 1000.
  • The total number of stations in each case is at most 1000.

출력

For each test case, output one line containing "Case #x:", where x is the test case number (starting from 1), then followed by Q lines, each line containing an integer y which is the shortest time you need for that query. If it's impossible, output -1 for that query instead.

예제 입력 1

2

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

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

예제 출력 1

Case #1:
11
Case #2:
18

힌트

In the first case, you are going to travel from station 1 of metro line 1 to station 4 of metro line 2. The best way is:

  • wait 3 minutes for line 1 and get on it.
  • take it for 3 minutes and get off at station 2.
  • take the tunnel and walk for 1 minute to station 2 of line 2.
  • wait 2 minutes for line 2 and get on it.
  • take it for 2 minutes and get off at station 4.

The total time is: 3+3+1+2+2=11.

채점 및 기타 정보

  • 예제는 채점하지 않는다.