Painting a fence (small)

From up to 10 offers, choose the fewest that cover sections 1 to 10000 using at most 3 distinct colors.

Medium4Brute forceIntervalsGreedyInterviewNo attempts yetTime limit5sMemory limit512 MB

Problem

You need to hire people to paint a fence. The fence is 10000 contiguous sections, numbered 1 to 10000.

Painters send you offers. Each offer is to paint one contiguous range of sections in one color. You have to accept a set of the offers so that both of these hold.

  • Every section of the fence is painted.
  • At most 3 colors are used to paint the fence.

If both conditions can be satisfied, find the minimum number of offers you must accept.

Two accepted offers may overlap. Two colors are the same color only when their strings are equal.

Input

The first line contains the number of test cases TT.

Each test case is given as follows.

  • The first line contains the number of offers NN.
  • The next NN lines each contain one offer in the form C A B. CC is the color, an uppercase letter string of length at most 10. AA is the first section and BB is the last section to be painted, with 1AB100001 \le A \le B \le 10000.

Limits

  • 1T501 \le T \le 50
  • 1N101 \le N \le 10

Output

Print one line per test case, in the order the cases are given, in the form Case #X: Y, where XX is the case number and YY is the minimum number of offers that must be accepted. If no set of offers satisfies the conditions, print Case #X: IMPOSSIBLE on that line instead.

Hint

These are the five test cases of the sample input.

  • In the first case, accepting both offers paints the whole fence, 5000 sections each, with no overlap.
  • In the second case the painters overlap, which is allowed.
  • In the third case, accepting all four offers covers the whole fence but uses 4 colors, so it does not satisfy the conditions.
  • In the fourth case, section 4001 cannot be painted.
  • In the fifth case, accepting just the first and the second offer paints the whole fence.