This page is still under construction.

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

Phone Plot

Time limit1sMemory limit128 MB

Summary
Classify each phone number as EXCELLENT, GOOD, or BAD by counting the fewest straight segments that cover its keypad trace.
Level

Medium5 of 10

Topics
Geometry, Simulation, Implementation
Solved
No attempts yet

Problem

Mobile phones are part of everyday life, and every mobile phone has a number pad used to dial a telephone number. A typical number pad is a 4-by-3 grid of cells, shown in Figure 1. Some phone numbers are easy to memorize because dialing their digits in order traces an easy-to-remember geometric shape.

Figure 1

For a phone number we build a Phone Plot: a sequence of connected line segments. Suppose an nn-digit phone number is d1d2d3…dn−1dnd_1 d_2 d_3 \ldots d_{n-1} d_n. The first segment connects the center of pad d1d_1 to the center of pad d2d_2, the second connects the centers of d2d_2 and d3d_3, and in general the kk-th segment connects the centers of pad dkd_k and pad dk+1d_{k+1}; the last segment connects dn−1d_{n-1} and dnd_n.

Here are two examples. The Phone Plot for 1023289873 is drawn with thick lines in Figure 2(a), and Figure 2(b) shows the Phone Plot for 1159533969.

Figure 2(a)

Figure 2(b)

Note that some segments may overlap other segments along the same straight line.

We characterize a Phone Plot by its Minimal Number of Decomposing Segments (MNDS): the smallest number of straight line segments whose union is exactly the given Phone Plot. For example, the MNDS of 1023289873 is 5 and the MNDS of 1159533969 is 3. If the Phone Plot collapses to a single point (for example 0000000000), its MNDS is defined to be 0.

Using the MNDS, each phone number is sorted into one of three disjoint classes.

  • EXCELLENT if the MNDS is at most 3.
  • GOOD if the MNDS is exactly 4.
  • BAD if the MNDS is 5 or more.

By this rule 1023289873 is BAD and 1159533969 is EXCELLENT. Figure 3 shows one more example, 5233999008; its MNDS is 5, so it is BAD.

Figure 3

For each given phone number, decide whether it is EXCELLENT, GOOD, or BAD according to the MNDS of its Phone Plot.

Input

The first line contains the number of test cases TT. Each of the next TT lines contains one phone number, a string of decimal digits. Each phone number has more than 5 and fewer than 20 digits.

Output

For each test case, print EXCELLENT, GOOD, or BAD on its own line, according to the MNDS of that phone number's Phone Plot.

Examples2

  1. Example 1

    Input
    4
    1023289873
    1159533969
    147258052322
    5233999008
    
    Expected output
    BAD
    EXCELLENT
    GOOD
    BAD
    
  2. Example 2

    Input
    2
    123321
    0000000000
    
    Expected output
    EXCELLENT
    EXCELLENT