Longest straight from dice
InterviewMemory limit1024 MB
Given dice with S_i sides each, find the most dice that can each show one number in a run of consecutive integers.
Problem
The most common die has 6 sides, each showing a different integer from 1 to 6. Many games use other kinds of dice too. A is a die with sides, each showing a different integer from 1 to . A is a typical die, a has four sides, and a has one million sides.

We start with a collection of dice. The -th die is a , meaning it has sides showing the integers 1 through . A straight of length starting at is the list of integers . We choose some of the dice (possibly all) and pick one number from each to form a straight. What is the longest straight we can form?
Input
The first line contains the number of test cases . Each test case is given in two lines. The first line contains a single integer , the number of dice. The second line contains integers , where each integer is the number of sides of a different die.
Output
For each test case, output one line in the format Case #x: y, where is the test case number (starting from 1) and is the maximum number of dice that can be used in a straight.
Limits
.
Hints
In Sample Case #1, there are multiple ways to form a straight using all 4 dice. One possible way is shown in the image above.
In Sample Case #2, no die can show an integer greater than 5, so no straight can use more than 5 dice. There are multiple ways to form a straight with exactly 5 dice. For example, pick 4 and 5 from both d5 dice, then pick 1, 2, and 3 from three of the d4 dice to form 1, 2, 3, 4, 5.
In Sample Case #3, the straight 1, 2, 3, 4, 5, 6, 7, 8, 9 is possible. Discard one d4 and use the remaining d4 dice, the d5, and the d6 to get 1 through 4. Use the d7 dice to get 5 through 7, and the d10 dice to get 8 and 9. No straight of length 10 is possible, so this is the best that can be done.
In Sample Case #4, only a straight of length 1 can be formed, and any integer on the given d10 works.