Multiprocessor Scheduling#371

Multiprocessor Scheduling

2 seconds
64 megabytes
Very hard

There are two applications running on a multiprocessor machine. Each application (i = 1, 2) consists of procedures, which are numbered from to and must be executed sequentially (in the order 1, \ldots, N). A procedure is identified by a pair , where i = 1, 2 identifies the application, and 1 \leq j \leq N is the procedure index within application . Procedure can only be executed on processor P(i, j) and its execution lasts D(i, j) seconds. We want to schedule the procedures of both applications so that the moment the last procedure finishes (in either application) is minimized; this moment is called the makespan. Both applications are available for scheduling from time .

The scheduling must follow these rules:

Write a program that, given the procedures of both applications, finds the minimum makespan.

Input

The first line contains — the number of test cases.

The first line of each test case contains the number (1 \leq N \leq 300), the number of procedures in each application. Then lines follow for the first application; the -th of these lines contains two integers separated by a space: P(1, j) and D(1, j). After that, the next lines describe the second application, in the same format: the -th line contains P(2, j) and D(2, j). 1 \leq P(i, j) \leq 10, 1 \leq D(i, j) \leq 15000 (i = 1, 2; 1 \leq j \leq N).

Note that it is possible that P(i, j) = P(k, l), which means procedures and (k, l) cannot overlap in time. If i = k, this does not matter, since those belong to the same application and must be executed sequentially.

Output

The output must contain exactly lines, each containing a single number — the minimum makespan for the corresponding test case. Print the answers in the order given in the input (i.e., the -th line corresponds to the -th test case).

Examples

Example 1

Loading
Loading