Printing Press

Given a DAG of book-ordering constraints, choose how many days to shorten each book so all finish within X, minimizing printing plus shortening cost.

Hard8Dynamic programmingGraphTopological sortBinary searchNo attempts yetTime limit10sMemory limit512 MB

Problem

A publisher has to print NN books within XX days. Books can normally be printed in parallel, but the publisher puts ordering restrictions on some of them. Volume 2 of a series cannot start printing before volume 1 is published, for instance. There are MM restrictions, each given as a pair (u,v)(u, v), which means that printing of book vv can start only after book uu is published.

Printing book ii takes AiA_i days and costs CiC_i. With extra resources on the presses, the printing time of book ii can be cut by RiR_i days down to AiRiA_i - R_i days, subject to AiRiBiA_i - R_i \ge B_i. Every day cut off costs an extra DiD_i.

Days are counted from day 0. If book ii starts on day SiS_i, its printing ends on day Si+(AiRi)1S_i + (A_i - R_i) - 1. A restriction (u,v)(u, v) means SvSu+(AuRu)S_v \ge S_u + (A_u - R_u), and every book must satisfy Si+(AiRi)XS_i + (A_i - R_i) \le X.

The total cost is the sum of the printing costs plus the sum of the shortening costs, iCi+iDiRi\sum_i C_i + \sum_i D_i R_i. Find the minimum total cost of printing all NN books within XX days.

Input

The first line holds the number of test cases TT. Each test case has the following format.

N X
A1 A2 ... AN
B1 B2 ... BN
C1 C2 ... CN
D1 D2 ... DN
M
u1 v1
...
uM vM
  • 1T3001 \le T \le 300
  • 1N2001 \le N \le 200, 1X1071 \le X \le 10^7
  • 1Ai1061 \le A_i \le 10^6, 1BiAi1 \le B_i \le A_i
  • 1Ci1061 \le C_i \le 10^6, 0Di1000 \le D_i \le 100
  • 0MN(N1)/20 \le M \le N(N-1)/2, 1ui,viN1 \le u_i, v_i \le N, uiviu_i \ne v_i
  • The same pair (u,v)(u, v) is never given twice, and the restrictions contain no cycle.

The distribution of NN is as follows. 85% of the test cases have N30N \le 30, three of them have N=200N = 200, and the rest have 30N10030 \le N \le 100.

Output

Print one line per test case. The line starts with Case k: , where kk is the test case number counting from 1. Append Impossible if the NN books cannot all be printed within XX days, otherwise append the minimum total cost.

Note

The figure below shows an example with three books. Book 1 takes 5 days and cannot be shortened. Book 2 and book 3 take 4 days each, and book 3 can start only after book 2 is published. Without extra resources all three books take 8 days. Cutting 2 days from book 2 and 1 day from book 3 finishes everything in 5 days.

Schedule of the three book example