Road

Time limit1sMemory limit128 MB

Summary
Given a road with passing places and a matrix describing where each eastbound car passes each westbound car, compute the minimum total time to realize that schedule. Cars drive at 12.5 m/s or wait, and cars in the same direction keep 25 m apart.
Level

Medium7 of 10

Topics
Implementation, Simulation, Greedy, Math
Solved
No attempts yet

Problem

In the Green Heart of Holland the villages are small and the roads are narrow. Some roads are only one car wide, so two cars that meet head on are stuck. A canal runs along both sides of the road, so neither driver can pull off the road to let the other one by. To get around this, the road is made a little wider here and there. At such a passing place one car stands aside while one or more cars from the other direction go past. That works while traffic is light. When many cars enter from both ends within a short time, the road jams.

Finding the best plan for such a road is quite hard, so this problem asks for something easier. The road runs east to west. An eastbound car enters at the west end and leaves at the east end, and a westbound car does the opposite. You are given the passing places, the number ee of eastbound cars, the number ww of westbound cars, and a schedule. For every pair of an eastbound car and a westbound car, the schedule names the point where those two cars pass each other.

Two cars that pass each other at point zz are both at zz at that moment, so neither of them travels past zz before the other one has arrived there.

Every car is ready to enter from the start and every driver wants to leave the road as early as possible. A car either stands still or drives at exactly 45 km/h, and starting and stopping take no time. Cars going the same way always keep a distance of at least 25 meters and never overtake each other, and they may queue up on the road while they wait. Two different passing places are at least 30 meters apart. The length of a car is ignored.

We measure the time between the moment the first car enters the road and the moment the last car leaves the road, and we want that interval to be as short as possible.

Input

The first line contains the number nn of test cases. Each test case has the following form.

  • One line with the length ll (0<l≤300000 < l \le 30000) of the road in meters and the number pp (0<p0 < p) of passing places.
  • One line with pp positive integers, the distance in meters from each passing place to the west end of the road, in increasing order.
  • One line with two positive integers ee and ww (0<e,w≤10000 < e, w \le 1000), the number of eastbound and the number of westbound cars.
  • ee lines with ww numbers each. The number zz (0≤z≤p+10 \le z \le p + 1) at position xx (1≤x≤w1 \le x \le w) of line yy (1≤y≤e1 \le y \le e) says that eastbound car yy passes westbound car xx at passing point zz. Passing point ii (1≤i≤p1 \le i \le p) is the ii-th passing place of the second line, passing point 00 is the west end, and passing point p+1p + 1 is the east end. The value z=0z = 0 means that car yy enters the road after car xx has left it, and the value z=p+1z = p + 1 means that car xx enters the road after car yy has left it.

Westbound car xx enters the road before westbound car x+1x + 1, and eastbound car yy enters the road before eastbound car y+1y + 1. Numbers on one line are separated by one or more spaces.

Output

For each test case print one line with one number: the time in seconds, rounded to the nearest integer, from the moment the first car enters the road until the moment the last car leaves it, for the fastest run that realizes the given schedule. Every distance is a whole number of meters and 45 km/h is 12.5 m/s, so the exact time is always a multiple of 0.08 seconds and never falls exactly halfway between two integers.

Examples2

  1. Example 1

    Input
    2
    150 1
    50
    1 1
    1
    100 1
    30
    3 2
    2 2
    1 2
    0 2
    
    Expected output
    16
    32
    
  2. Example 2

    Input
    1
    200 2
    60 140
    2 1
    2
    1
    
    Expected output
    29