AdoraBalls
Time limit6sMemory limit512 MB
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 azure balls, blue balls, cyan balls, and denim balls.
- One Bundle of Festivity holds azure balls, blue balls, cyan balls, and denim balls.
- One Bundle of Glee holds azure balls, blue balls, cyan balls, and denim balls.
- One Bundle of Happiness holds azure balls, blue balls, cyan balls, and denim balls.
Every child in the orphanage has exactly one favourite colour among the four. children like azure best, like blue best, like cyan best, and like denim best.
You buy Bundles of Enjoyment, Bundles of Festivity, Bundles of Glee, and Bundles of Happiness, where , , , 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 , , , 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 , the number of test cases.
Each test case takes five lines. The first line holds four integers , , , , the number of children whose favourite colour is azure, blue, cyan, and denim, in that order. Line of the next four lines holds , , , , the contents of the -th bundle.
Constraints
- for every
Output
For each test case, print one line. Print POSSIBALL if non-negative integers , , , obeying all three rules exist, and IMPOSSIBALL otherwise.