// Arup Guha
// 4/10/2021
// Solution to 2021 Code Jam Round 1A Problem A: Append Sort

import java.util.*;

public class Solution {
	
	public static void main(String[] args) {
	
		Scanner stdin = new Scanner(System.in);
		int nC = stdin.nextInt();
		
		// Process all cases.
		for (int loop=1; loop<=nC; loop++) {
		
			// Store our numbers.
			int n = stdin.nextInt();
			char[][] list = new char[n][];
			for (int i=0; i<n; i++)
				list[i] = stdin.next().toCharArray();
				
			int res = 0;
			
			// For each consecutive pair, greedily make the second one larger than the first
			// but as small as possible.
			for (int i=1; i<n; i++) {
				char[] tmp = go(list[i-1], list[i]);
				res += (tmp.length-list[i].length);
				list[i] = tmp;
			}
			
			// Ta da!
			System.out.println("Case #"+loop+": "+res);
		}
	}
	
	// Returns the smallest integer greater than a with a prefix of b.
	public static char[] go(char[] a, char[] b) {
	
		// b is bigger without looking at the digits.
		if (a.length < b.length) return b;
		
		// Lengths are the same, we have to look at the values.
		if (a.length == b.length) {
		
			// Find the first non-equal digit and see which is bigger.
			boolean ok = false;
			for (int i=0; i<a.length; i++) {
				if (a[i] > b[i]) break;
				if (a[i] < b[i]) {
					ok = true; 
					break;
				}
			}
			
			// b is already bigger return it.
			if (ok) return b;
			
			// b's not bigger but adding 0 will make it bigger.
			char[] res = new char[b.length+1];
			for (int i=0; i<b.length; i++) res[i] = b[i];
			res[b.length] = '0';
			return res;
		}
		
		// Case b.length < a.length
		
		// code = 0 means equal prefixes, 1 means b > a, 2 means a > b.
		int code = 0;
		for (int i=0; i<b.length; i++) {
			if (b[i] > a[i]) {
				code = 1;
				break;
			}
			if (b[i] < a[i]) {
				code = 2;
				break;
			}
		}
		
		// Prefixes are not equal.
		if (code > 0) {
			
			// Depending on code the end length of our answer is either the length of a, or 1
			// more than it.
			int size = code == 1 ? a.length : a.length+1;
			char[] res = new char[size];
			
			// Copy prefix and pad with 0s.
			for (int i=0; i<b.length; i++) 		res[i] = b[i];
			for (int i=b.length; i<size; i++)	res[i] = '0';
			return res;
		}
		
		// See if the numbers that remain in a have all 9s.
		boolean all9s = true;
		for (int i=b.length; i<a.length; i++)
			if (a[i] != '9')
				all9s = false;
				
		// If we have all 9s, we can't get another number bigger with the same # of digits, so 
		// just add zeros to b.
		if (all9s) {
			char[] res = new char[a.length+1];
			for (int i=0; i<b.length; i++) 			res[i] = b[i];
			for (int i=b.length; i<res.length; i++)	res[i] = '0';
			return res;
		}
		
		// Add 1 to a.
		
		// Copy a into res.
		char[] res = new char[a.length];
		for (int i=0; i<a.length; i++) res[i] = a[i];
		
		// Algorithm to add 1.
		int i = res.length-1;
		while (res[i] == '9') {
			res[i] = '0';
			i--;
		}
		
		// Add 1 to this digit without a carry and return!
		res[i] = (char)(res[i] + 1);
		return res;
	}
}