Survivor (Large)

Eat foods in expiry order so each meal starts before it spoils and the next meal follows after its satiation time to maximize total survival time.

Medium7Dynamic programmingGreedySortingNo attempts yetTime limit10sMemory limit512 MB

Problem

You are stranded on a deserted island. You managed to grab one box of food, but the island is bare rock with no plants and no way to fish, so no more food is coming.

The box holds NN foods. Each food ii is labeled with its remaining shelf life PiP_i and the satiation time SiS_i it keeps hunger away once you eat it. Both values are measured in minutes.

The rules for eating are the following.

  • You start eating now, at minute 0.
  • Food past its shelf life is discarded at once. At minute tt you may eat only a food with PitP_i \ge t. A food whose remaining shelf life is 0 must be eaten right now or thrown away.
  • If you eat food ii at minute tt, you eat nothing else until minute t+Sit + S_i.
  • The moment minute t+Sit + S_i arrives, you starve to death immediately unless you eat another food.

Find the longest time you can survive on the island.

Input

The first line contains the number of test cases TT. Each test case has the following form.

The first line contains the number of foods NN. Each of the next NN lines contains the remaining shelf life PiP_i and the satiation time SiS_i of one food, separated by a space.

Constraints

  • Every input value is an integer.
  • 1T1001 \le T \le 100
  • 1N10001 \le N \le 1000
  • 0Pi1000000 \le P_i \le 100000
  • 1Si10001 \le S_i \le 1000

Output

For each test case xx, print the longest survival time yy on one line in the form Case #x: y.