Sending Substrings

시간 제한6초메모리 제한2048 MB

요약
n개 팀 이름이 주어질 때, 서로 다른 두 팀의 순서 있는 쌍마다 두 이름 모두의 부분문자열인 서로 다른 비어 있지 않은 문자열의 개수를 세어 합한다.
난이도

보통10점 중 7점

유형
문자열, 트라이, 누적 합, 구현
정답자
아직 제출이 없습니다

문제

You are a participant of the final round of the prestigious International Code Printing Contest (ICPC). Upon arriving for the trial round, you discovered that, unlike regular programming competitions, any team can send code to be printed for any other team!

The organizers do not want to incur additional expenses for paper or to check messages for unauthorized hints to problem solutions. Therefore, the following conditions must be met for printing text to another team:

  • if SS is the name of the sending team, and TT is the name of the receiving team, then the text sent for printing must be a non-empty substring of both SS and TT;
  • a team cannot send the same message to the same other team twice.

Solving problems in the competition is great, but unexpectedly sending messages to opponents is also fun. You wondered what is the maximum number of messages that can be transmitted between different teams during the contest in this way.

If a message was transmitted between several ordered pairs of teams, it should be counted in the answer the corresponding number of times. Messages printed by a team for itself should not be considered in the answer.

입력

The first line contains an integer TT (1≤T≤1051 \leq T \leq 10^5), denoting the number of test cases.

Then TT descriptions of test cases follow. The first line of the description contains an integer nn (1≤n≤1051 \leq n \leq 10^5), denoting the number of teams.

Each of the following nn lines of the description contains a string SS, consisting only of lowercase Latin letters (1≤∣S∣≤105)1 \leq |S| \leq 10^5), denoting the name of the next team. The names of the teams may be the same.

It is guaranteed that the total number of teams in all test cases does not exceed 10510^5 and that the sum of the lengths of the team names in all test cases does not exceed 5⋅1055 \cdot 10^5.

출력

For each test case, output a single integer: the maximum number of messages that can be transmitted between different teams.

힌트

In the first test case, each team can send the following strings to another team: strings a, ab, b, c.

In the second test case, strings a, d, f, i, m, n, o, re, t, un, um can be sent in two ways, strings e, r, s can be sent in six ways, and string u can be sent in twelve ways.

예제1

  1. 예제 1

    입력
    2
    2
    abacaba
    abc
    5
    dungeonthread
    mediumrare
    bsunumberseven
    sostuffy
    fiksiki
    
    예상 출력
    8
    52