TopCoder

User's AC Ratio

NaN% (0/0)

Submission's AC Ratio

NaN% (0/0)

Tags

Description

My kid's favorite subject is math, as you know by now. He is learning
division now, and his teacher has taught him about even numbers being
divisible by 2, numbers whose digits add up to a multiple of 3 being
exactly divisible by 3 etc.


He was familiar with division by 11 during selection for soccer teams
on his playground, and was wondering whether there was any easy rule
to see if a number was divisible by 11. For example, he wondered, if
he arranged a number of digits 0-9 in a row to form a number, which
ones would be divisible by 11?


He decided to start off with d[1] 1's, d[2] 2's ..., d[9] 9's, and
seeing what is the minimum multiple of 11 that he could get by using all
these digits, together with any number of 0's. Please help him figure
out how many digits there are in this minimum multiple.

Input Format

The first line will contain the number of test cases T. T lines follow
one corresponding to each test case. Each line has 9 integers
d[1],...,d[9].

1 <= T <= 100

1 <= d[1] + ... + d[9] <= 100

Output Format

Output T lines one corresponding to each test case. The ith line
should contain the required answer for the corresponding test case.
If he will not be able to form a multiple of 11 in this way, print -1.

Sample Input 1

2
2 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 1

Sample Output 1

2
3

Hints

For the first case, the number 11 can be formed which has 2 digits.

For the second case, number 209 can be formed which is divisible by 11 and has 3 digits.

Problem Source

Migrated from old NTUJ.

Regional Amritapuri 2010

Subtasks

No. Testdata Range Score

Testdata and Limits

No. Time Limit (ms) Memory Limit (VSS, KiB) Output Limit (KiB) Subtasks
0 1000 65536 200