Under construction

Still being built. Things may change

Kingdom Rush

Time limit5sMemory limit512 MB

Summary
Earn one-star and two-star clears with star thresholds in an order that finishes every level with two stars in the fewest plays.
Level

Medium5 of 10

Topics
Greedy, Sorting
Solved
No attempts yet

Problem

Ryan is playing Kingdom Rush, a single-player tower defense game made by Ironhide Game Studio. In this game you earn stars by clearing levels, and more stars make the player stronger. So even if Ryan cannot clear level 2 right now, he may be able to clear it after collecting stars on level 1.

The rules in this problem are not the rules of the real game. You do not need to have played it to solve the problem.

Here, clearing a level gives either a one-star rating or a two-star rating. Stars are earned as follows.

  • Clearing a level you have never cleared before with a one-star rating earns 1 star.
  • Clearing a level you have never cleared before with a two-star rating earns 2 stars.
  • Clearing a level you had cleared before only with a one-star rating, this time with a two-star rating, earns 1 more star.

In any other case you earn no stars.

Ryan cannot clear every level right away. To clear level ii with a one-star rating he needs aia_i stars, and to clear it with a two-star rating he needs bib_i stars. bib_i is not smaller than aia_i.

For example, consider a game with two levels. Level 1 needs 0 stars for a one-star rating and 1 star for a two-star rating. Level 2 needs 0 stars for a one-star rating and 2 stars for a two-star rating. Ryan can play like this.

  1. He starts with 0 stars, so he can clear level 1 or level 2 with a one-star rating. He clears level 1 with a one-star rating and then has 1 star.
  2. Now he can clear level 2 with a one-star rating, or level 1 with a two-star rating. He clears level 1 with a two-star rating and then has 2 stars.
  3. Now he can clear level 2 with a two-star rating. He does that and then has 4 stars.
  4. Every level has a two-star rating, and he has 2 stars per level for 4 in total. He cleared a level 3 times: level 1 twice and level 2 once.

Find the minimum number of times Ryan has to clear a level so that every level has a two-star rating.

Input

The first line contains the number of test cases TT. Each test case starts with a line containing the number of levels NN. The next NN lines describe the levels. The iith of them contains the integers aia_i and bib_i, the number of stars needed for a one-star rating and for a two-star rating on level ii.

Limits

  • 1≤T≤1001 \le T \le 100
  • 1≤N≤101 \le N \le 10
  • 0≤ai≤bi≤20010 \le a_i \le b_i \le 2001

Output

For each test case, print one line of the form Case #x: y, where xx is the test case number starting from 1 and yy is the minimum number of times Ryan has to clear a level until every level has a two-star rating. If a two-star rating on every level is impossible, print Too Bad in place of yy, with exactly that capitalization.

Examples3

  1. Example 1

    Input
    1
    2
    0 1
    0 2
    
    Expected output
    Case #1: 3
    
  2. Example 2

    Input
    3
    1
    0 0
    1
    0 1
    1
    0 3
    
    Expected output
    Case #1: 1
    Case #2: 2
    Case #3: Too Bad
    
  3. Example 3

    Input
    2
    3
    0 0
    0 0
    0 0
    2
    1 1
    0 2
    
    Expected output
    Case #1: 3
    Case #2: 3