Two college students, Ji-Sung and Young-Pyo, are roommates sharing one dormitory room. Because they have lived together for a long time, they also share household appliances such as a hair dryer, an electric iron, and a battery charger. An appliance can be used by only one person at a time, so whenever both of them want the same appliance, their usage intervals must not overlap.
There are n shared appliances, numbered from 1 to n. Using appliance i once takes Ji-Sung pi time units and Young-Pyo qi time units.
On a given day, Ji-Sung wants to use appliances in the exact order oi1,oi2,…,oiα, and Young-Pyo in the exact order oj1,oj2,…,ojβ. The same appliance may appear several times in a sequence. Each person can start the next appliance in their own sequence only after finishing the current one, but the two people act independently and may use different appliances at the same time. When both need the same appliance, one of them has to wait until the other is done.
Find the earliest time by which both of them can finish their entire sequences.
Worked example: suppose there are 3 appliances, taking Ji-Sung 1,2,1 time units and Young-Pyo 2,1,3 time units respectively. Ji-Sung uses them in the order o1,o3,o1,o2 and Young-Pyo in the order o1,o2,o1,o3. An optimal schedule finishes at time 8.
Ji-Sung: 
Young-Pyo: 
The input is read from standard input. The first line contains the number of test cases T. Each test case consists of the following six lines.
Write to standard output. For each test case, print on its own line the minimum time by which both Ji-Sung and Young-Pyo finish using all of their appliances.