Under construction

Still being built. Things may change

The Escape

Time limit2sMemory limit512 MB

Summary
In a three-floor grid dungeon with walls, ladders, and one-way trapdoors linking labelled squares, find the minimum time to reach the exit ladder on floor 1.
Level

Medium6 of 10

Topics
BFS, Graph, Shortest path, Implementation
Solved
No attempts yet

Problem

The legendary orb lies at the bottom of a dungeon with three floors. You fought past every trap and every monster, and the orb is now in your bag. The moment you lift it the dungeon starts to shake, because the lords of Pandemonium have noticed the theft. Stay too long and they catch you, so you have to carry the orb out as fast as you can.

The dungeon has three floors. Floor 1 is the top floor, floor 3 is the bottom floor, and the chamber with the orb is on floor 3. Each floor is a 50×5050 \times 50 grid. The bottom left square is (0,0)(0, 0) and both coordinates run from 00 to 4949. Every square is either a wall square or a floor square. You cannot enter a wall square, and you walk freely over floor squares.

A floor square may hold a ladder that leads up to the floor above, or a trapdoor connected to a ladder on the floor below. Moving to a neighboring floor square costs 1 time unit, and climbing a ladder also costs 1 time unit. You move in the four straight directions and diagonally as well, as long as the square you move into is not a wall. Trapdoors are locked from below, so you cannot use one to go down. Once you climb a ladder, the trapdoor locks behind you, so you cannot return to the floor you came from.

Floor 3 has three ladders up and no trapdoors. Floor 1 has three trapdoors and one exit ladder that leads out of the dungeon. Floor 2 has three trapdoors down and three ladders up. Every trapdoor and every ladder except the exit ladder is labelled 0, 1 or 2, and ladder ii on a floor leads to the square with trapdoor ii on the floor above. On one floor, no two of these ladders and trapdoors share a square.

Find the shortest time you need to reach the square with the exit ladder on floor 1.

Input

The first line contains the number of test cases TT. (1≤T≤51 \le T \le 5)

Each test case is a block of three lines, given in the order floor 3, floor 2, floor 1.

The first line describes floor 3. It starts with the coordinate of the square where the hero stands, given as two integers, followed by the coordinates of ladders 0, 1 and 2 in that order. After that comes the number of wall squares WW on this floor, followed by the coordinates of the WW wall squares.

The second line describes floor 2. It contains the coordinates of trapdoors 0, 1 and 2, the coordinates of ladders 0, 1 and 2, the number of wall squares WW, and the coordinates of the WW wall squares, in that order.

The third line describes floor 1. It contains the coordinates of trapdoors 0, 1 and 2, the coordinate of the exit ladder, the number of wall squares WW, and the coordinates of the WW wall squares, in that order.

Every coordinate is given as x y with 0≤x,y≤490 \le x, y \le 49. On one floor the wall coordinates are all different, and no wall square is the starting square, a ladder square or a trapdoor square. 0≤W≤25000 \le W \le 2500. An escape route always exists.

Output

For each test case print the smallest number of time units needed to reach the square with the exit ladder on floor 1, one per line. Do not count the climb up the exit ladder.

Hint

In the example the shortest route climbs ladder 0 on floor 3 and ladder 2 on floor 2. It moves between squares 8 times and climbs a ladder twice, so the answer is 10.

Examples2

  1. Example 1

    Input
    1
    0 0 4 4 3 1 3 4 3 1 2 2 2 3 2
    0 0 4 0 0 4 2 2 4 4 0 1 5 2 1 3 3 2 0 1 1 1 2
    0 0 0 2 3 4 4 4 5 0 1 1 0 2 0 3 0 4 0
    
    Expected output
    10
    
  2. Example 2

    Input
    1
    0 0 0 0 10 0 0 10 0
    0 0 1 0 2 0 1 1 5 5 6 6 0
    7 7 8 8 9 9 7 8 0
    
    Expected output
    4