HS12MULT - Multinomial numbers

no tags 

You may perhaps know how to find the last nonzero digit of n factorial. This time your task is harder, find the last nonzero decimal digit of the multinomial coefficient:
(a1+a2+ … +an)!/(a1!*a2!* … *an!) . Note that this is an extension of the classical problem, since factorials (and binomial numbers) are also multinomial numbers!

Input

An integer T, denoting the number of testcases (T≤10000). In each line you are given one positive integer ( n≤20 ), followed by n integers: a1,a2,…,an, where 0 ≤ ai ≤ 1000000000. There are 4 input sets for 10 points.

Output

Output T lines, the case number followed by the last nonzero decimal digit. See the sample output for the correct format!

Example

Input:
7
1 0
2 11 9
4 5 7 2 9
3 1000 3000 2000
3 100000000 200000000 300000000
2 4 9
8 1 1 4 7 4 8 9 2

Output:
Case 1: 1
Case 2: 6
Case 3: 8
Case 4: 6
Case 5: 2
Case 6: 5
Case 7: 4

Warning: A naive algorithm will probably solve only the first two input sets.


hide comments
Ehor Nechiporenko: 2012-12-14 15:38:49

@Robert Gerbicz, thanks you a lot for the problem. It helped me to find one interesting quite usefull algo and also to make a good job of optimization!

Robert Gerbicz: 2012-12-09 14:11:22

Changed the judge, you have to solve all input sets to get accepted.

Robert Gerbicz: 2012-12-09 14:09:33

That 1.7 sec. is the total running time on the solved sets. You have timeout only on the last, but hard input.

Ehor Nechiporenko: 2012-12-09 14:09:33

How could be the solution have the timeout with 10 seconds, if total time of running is 1.7 sec? Is everything fine with judge?


Added by:Robert Gerbicz
Date:2012-09-18
Time limit:1s-1.331s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ICK NODEJS PY_NBC
Resource:High School Programming League 2012/13