This page is still under construction.

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

Supercap Travels

Time limit1sMemory limit128 MB

Summary
For each city distance, compute the best trip fare under the doubling speed profile and output the highest-scoring city per region.
Level

Medium6 of 10

Topics
Math, Greedy, Implementation
Solved
No attempts yet

Problem

In 2037, two and a quarter centuries after the first commercial railway locomotive, supersonic magnetic levitation capsules called supercaps entered service. A supercap runs at up to 512512 m/s, so a trip between two cities takes seconds instead of hours.

A supercap changes speed instantly, and every speed it holds is a power of two in m/s. One trip covers exactly DD meters, starts at rest, ends at rest, and has four parts.

  1. Acceleration. The capsule runs one second at 20=12^0 = 1 m/s, one second at 21=22^1 = 2 m/s, one second at 22=42^2 = 4 m/s, and so on, doubling every second until it reaches its upper speed UU. Every speed on the way, UU included, is held for exactly one second.
  2. Upper glide. The capsule holds UU for gg further seconds, where g≥0g \ge 0.
  3. Deceleration. The capsule halves its speed every second, from U/2U/2 down to the fixed lower speed of 1616 m/s. Every speed on the way, 1616 m/s included, is held for exactly one second.
  4. Final glide. The capsule holds 1616 m/s for a whole number of seconds, then 88 m/s, then 44 m/s, then 22 m/s, then 11 m/s, and then it stops. Each of these five counts may be zero, so a speed may be skipped.

The speed against time graph below shows a typical trip.

The upper speed is not a free choice. The capsule accelerates as far as it can, so UU is the largest power of two, at most 512512, whose acceleration and deceleration parts alone already fit inside the trip: (2U−1)+(U−16)≤D(2U - 1) + (U - 16) \le D.

The fare of a trip is proportional to

A−100BA - 100B

where AA is the number of seconds spent above 1616 m/s and BB is the number of seconds spent at 1616 m/s or below. Time above the lower speed earns money, and one second at or below the lower speed costs the corporation 100 times what one second above it earns. Every city is priced by its best trip, so the fare score of a city at distance DD is the largest value of A−100BA - 100B over all trips that cover exactly DD meters.

In the first build phase the capital city ZZ of a region gets one link into each neighbouring region, and that link reaches exactly one city of that region. Cities inside the same region are never linked to each other. For every neighbouring region, report the city with the largest fare score.

Input

The first line contains an integer TT (1≤T≤101 \le T \le 10), the number of test cases.

Each test case begins with a line holding an integer RR (1≤R≤101 \le R \le 10), the number of neighbouring regions. The descriptions of the RR regions follow in order.

Each region description begins with a line holding an integer CC (1≤C≤101 \le C \le 10), the number of cities of that region that have a supercap station. Each of the next CC lines holds a city name YY and an integer DD (1000≤D≤2×1061000 \le D \le 2 \times 10^6). The name YY is one word of English letters, and DD is the distance in meters from the station in the capital city ZZ to the station in city YY. Inside one region all distances are distinct.

Output

Print one line for each test case. The line starts with Case #x:, where xx is the number of the test case counting from 11, then one space, then the names of the chosen cities separated by single spaces. The names come in the same order as the regions of that test case. If several cities of one region reach the same largest fare score, choose the one that appears first in the input.

Examples1

  1. Example 1

    Input
    3
    1
    2
    Yoyo 8712
    Zing 3118
    2
    1
    Burj 18000
    3
    Dino 92400
    Ambro 47624
    Ebb 25725
    1
    2
    Klm 1500000
    Mys 1450000
    
    Expected output
    Case #1: Yoyo
    Case #2: Burj Ambro
    Case #3: Mys