---
{"category_name":"easy","problem_code":"LNGSUB","problem_name":"Longest Spanning Substrings","problemComponents":{"constraints":"- $1 \\leq T \\leq 100$\n- $1 \\leq N \\leq 100$\n- for each valid $i$, $S_i$ contains only lowercase English letters\n- the sum of lengths of all strings over all test cases does not exceed $5 \\cdot 10^5$\n","constraintsState":true,"subtasks":"**Subtask #1 (10 points):** the sum of lengths of all strings over all test cases does not exceed $5 \\cdot 10^2$\n\n**Subtask #2 (20 points):** the sum of lengths of all strings over all test cases does not exceed $5 \\cdot 10^3$\n\n**Subtask #3 (70 points):** original constraints\n","subtasksState":true,"inputFormat":"- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.\n- The first line of each test case contains a single integer $N$.\n- $N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $S_i$.\n","inputFormatState":true,"outputFormat":"For each test case, print a single line containing one integer --- the maximum weight of a spanning tree.\n","outputFormatState":true,"sampleTestCases":{"0":{"id":1,"input":"3\n3\nbbcab\naa\naab\n3\nbbc\nbcaa\nabb\n3\naa\nacc\nabbca\n","output":"4\n4\n2\n","explanation":"**Example case 1:** The strings $S_2, S_3$ have a longest common substring with length $2$, and the strings $S_1, S_3$ have a longest common substring with length $2$. The spanning tree which contains the edges $(2,3)$ and $(1,3)$ has the maximum weight $4$.\n","isDeleted":false}}},"video_editorial_url":"","languages_supported":{"0":"CPP14","1":"C","2":"JAVA","3":"PYTH 3.6","4":"CPP17","5":"PYTH","6":"PYP3","7":"CS2","8":"ADA","9":"PYPY","10":"TEXT","11":"PAS fpc","12":"NODEJS","13":"RUBY","14":"PHP","15":"GO","16":"HASK","17":"TCL","18":"PERL","19":"SCALA","20":"LUA","21":"kotlin","22":"BASH","23":"JS","24":"LISP sbcl","25":"rust","26":"PAS gpc","27":"BF","28":"CLOJ","29":"R","30":"D","31":"CAML","32":"FORT","33":"ASM","34":"swift","35":"FS","36":"WSPC","37":"LISP clisp","38":"SQL","39":"SCM guile","40":"PERL6","41":"ERL","42":"CLPS","43":"ICK","44":"NICE","45":"PRLG","46":"ICON","47":"COB","48":"SCM chicken","49":"PIKE","50":"SCM qobi","51":"ST","52":"SQLQ","53":"NEM"},"max_timelimit":1,"source_sizelimit":50000,"problem_author":"theoneyouwant","problem_tester":"","date_added":"25-08-2021","tags":{"0":"theoneyouwant"},"problem_difficulty_level":"Easy-Medium","best_tag":"","editorial_url":"","time":{"view_start_date":1630170002,"submit_start_date":1630170002,"visible_start_date":1630170002,"end_date":1735669800},"is_direct_submittable":false,"problemDiscussURL":"https://discuss.codechef.com/search?q=LNGSUB","is_proctored":false,"visitedContests":{},"layout":"problem"}
---
### Read problem statements in [Mandarin](https://www.codechef.com/download/translated/LTIME99/mandarin/LNGSUB.pdf), [Bengali](https://www.codechef.com/download/translated/LTIME99/bengali/LNGSUB.pdf), [Russian](https://www.codechef.com/download/translated/LTIME99/russian/LNGSUB.pdf), and [Vietnamese](https://www.codechef.com/download/translated/LTIME99/vietnamese/LNGSUB.pdf) as well.
You are given $N$ strings $S_1, S_2, \ldots, S_N$. Consider a complete undirected graph with $N$ vertices (numbered $1$ through $N$), in which the weight of an edge between vertices $u$ and $v$ is equal to the length of the longest common substring of $S_u$ and $S_v$.
Find the maximum possible weight of a spanning tree of this graph.
<aside style='background: #f8f8f8;padding: 10px 15px;'><div>All submissions for this problem are available.</div></aside>