Under construction

Still being built. Things may change

Lumbo Jumbo

Time limit2sMemory limit512 MB

Summary
Given a 3 by N weighted grid and P important cells, find the minimum total edge cost of a walk that visits every important cell at least once.
Level

Medium7 of 10

Topics
Dynamic programming, Graph, Minimum spanning tree
Solved
No attempts yet

Problem

In Lala land, young Lumbo and his older brother Jumbo used to play a game they loved. The original game went like this. Jumbo picks a number NN and draws a 3×N3 \times N grid on the floor, marking some cells as important. A cell is written (i,j)(i, j), where ii is the row number (row 1 is the top row) and jj is the column number (column 1 is the leftmost column). The grid has 3 rows and NN columns. Jumbo then asks Lumbo to jump on the grid cells with one leg, following these rules:

  1. Lumbo can start from any cell he chooses.
  2. Every important cell must be visited at least once.
  3. Lumbo can jump from one cell to another only if the two cells are adjacent. Two cells are adjacent if they share at least one edge.
  4. Lumbo may visit non-important cells as many times as he wants. Otherwise the game could be impossible.

One day Jumbo decided the game was too easy, so he added a new rule:

  1. Jumbo assigns a cost to each edge between two adjacent cells. To cross an edge, Lumbo pays its cost. Once Lumbo has paid for an edge, he can use it as many times as he wants without paying again.

Jumbo asks Lumbo to jump again under all the rules above, visiting each important cell at least once, and this time to minimize the total cost.

Lumbo tried several jumps but is not sure whether any of them is the cheapest. Help him compute the minimum cost.

Input

The first line contains a single integer TT, the number of test cases (1≤T≤1001 \le T \le 100).

Each test case starts with a line containing two integers NN and PP. NN is the number of columns (1≤N≤10,0001 \le N \le 10,000), and PP is the number of important cells (1≤P≤1001 \le P \le 100).

The next 3 lines each contain N−1N-1 integers. The first line gives the costs of the vertical edges in row 1, that is, between (1,1)(1, 1) and (1,2)(1, 2), up to between (1,N−1)(1, N-1) and (1,N)(1, N). The second and third lines give the same for rows 2 and 3.

The next 2 lines each contain NN integers. The first line gives the cost of the edge between (1,c)(1, c) and (2,c)(2, c) for each column cc. The second line gives the cost of the edge between (2,c)(2, c) and (3,c)(3, c) for each column cc.

Every edge cost is between 0 and 100 inclusive (0≤0 \le edge cost ≤100\le 100).

The last PP lines each contain two integers ii and jj, the row and column of an important cell (1≤i≤31 \le i \le 3, 1≤j≤N1 \le j \le N).

Output

For each test case, print one line with the minimum cost Lumbo must pay to visit every important cell at least once.

Hint

The figure below explains the second test case.

Examples1

  1. Example 1

    Input
    2
    3 4
    11 28
    1 62
    94 89
    31 15 64
    76 57 33
    1 2
    1 1
    1 3
    2 3
    4 3
    13 21 32
    11 6 70
    23 14 15
    16 23 31 17
    34 13 11 9
    1 1
    3 4
    1 4
    
    Expected output
    103
    85