This page is still under construction.

Parts of this page are still being built. What you see may change.

Driving license test

Time limit2sMemory limit256 MB

Summary
Move only right and down from the top left corner to the bottom right corner with at most G fuel to arrive as early as possible.
Level

Medium6 of 10

Topics
Dynamic programming, Graph
Solved
No attempts yet

Problem

A driving license test is held on a course laid out as a grid with MM rows and NN columns.

The test has three rules.

  • Rule 1: The candidate starts at the top left point s and drives east (right) and south (down) only, until reaching the bottom right point t.
  • Rule 2: Driving one grid segment straight takes time LL, the same going right and going down. LL is a constant fixed before the test. Changing direction always takes time 1. At the start point s the candidate can pick either right or down, and that first choice costs no time.
  • Rule 3: The car is filled with GG fuel before the test. The candidate must reach t as early as possible while using at most GG fuel.

Following rule 2 means driving to suit the state of the road, so the fuel spent over the same time LL differs from segment to segment. The integer written on a unit segment is the fuel it takes to drive that segment for time LL. Changing direction takes no fuel.

The picture above draws two routes that work on the 4 row 6 column example when G=19G = 19 and L=10L = 10. The left route spends 17 fuel and arrives at time 83. It drives 8 unit segments straight and changes direction 3 times. The right route spends only 16 fuel, but it changes direction more often, so its time is 85. A few other routes also arrive at time 83 with at most 19 fuel. No route arrives at t earlier than time 83 while using at most 19 fuel.

Given the state of the grid together with GG and LL, find the earliest time to reach t.

Input

The first line has the number of test cases TT.

The first line of each test case has four integers MM, NN, LL and GG. MM is the number of rows of the grid, NN is the number of columns, LL is the time to drive one unit segment straight, and GG is the amount of fuel. (2≤M,N≤1002 \le M, N \le 100, 1≤L≤101 \le L \le 10, 1≤G≤1 000 0001 \le G \le 1\,000\,000)

Then come MM lines with N−1N-1 integers each. The jj-th integer on the ii-th line is the fuel of the horizontal segment that joins column jj and column j+1j+1 in row ii. After that come M−1M-1 lines with NN integers each. The jj-th integer on the ii-th line is the fuel of the vertical segment that joins row ii and row i+1i+1 in column jj.

Every unit segment takes between 1 and 1000 fuel.

Output

For each test case print the answer on its own line. If s to t is possible with at most GG fuel, print the earliest arrival time. Otherwise print -1.

Examples2

  1. Example 1

    Input
    3
    4 6 10 19
    4 3 6 7 9
    3 1 2 7 5
    2 2 6 1 9
    5 3 4 3 2
    1 5 4 4 4 2
    6 1 1 3 1 7
    2 2 3 2 3 5
    3 4 5 10
    4 5 6
    2 3 1
    5 7 8
    1 8 6 7
    4 6 9 1
    3 3 10 9
    2 2
    2 2
    2 2
    3 3 3
    3 3 3
    
    Expected output
    83
    27
    -1
    
  2. Example 2

    Input
    2
    2 2 1 3
    5
    1
    2 3
    2 2 10 2
    5
    1
    2 3
    
    Expected output
    3
    -1