TopCoder

User's AC Ratio

NaN% (0/0)

Submission's AC Ratio

NaN% (0/0)

Tags

Description


Lately, there is one serious problem with Panda Land Safe Box: several safes have been robbed! The safes are using old 4-digits rolling lock combination (you only have to roll the digit, either up or down, until all four of them match the key). Each digit is designed to roll from 0 to 9. Rolling-up at 9 will make the digit become 0, and rolling-down at 0 will make the digit become 9. Since there are only 10000 possible keys, 0000 through 9999, anyone can try all the combinations until the safe is unlocked.



<!-- MATH
$\epsfbox{p4138.eps}$
-->
WIDTH="549" HEIGHT="166" ALIGN="BOTTOM" BORDER="0"
SRC="/ntujudge/problemdata/628.jpg"
ALT="\epsfbox{p4138.eps}">

What's done is done. But in order to slow down future robbers' attack, Panda Security Agency (PSA) has devised a new safer lock with multiple keys. Instead of using only one key combination as the key, the lock now can have up to N
keys which has to be all unlocked before the safe can be opened. These locks works as the following:

  • Initially the digits are at 0000.
  • Keys can be unlocked in any order, by setting the digits in the lock to match the desired key and then pressing the UNLOCK button.
  • A magic JUMP button can turn the digits into any of the unlocked keys without doing any rolling.
  • The safe will be unlocked if and only if all the keys are unlocked in a minimum total amount of rolling, excluding JUMP (yes, this feature is the coolest one).
  • If the number of rolling is exceeded, then the digits will be reset to 0000 and all the keys will be locked again. In other word, the state of the lock will be reset the cracking is failed.




PSA is quite confident that this new system will slow down the cracking, giving them enough time to identify and catch the robbers. In order to determine the minimum number of rolling needed, PSA wants you to write a program. Given all the keys, calculate the minimum number of rolls needed to unlock the safe.

Input Format


The first line of input contains an integer T

, the number of test cases follow. Each case begins with an integer N
<!-- MATH
$(1 \le N \le 500)$
-->
(1 WIDTH="18" HEIGHT="31" ALIGN="MIDDLE" BORDER="0"
SRC="/ntujudge/problemdata/628-2.png"
ALT="$ \le$">N WIDTH="18" HEIGHT="31" ALIGN="MIDDLE" BORDER="0"
SRC="/ntujudge/problemdata/628-2.png"
ALT="$ \le$">500)

, the number of keys. The next N
lines, each contains exactly four digits number (leading zero allowed) representing the keys to be unlocked.

Output Format


For each case, print in a single line the minimum number of rolls needed to unlock all the keys.

Sample Input 1



4
2 1155 2211
3 1111 1155 5511
3 1234 5678 9090
4 2145 0213 9113 8113



Sample Output 1



16
20
26
17



Hints

Problem Source

Migrated from old NTUJ.

ACM ICPC regional Jakarta site 2008

Subtasks

No. Testdata Range Score

Testdata and Limits

No. Time Limit (ms) Memory Limit (VSS, KiB) Output Limit (KiB) Subtasks
0 10000 65536 2000