Under construction

Still being built. Things may change

Stable Neighbors

Time limit5sMemory limit512 MB

Summary
Place given counts of six mane colors around a ring so that no two neighbors share a base hair color, and output the lexicographically smallest valid arrangement.
Level

Hard8 of 10

Topics
Greedy, Implementation, Math, Brute force
Solved
No attempts yet

Problem

You keep NN pet unicorns. The mane of a unicorn holds one or two kinds of hair among red, yellow and blue, and the color of the mane depends on exactly which kinds it holds.

  • A mane with a single kind of hair looks like that color. A mane with only blue hairs is blue.
  • A mane with red hairs and yellow hairs looks orange.
  • A mane with yellow hairs and blue hairs looks green.
  • A mane with red hairs and blue hairs looks violet.

You have RR, OO, YY, GG, BB and VV unicorns whose manes are red, orange, yellow, green, blue and violet.

You built a stable with NN stalls arranged in a ring, so every stall borders exactly two other stalls. You want to put exactly one unicorn in each stall. Two unicorns cannot be neighbors if their manes share at least one kind of hair. An orange mane cannot sit next to a violet mane, because both hold red hairs, and a green mane cannot sit next to a yellow mane, because both hold yellow hairs.

Decide whether all the unicorns fit, and print an arrangement when they do.

Input

The first line holds the number of test cases TT. Each of the next TT lines holds one test case: seven integers NN, RR, OO, YY, GG, BB and VV separated by spaces.

Limits

  • 1≤T≤1001 \le T \le 100
  • 3≤N≤10003 \le N \le 1000
  • R+O+Y+G+B+V=NR + O + Y + G + B + V = N
  • RR, OO, YY, GG, BB and VV are all at least 00.

Output

For each test case, print one line in the form Case #x: y, where xx is the test case number starting from 11.

If the unicorns do not all fit, yy is IMPOSSIBLE. Otherwise yy is a string of NN characters that lists the arrangement, read clockwise from a stall of your choice. Write R for a red mane, O for orange, Y for yellow, G for green, B for blue and V for violet. The stalls form a ring, so the first character and the last character are neighbors too.

When several arrangements work, print only the string that comes first in lexicographic order among all strings you can obtain, including every choice of the starting stall. The letters order as B, G, O, R, V, Y.

Notes

In a stable with three stalls every stall borders the other two, so two unicorns with the same mane color can never both be placed.

With two unicorns each of the red, yellow and blue manes, BYBRYR obeys the rules. It is not the answer, because another valid arrangement comes earlier in lexicographic order. Under the same counts BYRYRB breaks the rules, since its last B and its first B are neighbors.

BRYBGR breaks the rules as well. A blue mane sits next to a green mane, and both hold blue hairs.

Examples2

  1. Example 1

    Input
    4
    6 2 0 2 0 2 0
    3 1 0 2 0 0 0
    6 2 0 1 1 2 0
    4 0 0 2 0 0 2
    
    Expected output
    Case #1: BRBYRY
    Case #2: IMPOSSIBLE
    Case #3: BRGRBY
    Case #4: VYVY
    
  2. Example 2

    Input
    6
    3 1 0 1 0 1 0
    3 3 0 0 0 0 0
    3 0 1 0 0 2 0
    4 2 0 0 2 0 0
    6 0 3 0 0 3 0
    3 2 0 0 1 0 0
    
    Expected output
    Case #1: BRY
    Case #2: IMPOSSIBLE
    Case #3: IMPOSSIBLE
    Case #4: GRGR
    Case #5: BOBOBO
    Case #6: IMPOSSIBLE