|
|||
Anti Brute Force LockLately, there is a serious problem with Panda Land Safe Box: several safes have been robbed! The safes are using old 4-digit 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.
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, all of which have to be unlocked before the safe can be opened. These locks work as follows:
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 SpecificationThe first line of the input contains an integer T, the number of test cases that follow. Each case consists of a line, beginning with an integer N (1 ≤ N ≤ 500), the number of keys, followed by N numbers, each containing exactly four digits (leading zeros allowed), representing the keys to be unlocked. Output SpecificationFor each case, print in a single line the minimum number of rolls needed to unlock all the keys. Sample Input
Output for Sample Input
Explanation for the 2nd case:
Total rolls: 4 + 8 + 8 = 20. |
|||
University of Debrecen; Faculty of Informatics; v. 09/30/2024 |