public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder out = new StringBuilder();
		int nCases = Integer.parseInt(in.readLine().trim());
		for (int nCase = 0; nCase < nCases; nCase++) {
			int[] nk = readInts(in.readLine());
			int n = nk[0], k = nk[1];
			ArrayList<String> words = new ArrayList<>();
			String word;
			int length = 0;
			for (int i = 0; i < k; i++) {
				word = in.readLine().trim();
				words.add(word);
				length += word.length();
			}
			t = new char[length];
			int index = 0;
			for (int i = 0; i < k; i++) {
				char[] p = words.get(i).toCharArray();
				preprocess(p);
				int save = KMP(p, t, Math.max(0, index - p.length), index);
				for (int j = save; j < p.length; j++)
					t[index++] = p[j];
			}
			out.append(index + "\n");
		}
		System.out.print(out);
	}