Similar Words

Time limit4sMemory limit512 MB

Summary
Given a set of distinct words, choose as many prefixes as possible so that no two chosen words differ by deleting one leading letter.
Level

Hard8 of 10

Topics
Trie, Tree, Dynamic programming, Greedy
Solved
No attempts yet

Problem

A non-empty sequence of lowercase English letters is called a word. A prefix of a word xx is a word yy that can be obtained from xx by removing zero or more of its last letters.

Two words are called similar if one of them can be obtained from the other by removing its first letter.

You are given a set SS of words. Find the maximal possible size of a set XX of non-empty words such that:

  • every word in XX is a prefix of some word in SS;
  • XX contains no two similar words.

Input

The input contains multiple test cases. The first line contains an integer tt, the number of test cases. The descriptions of the test cases follow.

The first line of each description contains an integer nn, the number of words in the set SS (1≤n≤1061 \le n \le 10^6). Each of the following nn lines contains one non-empty word, an element of SS. All words in SS are distinct.

The total length of all words in one input does not exceed 10610^6.

Output

For each test case, print one line containing one integer mm, the maximal number of words that XX can contain.

Examples1

  1. Example 1

    Input
    2
    3
    aba
    baba
    aaab
    2
    aa
    a
    
    Expected output
    6
    1