Contents

• For Solution

Utkarsh is a cool guy and had fun throughout the year. Now his exams are approaching and he hasn’t studied for them at all!

Utkarsh has to read several chapters for his exams, but he is smart and realized that he doesn’t actually need to read each and every one of them thoroughly. Several chapters share the same content, so he can skip some parts of some chapters to save time.

Each chapter is a non-empty string of lowercase characters. Utkarsh has to read NN distinct chapters for the exam, denoted by the set S={S1,S2,,SN}S={S1,S2,…,SN}. He will read all NN chapters in some order.

Help Utkarsh solution codechef

For each chapter SiSi, he can skip reading some part of it by considering at most two other chapters that he has read already.

Formally, he has 33 options for the chapter Si:Si:

1. Suppose there exist two chapters Sj,SkSSj,Sk∈S (jj can be equal to kk) that he has previously read, and three (possibly empty) strings of lowercase alphabets AABB, and CC such that Si=A+Sj+B+Sk+CSi=A+Sj+B+Sk+C (where ++ denotes string concatenation). Then he has to read only |A|+|B|+|C||A|+|B|+|C| characters.

2. Suppose there exists a chapter SjSSj∈S that he has previously read, along with two (possibly empty) strings AA and BB of lowercase alphabets such that Si=A+Sj+BSi=A+Sj+B. Then he has to read only |A|+|B||A|+|B| characters.

3. Finally, he can also choose to read chapter SiSi completely, requiring |Si||Si| characters.

Note that there is no forced hierarchy among the options, i.e, Utkarsh is free to choose how he reads SiSi chapter among all choices available to him.

Find the minimum amount of characters Utkarsh has to read in total if he chooses an optimal ordering of the chapters.

Help Utkarsh solution codechef

• The first line of input contains a single integer TT, the number of test cases. The description of TT test cases follows.
• Each test case consists of N+1N+1 lines.
• The first line contains a single integer NN, the number of chapters.
• Each of the following NN lines contains a non-empty lowercase string.

Help Utkarsh solution codechef

For each test case, print a single line containing one integer denoting the minimum number of characters Utkarsh has to read over all chapters.

Constraints

• 1T1031≤T≤103
• 1N1051≤N≤105
• 1|Si|31051≤|Si|≤3⋅105
• Sum of |Si||Si| over all test cases doesn’t exceed 21062⋅106.

3
5
a
aba
aabc
bc
c
2
aaa
bb
3
z
zz
zzz

5
5
1

Help Utkarsh solution codechef

Test case 1:

Ukarsh reads the chapters in the following order :

Thus, 55 characters are read in total.

Test case 2:

Ukarsh reads the chapters in the following order :

Thus 55 characters are read in total.

Test case 3:

Ukarsh reads the chapters in the following order :