This page is still under construction.

Parts of this page are still being built. What you see may change.

AdoraBalls

Time limit6sMemory limit512 MB

Summary
Given the counts of children in four colours and four bundle types, decide whether some non-negative number of each bundle can supply equal, positive balls to every child with none left over.
Level

Hard8 of 10

Topics
Number theory, Math, Brute force, Greedy
Solved
No attempts yet

Problem

An orphanage nearby collects a cheap toy ball called an AdoraBall. AdoraBalls come in four colours: azure, blue, cyan, and denim. Nobody sells them one at a time. The only thing you can buy is one of these four bundles.

  • One Bundle of Enjoyment holds a1a_1 azure balls, b1b_1 blue balls, c1c_1 cyan balls, and d1d_1 denim balls.
  • One Bundle of Festivity holds a2a_2 azure balls, b2b_2 blue balls, c2c_2 cyan balls, and d2d_2 denim balls.
  • One Bundle of Glee holds a3a_3 azure balls, b3b_3 blue balls, c3c_3 cyan balls, and d3d_3 denim balls.
  • One Bundle of Happiness holds a4a_4 azure balls, b4b_4 blue balls, c4c_4 cyan balls, and d4d_4 denim balls.

Every child in the orphanage has exactly one favourite colour among the four. a0a_0 children like azure best, b0b_0 like blue best, c0c_0 like cyan best, and d0d_0 like denim best.

You buy EE Bundles of Enjoyment, FF Bundles of Festivity, GG Bundles of Glee, and HH Bundles of Happiness, where EE, FF, GG, HH are non-negative integers. Then you open every bundle you bought and hand out the balls under three rules.

  • A child receives balls of their own favourite colour only.
  • Every child receives the same number of balls, and that number is at least one.
  • No ball is left over. You keep none of them.

Decide whether some choice of EE, FF, GG, HH obeys all three rules.

If the orphanage has no children at all, buying nothing obeys all three rules, so the answer is yes.

Input

The first line holds one integer TT, the number of test cases.

Each test case takes five lines. The first line holds four integers a0a_0, b0b_0, c0c_0, d0d_0, the number of children whose favourite colour is azure, blue, cyan, and denim, in that order. Line ii of the next four lines holds aia_i, bib_i, cic_i, did_i, the contents of the ii-th bundle.

Constraints

  • 1≤T≤200001 \le T \le 20000
  • 0≤ai,bi,ci,di≤550 \le a_i, b_i, c_i, d_i \le 55 for every i=0,1,2,3,4i = 0, 1, 2, 3, 4

Output

For each test case, print one line. Print POSSIBALL if non-negative integers EE, FF, GG, HH obeying all three rules exist, and IMPOSSIBALL otherwise.

Examples6

  1. Example 1

    Input
    3
    3 3 4 4
    1 0 0 0
    0 1 0 0
    0 0 1 0
    0 0 0 1
    3 3 4 4
    1 0 0 0
    0 1 0 0
    0 0 1 0
    1 1 1 0
    3 3 4 4
    1 2 4 3
    2 1 1 4
    2 4 3 1
    2 1 4 1
    
    Expected output
    POSSIBALL
    IMPOSSIBALL
    POSSIBALL
    
  2. Example 2

    Input
    2
    2 4 6 8
    1 2 3 4
    0 0 0 0
    0 0 0 0
    0 0 0 0
    2 4 6 8
    1 2 3 5
    0 0 0 0
    0 0 0 0
    0 0 0 0
    
    Expected output
    POSSIBALL
    IMPOSSIBALL
    
  3. Example 3

    Input
    3
    0 0 0 0
    1 2 3 4
    5 6 7 8
    9 10 11 12
    13 14 15 16
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 1
    1 1 1 1
    0 0 0 0
    0 0 0 0
    0 0 0 0
    
    Expected output
    POSSIBALL
    POSSIBALL
    IMPOSSIBALL
    
  4. Example 4

    Input
    3
    1 1 1 1
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    1 0 0 0
    1 1 0 0
    1 0 0 0
    0 0 0 0
    0 0 0 0
    1 0 0 0
    1 1 0 0
    0 1 0 0
    0 0 0 0
    0 0 0 0
    
    Expected output
    IMPOSSIBALL
    POSSIBALL
    IMPOSSIBALL
    
  5. Example 5

    Input
    3
    1 1 0 0
    2 0 0 0
    0 3 0 0
    0 0 0 0
    0 0 0 0
    1 1 0 0
    2 1 0 0
    1 2 0 0
    0 0 0 0
    0 0 0 0
    5 7 0 0
    3 0 0 0
    0 2 0 0
    0 0 0 0
    0 0 0 0
    
    Expected output
    POSSIBALL
    POSSIBALL
    POSSIBALL
    
  6. Example 6

    Input
    2
    1 2 2 2
    1 0 0 0
    0 1 0 0
    0 0 1 0
    1 1 1 1
    3 3 3 1
    1 0 0 0
    0 1 0 0
    0 0 1 0
    1 1 1 1
    
    Expected output
    IMPOSSIBALL
    POSSIBALL