Welcome to ZOJ
Select Problem
ZOJ Problem Set - 2332

Time Limit: 2 Seconds      Memory Limit: 65536 KB

Wealthy alsomagic!
Supernatural alsomagic!
But also poor alsomagic!
Because he is now puzzled by a problem, and will go crazy if you can��t help him.

alsomagic has a lot of gems with different colors and shapes. His supernatural power provides him the ability of transforming some gems' shape and color! Now he wants to give some of gems to his darling girlfriend as gift. But a problem came with him. His girlfriend dislikes too many gems with the same color! And alsomagic, will also be disgusted with lots of gems with the same shape.

So he attempts to change some gems to solve this problem.


The first line of a multiple input is an integer T, followed by T input blocks. The first line of each input block are two integers N, M (1 =< N,M <= 10) indicating alsomagic has N different shapes of gems with M different colors, then N lines of integers follow, each line contains M integers. The integer K in row R column C states that alsomagic has K number of gems with shape R and color C.

Follow this is integer D, then D lines of integers. Each line contains four integers R1��C1��R2��C2(0 =< R1, R2 < N, 0 <= C1, C2 < M) indicating one kind of transfor way, by which he can change some of shape R1, color C1 gems to shape R2, color C2, and VICE VERSA. But each way of transform can only be used ONCE!

Then N integers, the K-th integer is the number of gems with SHAPE K that alsomagic can tolerance.
Then M integers. The K-th integer is the number of gems with COLOR K that alsomagic��s girlfriend can tolerance!


One line percase, with ��Yes�� if there is a way to solve this problem, otherwise ��No��.

Sample Input


1 3
3 2 6
0 1 0 2
1 3 2

1 3
3 2 6
0 1 0 2
1 3 2

Sample Output


Special thanks to:
SHEN, Guanghao
WU, Jiazhi

Author: LIU, Yaoting
Source: ZOJ 3rd Anniversary Contest
Submit    Status