Crossing the Road (Large)

Find the minimum time for a pedestrian to travel across a grid of intersections where crossing depends on periodically timed traffic lights.

Medium7Shortest pathGraphSimulationNo attempts yetTime limit5sMemory limit512 MB

Problem

The city in this problem is a grid made of NN roads running east to west and MM roads running north to south. There is an intersection wherever an east-west road meets a north-south road, and every intersection has pedestrian lights. A pedestrian can cross a road only in the direction that has a green light.

Intersections are numbered from row 00 to row N1N-1 going from north to south, and from column 00 to column M1M-1 going from west to east.

The pedestrian wants to get from the northeast corner of the southwest block to the southwest corner of the northeast block. She starts at the southwest corner of intersection (N1,0)(N-1, 0) and finishes at the northeast corner of intersection (0,M1)(0, M-1).

Two moves are available.

  • Cross a road at one intersection, moving to a corner that is not diagonally opposite. This takes 1 minute, and the light for the direction she is crossing must stay green for that entire minute. Crossing in the north-south direction needs the north-south light to be green, and crossing in the east-west direction needs the east-west light to be green.
  • Walk along one edge of a block to a corner of a neighboring intersection. This takes 2 minutes.

The pedestrian moves only along the edges of the blocks. She cannot go straight from one corner of a block to the opposite corner. She can wait at a corner as long as she likes.

Traffic lights repeat the following cycle. At intersection ii the north-south lights stay green for SiS_i minutes while the east-west lights stay red. Then the north-south lights turn red, the east-west lights turn green, and they stay that way for WiW_i minutes. Then the same cycle starts again. The pedestrian starts moving at t=0t=0 minutes, and the lights at intersection ii start a cycle by turning green in the north-south direction at t=Tit=T_i minutes. The same cycle repeats before t=Tit=T_i as well.

For example, suppose one intersection has S0=3S_0 = 3, W0=2W_0 = 2, T0=0T_0 = 0. The north-south direction turns green at 0 minutes and stays green for 3 minutes, so during that time the pedestrian can cross in the north-south direction and not in the east-west direction. Then the lights switch, and for the next 2 minutes she can cross in the east-west direction only. Five minutes after it started, the cycle starts again. This is exactly the same as S0=3S_0 = 3, W0=2W_0 = 2, T0=10T_0 = 10.

Input

The first line contains the number of test cases, CC. Then CC test cases follow in the format below.

The first line of a test case contains NN and MM, the number of east-west roads (rows) and the number of north-south roads (columns). Then NN lines follow. The iith of those lines describes the intersections in row ii counted from the north, where row 0 is the northmost, and contains 3M3M integers separated by spaces in this order:

Si,0 Wi,0 Ti,0 Si,1 Wi,1 Ti,1  Si,M1 Wi,M1 Ti,M1S_{i,0}\ W_{i,0}\ T_{i,0}\ S_{i,1}\ W_{i,1}\ T_{i,1}\ \dots\ S_{i,M-1}\ W_{i,M-1}\ T_{i,M-1}

Si,jS_{i,j}, Wi,jW_{i,j} and Ti,jT_{i,j} refer to the intersection in row ii from the north and column jj from the west.

Limits

  • CC, NN, MM, Si,jS_{i,j}, Wi,jW_{i,j} and Ti,jT_{i,j} are all non-negative integers.
  • C100C \le 100
  • 1N,M201 \le N, M \le 20
  • 0<Si,j,Wi,j1070 < S_{i,j}, W_{i,j} \le 10^7
  • 0Ti,j1080 \le T_{i,j} \le 10^8

Output

For each test case, output one line containing Case #x: t, where xx is the test case number and tt is the minimum number of minutes the pedestrian needs to get from her starting corner to her destination.

Hint

In the first test case of the first example, the lights are the ones described in the statement. The pedestrian crosses to the north (1 minute), waits 2 minutes and then crosses to the east (1 minute), for a total of 4 minutes.

The second test case is shown in the diagram below. The pedestrian crosses to the east (1 minute), waits 2 minutes and crosses to the north (1 minute). Then she walks east one block (2 minutes) and crosses to the east (1 minute), for a total of 7 minutes.