Multi-Machine Scheduling of Two Applications

No attempts yetTime limit5sMemory limit256 MB

Problem

Ellie wants to finish two applications as early as possible. Application ii (i=1,2i=1,2) consists of ns(i)ns(i) identical steps numbered from 11 to ns(i)ns(i). Her cluster holds MM machines numbered from 11 to MM, and she runs the steps of both applications on them. The machines differ in speed: running one step of application ii on machine jj takes T(i,j)T(i,j) seconds.

Every step is CPU heavy, so a machine runs at most one step at a time. If several steps are assigned to one machine, their execution intervals must not overlap, and they may touch only at endpoints. A running step cannot be paused and resumed later either. Once a step starts, Ellie either lets it finish or cancels it before completion and runs it again from the beginning later, on the same machine or on another one.

Because of data dependencies, the steps of one application run in order. Step jj of application ii starts at the moment step j1j-1 of that application finished, or later. Step jj may run on the machine that ran step j1j-1 or on any other machine. Steps of different applications do not depend on each other.

Ellie can start executing steps at time 00. She wants the moment TENDTEND (in seconds) when the last step of either application finishes to be as small as possible. Print the minimum possible value of TENDTEND.

Input

The first line contains the number of test cases TT. The TT test cases follow, each on three lines. The first line holds the integers ns(1)ns(1), ns(2)ns(2) and MM. The second line holds the MM integers T(1,1)T(1,1), T(1,2)T(1,2), ..., T(1,M)T(1,M) in this order. The third line holds the MM integers T(2,1)T(2,1), T(2,2)T(2,2), ..., T(2,M)T(2,M) in this order.

  • 1T201 \le T \le 20
  • 1ns(i)1061 \le ns(i) \le 10^6
  • 1M101 \le M \le 10
  • 1T(i,j)10001 \le T(i,j) \le 1000

Output

For each test case, in the order given in the input, print the minimum possible value of TENDTEND on its own line.

Notes

With a single machine, every step of both applications runs on it one after another, so TENDTEND is the sum of the two execution times.

If the two applications use one machine each, TENDTEND is the larger of the two execution times. When both applications want the same machine, one of them has to work on a machine that is slower for it.

Once an application finishes, its machine becomes free and the other application can move there. Take ns(1)=765432ns(1)=765432, ns(2)=766ns(2)=766, M=2M=2, T(1,1)=1T(1,1)=1, T(1,2)=2T(1,2)=2, T(2,1)=1T(2,1)=1, T(2,2)=1000T(2,2)=1000. Running every step of application 1 on machine 1 finishes it at time 765432765432. Application 2 runs its first 765765 steps on machine 2 and finishes them at time 765000765000, waits until machine 1 is free, and runs its last step on machine 1, finishing at time 765433765433. Keeping the last step on machine 2 would end at 766000766000, so the move is faster.

Sometimes the fastest plan lets both applications share one machine over disjoint time intervals while each of them moves across several machines.