Reading a Chord

Given a set of 3 to 5 tones, print every chord name (base chord plus optional tension) whose tone set equals the given set, in ASCII order.

Medium4Hash mapMathBrute forceImplementationNo attempts yetTime limit8sMemory limit512 MB

Problem

Given a set of tones, print every chord name that denotes exactly that set.

The scale has 12 tones:

C, C#, D, D#, E, F, F#, G, G#, A, A#, B

Two neighboring tones differ by a half step and the right one is higher, so G is three half steps higher than E. C is a half step higher than B. Strictly speaking the C that follows B belongs to the next octave, but octaves do not matter here, so a tone is an offset counted modulo 12.

A chord is a set of two or more different tones and is called by a chord name. One chord may have several chord names, but a chord name denotes exactly one set of tones.

A chord name is a base chord followed by at most one tension. There are five base chords, listed here on the root C.

Chord nameComponents
CC, E, G
C7C, E, G, A#
CM7C, E, G, B
CmC, D#, G
Cm7C, D#, G, A#

The tone a chord is built on, C for the five chords above, is the root of the chord. A chord name gives its root as an absolute tone and every other component as a distance in half steps from the root, so moving the root moves every component with it. The name D denotes the tones D, F# and A, which are C, E and G shifted up by two half steps.

A tension is a number with an optional plus or minus sign, written in parentheses after the base chord. The number is a distance from the root.

TensionHalf steps above the rootTone when the root is C
-91C#
92D
+93D#
-114E
115F
+116F#
-138G#
139A
+1310A#

C(9) is the chord C plus the tone D, that is the set C, D, E, G. C(+11) is the chord C plus the tone F#. Because the number counts from the root, changing the root changes the tone it selects, as the two figures below show.


Figure 1: tensions for chords rooted on C


Figure 2: tensions for chords rooted on E

A tension of +5+5 or 5-5 is special. It adds no tone. It sharps the fifth (shifts it up a half step) or flats it (shifts it down a half step), where the fifth is the component seven half steps above the root. So C(+5) is the set C, E, G#, not C, E, G, G#.

Every other tension adds exactly one tone, and that tone is never one the base chord already contains. A name whose tension lands on a component of its own base chord is not a chord name and is never printed. C(-11) is not a chord name, because 11-11 selects E and the chord C already has E.

The syntax of a chord name in Backus-Naur form:

chord        ::= base_chord tension_part?
base_chord   ::= root_spec ( "" | "7" | "M7" | "m" | "m7" )
root_spec    ::= tone
tone         ::= "C" | "C#" | "D" | "D#" | "E" | "F" | "F#" | "G" | "G#" | "A" | "A#" | "B"
tension_part ::= "(" tension ")"
tension      ::= ( ("+" | "-")? ("9" | "11" | "13") ) | ( ("+" | "-") "5" )

Suppose you look for the names of the tones C, E and G. The base chord table gives C at once, so C is one answer. There is one more. Em is the set E, G, B, and sharping its fifth turns B into C, which leaves E, G, C. That modification is written with the tension +5+5, so Em(+5) is the second answer.

Input

The first line contains an integer NN, the number of test cases (1N20001 \le N \le 2000).

Each of the next NN lines contains an integer mm (3m53 \le m \le 5) followed by mm tones, with exactly one space between neighboring items. Every tone is one of the 12 names above, and the same tone never appears twice on one line.

Output

Print one line for each test case.

On that line print every chord name whose set of tones equals the set given in the test case, separated by exactly one space, with no extra space anywhere. Print a chord name at most once.

Print the names in ascending order, comparing two names character by character by ASCII code. Under that order # comes first, then (, then +, then -, then the digits, then the uppercase letters, then the lowercase letters, so C, C#, C(13), C7, CM7, Cm is a sorted run and Am(+13) comes before Am7.

If no chord name matches the set, print UNKNOWN in capital letters.