Hot Dog Proliferation

Spread vendors sharing corners with pair splits that send one vendor east and one west, using the fewest moves so every corner holds at most one vendor.

Medium7MathGreedySortingPrefix sumNo attempts yetTime limit5sMemory limit512 MB

Problem

Hot dog vendors have started selling at the corners (intersections) of a very long east-west street. The trouble is that several vendors may end up on the same corner, and then they take each other's customers. The vendors have a plan for that.

Whenever a corner holds two or more vendors, exactly two of them can perform a move. A move works like this.

  • One vendor moves one corner further east along the street.
  • The other vendor moves one corner further west along the street.

The street is really long, so the vendors never run out of corners. You are given the starting position of every vendor. Find the minimum number of moves needed until all vendors sit on different corners.

For example, suppose the number of vendors on each corner, listed from west to east, is 0 0 2 1 2 0 0. Three moves separate them.

... 0 0 2 1 2 0 0 ...
        |
        +--- move here

... 0 1 0 2 2 0 0 ...
          |
          +--- move here

... 0 1 1 0 3 0 0 ...
            |
            +--- move here

... 0 1 1 1 1 1 0 ...

Input

Every corner carries an integer label, positive or negative. For each i, corner i+1 is the next corner east of corner i. The input describes corners with these labels.

The first line contains the number of test cases TT. TT test cases follow. Each case begins with the number of corners CC that hold at least one vendor in the starting configuration. Each of the next CC lines contains two space-separated integers PP and VV, meaning that corner PP holds VV vendors.

Limits

  • 1T501 \le T \le 50
  • 1C2001 \le C \le 200
  • 1000000P1000000-1000000 \le P \le 1000000
  • Within one test case the PP values are distinct and listed in increasing order.
  • Every VV is a positive integer, and the VV values of one test case sum to at most 100000100000.
  • It is always possible to separate the vendors in a finite number of moves.

Output

For each test case, print one line in the form Case #x: M, where xx is the test case number starting from 1 and MM is the minimum number of moves needed before all vendors end up on different corners.