Budget

Time limit1sMemory limit256 MB

Summary
Given row sums, column sums, and bound constraints on individual cells or whole rows/columns, decide whether a matrix of non-negative integers exists.
Level

Medium4 of 10

Topics
Greedy, Math, Implementation
Solved
No attempts yet

Problem

A budget proposal is a table (matrix) in which the rows represent different kinds of expenses and the columns represent different sites.

Two things are already known: the total for each kind of expense (the sum of each row) and the total for each site (the sum of each column). In addition, there may be extra constraints on individual entries — for example, that one site needs at least 2000 kronor for food, or that another site will spend no more than 100 kronor on paperclips.

Your task is to determine whether a budget proposal exists that uses only non-negative integer amounts, matches every row total and column total exactly, and satisfies all of the extra constraints.

Input

The first line contains an integer NN, the number of test cases.

Each test case is given as follows:

  • The first line contains two integers mm and nn (m≤200m \le 200, n≤20n \le 20), the number of rows and the number of columns.
  • The second line contains mm integers: the row sums of the matrix.
  • The third line contains nn integers: the column sums of the matrix.
  • The fourth line contains an integer cc, the number of constraints.
  • Each of the next cc lines contains one constraint.

A constraint is written as r c op v. The integers rr and cc select an entry (or a set of entries) of the matrix: the upper-left corner is 1 1, and 0 means all, so 4 0 means every entry in the fourth row and 0 0 means the entire matrix. op is one of <, =, >, and vv is an integer. For example, 1 2 > 5 means the entry in row 1, column 2 must be strictly greater than 55, and 4 0 = 3 means every entry in the fourth row must equal 33.

Output

For each test case, print POSSIBLE if there exists a matrix of non-negative integers that satisfies all row sums, all column sums, and every constraint; otherwise print IMPOSSIBLE. Print each answer on its own line.

Examples3

  1. Example 1

    Input
    2
    2 3
    8 10
    5 6 7
    4
    0 2 > 2
    2 1 = 3
    2 3 > 2
    2 3 < 5
    
    2 2
    4 5
    6 7
    1
    1 1 > 10
    Expected output
    POSSIBLE
    IMPOSSIBLE
    
  2. Example 2

    Input
    1
    1 1
    5
    5
    0
    Expected output
    POSSIBLE
    
  3. Example 3

    Input
    1
    1 1
    5
    6
    0
    Expected output
    IMPOSSIBLE