#include<functional>
#include <algorithm>
#include  <iostream>
#include   <ctype.h>
#include   <cstring>
#include   <fstream>
#include    <cstdio>
#include    <vector>
#include    <string>
#include     <cmath>
#include     <deque>
#include     <stack>
#include     <queue>
#include      <list>
#include       <set>
#include       <map>
#define INF 0X7FFFFFFF
#define PI acos(-1.0)
typedef long long ll;
using namespace std;
const int N = 1e6 + 10;
inline int getint()
{
	int w = 0, q = 0;
	char c = getchar();
	while ((c<'0' || c>'9') && c != '-') c = getchar();
	if (c == '-') q = 1, c = getchar();
	while (c >= '0'&&c <= '9') w = w * 10 + c - '0', c = getchar();
	return q ? -w : w;
}
struct Node {
	int next[26];
};

string str;
Node node[N];
int o;
int n;
int newpoint() {
	o++;
	memset(node[o].next, -1, sizeof(node[o].next));
	return o;
}
void updata(int &rt, int t) {
	if (rt == -1) {
		rt = newpoint();
	}
	if (t == str.size()) {
		return;
	}
	updata(node[rt].next[str[t] - 'a'], t + 1);
}
int ans;
vector<int> e[N];
int sum[N][2];
int u[N];
void dfs(int m,int p) {
	if (p > 0) {
		u[m] = 1;
		e[p].push_back(m);
	}
	for (int i = 0; i < 26; i++) {
		if (node[m].next[i] != -1 && node[p].next[i] != -1) {
			dfs(node[m].next[i], node[p].next[i]);
		}
	}
}
void dp(int v) {
	sum[v][0] = 0;
	sum[v][1] = 1;
	for (int i = 0; i < e[v].size(); i++) {
		dp(e[v][i]);
		sum[v][0] += max(sum[e[v][i]][1], sum[e[v][i]][0]);
		sum[v][1] += sum[e[v][i]][0];
	}
}
int main() {
	int T = getint();
	while (T--) {
		n = getint();
		int head = 0;
		o = 0;
		memset(node[0].next, -1, sizeof(node[0].next));
		for (int i = 0; i < n; i++) {
			cin >> str;
			updata(head, 0);
		}

		for (int i = 0; i < 26; i++) {
			if (node[0].next[i] != -1) {
				dfs(node[0].next[i], 0);
			}
		}
		ans = 0;
		for (int i = 1; i <= o; i++) {
			if (!u[i]) {
				dp(i);
				ans += max(sum[i][0], sum[i][1]);
			}
		}
		printf("%d\n", ans);
		for (int i = 0; i <= o; i++) {
			e[i].clear();
			u[i] = 0;
			sum[i][0] = 0;
			sum[i][1] = 0;
		}
	}
	return 0;
}


//Relive your past life.
//Face your demons.
//The past is never dead,it is not even past.
//The memories are not only the key to the past but...also to the future.
//coded in Rusty Lake
#include<cmath>
#include<math.h>
#include<ctype.h>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cerrno>
#include<cfloat>
#include<ciso646>
#include<climits>
#include<clocale>
#include<complex>
#include<csetjmp>
#include<csignal>
#include<cstdarg>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cwchar>
#include<cwctype>
#include<deque>
#include<exception>
#include<fstream>
#include<functional>
#include<iomanip>
#include<ios>
#include<iosfwd>
#include<iostream>
#include<istream>
#include<iterator>
#include<limits>
#include<list>
#include<locale>
#include<map>
#include<memory>
#include<new>
#include<numeric>
#include<ostream>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<stdexcept>
#include<streambuf>
#include<string>
#include<typeinfo>
#include<utility>
#include<valarray>
#include<vector>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#define ll   long long
#define pb   push_back
#define mp   make_pair
#define orz  1000000007
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
int T,n,ch[1000006][26],k,len[1000005],f[1000005],q[1000005],dp[1000005][2];
vector<int> v[1000005];
void dfs(int x,int y){
    dp[x][1]=1;
    for(int i=0;i<v[x].size();++i){
        int z=v[x][i];
        if(z==y) continue;
        dfs(z,x);
        dp[x][0]+=max(dp[z][0],dp[z][1]);
        dp[x][1]+=dp[z][0];
    }
}
void solve(){
    while(k>=0){
        memset(ch[k],0,sizeof(ch[k]));
        len[k]=f[k]=0;
        --k;
    }
    k=0;
    scanf("%d",&n);
    for(int i=1;i<=n;++i){
        int o=0;
        char c=getchar();
        while(c<97)c=getchar();
        while(c>96){
            int p=c-97;
            if(!ch[o][p])len[ch[o][p]=++k]=len[o]+1;
            o=ch[o][p],c=getchar();
        }
    }
    int qr=0;
    for(int i=0;i<26;++i)if(ch[0][i])q[++qr]=ch[0][i];
    for(int ql=1;ql<=qr;++ql){
        int o=q[ql];
        for(int i=0;i<26;++i){
            if(ch[o][i])f[ch[o][i]]=ch[f[o]][i],q[++qr]=ch[o][i];
            else ch[o][i]=ch[f[o]][i];
        }
    }
    for(int i=1;i<=k;++i)v[i].clear(),dp[i][0]=dp[i][1]=0;
    for(int i=1;i<=k;++i)if(f[i]&&len[f[i]]==len[i]-1)v[f[i]].pb(i);
    int ans=0;
    for(int i=1;i<=k;++i){
        if(f[i]&&len[f[i]]==len[i]-1) continue;
        dfs(i,0);
        ans+=max(dp[i][0],dp[i][1]);
    }
    printf("%d\n",ans);
}
int main(){
    scanf("%d",&T);
    while(T--)solve();
    //system("pause");
    return 0;
}
//hello


#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<map>
#include<vector>
#include<ctime>
#include<stack>
#include<cctype>
#include<set>
#define mp make_pair
#define pa pair<int,int>
#define INF 0x3f3f3f3f
#define inf 0x3f
#define fi first
#define se second
#define pb push_back
#define ll long long
#define ull unsigned long long

using namespace std;

inline ll read()
{
	long long f=1,sum=0;
	char c=getchar();
	while (!isdigit(c)){if (c=='-') f=-1;c=getchar();}
	while (isdigit(c)){sum=sum*10+c-'0';c=getchar();}
	return sum*f;
}
const int MAXN=1000010;
const int CHARSET=26;
int rt,size,fail[MAXN*CHARSET],tr[MAXN][CHARSET],deep[MAXN*CHARSET];
void new_node(int x,int ch)
{
	tr[x][ch]=++size;
	memset(tr[size],0,sizeof(tr[size]));
	fail[size]=0;
	deep[size]=deep[x]+1;
}
void insert(char s[])
{
	int now=rt,len=strlen(s);
	for (int i=0;i<len;i++)
	{
		int x=s[i]-'a';
		if (!tr[now][x])
			new_node(now,x);
		now=tr[now][x];
	}
}
queue <int> q;
void get_fail()
{
	while (!q.empty()) q.pop();
	for (int i=0;i<CHARSET;i++)
		if (tr[rt][i]) fail[tr[rt][i]]=rt,q.push(tr[rt][i]);
		else tr[rt][i]=rt;
	while (!q.empty())
	{
		int x=q.front();
		q.pop();
		for (int i=0;i<CHARSET;i++)
			if (tr[x][i]) fail[tr[x][i]]=tr[fail[x]][i],q.push(tr[x][i]);
			else tr[x][i]=tr[fail[x]][i];
	}
}
char s[MAXN];
struct edge
{
	int next,to;
};
edge e[MAXN*2];
int head[MAXN],cnt;
void init()
{
	rt=size=0;
	memset(tr[size],0,sizeof(tr[size]));
	fail[size]=0;
	memset(head,0,sizeof(head));
	cnt=0;
}
void addedge(int u,int v)
{
	e[++cnt].next=head[u];
	e[cnt].to=v;
	head[u]=cnt;
}
int f[MAXN][2];
void dfs(int x)
{
	f[x][0]=1,f[x][1]=0;
	for (int i=head[x];i;i=e[i].next)
	{
		int v=e[i].to;
		dfs(v);
		f[x][0]+=f[v][1];
		f[x][1]+=max(f[v][0],f[v][1]);
	}
}
int main()
{
	int T;
	scanf("%d",&T);
	while (T--)
	{
		init();
		int n;
		scanf("%d",&n);
		for (int i=1;i<=n;i++)
			scanf("%s",s),insert(s);
		get_fail();
		for (int i=1;i<=size;i++)
			if (deep[fail[i]]==deep[i]-1)
				addedge(fail[i],i);
			else
				addedge(0,i);
		dfs(0);
		cout<<f[0][1]<<endl;
	}
	return 0;
}


#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<map>
#include<vector>
#include<ctime>
#include<stack>
#include<cctype>
#include<set>
#define mp make_pair
#define pa pair<int,int>
#define INF 0x3f3f3f3f
#define inf 0x3f
#define fi first
#define se second
#define pb push_back
#define ll long long
#define ull unsigned long long

using namespace std;

inline ll read()
{
	long long f=1,sum=0;
	char c=getchar();
	while (!isdigit(c)){if (c=='-') f=-1;c=getchar();}
	while (isdigit(c)){sum=sum*10+c-'0';c=getchar();}
	return sum*f;
}
const int MAXN=1000010;
const int CHARSET=26;
int rt,size,fail[MAXN*CHARSET],tr[MAXN][CHARSET],deep[MAXN*CHARSET];
void new_node(int x,int ch)
{
	tr[x][ch]=++size;
	memset(tr[size],0,sizeof(tr[size]));
	fail[size]=0;
	deep[size]=deep[x]+1;
}
void insert(char s[])
{
	int now=rt,len=strlen(s);
	for (int i=0;i<len;i++)
	{
		int x=s[i]-'a';
		if (!tr[now][x])
			new_node(now,x);
		now=tr[now][x];
	}
}
queue <int> q;
void get_fail()
{
	while (!q.empty()) q.pop();
	for (int i=0;i<CHARSET;i++)
		if (tr[rt][i]) fail[tr[rt][i]]=rt,q.push(tr[rt][i]);
		else tr[rt][i]=rt;
	while (!q.empty())
	{
		int x=q.front();
		q.pop();
		for (int i=0;i<CHARSET;i++)
			if (tr[x][i]) fail[tr[x][i]]=tr[fail[x]][i],q.push(tr[x][i]);
			else tr[x][i]=tr[fail[x]][i];
	}
}
char s[MAXN];
struct edge
{
	int next,to;
};
edge e[MAXN*2];
int head[MAXN],cnt;
void init()
{
	rt=size=0;
	memset(tr[size],0,sizeof(tr[size]));
	fail[size]=0;
	memset(head,0,sizeof(head));
	cnt=0;
}
void addedge(int u,int v)
{
	e[++cnt].next=head[u];
	e[cnt].to=v;
	head[u]=cnt;
}
int f[MAXN][2];
void dfs(int x)
{
	f[x][0]=1,f[x][1]=0;
	for (int i=head[x];i;i=e[i].next)
	{
		int v=e[i].to;
		dfs(v);
		f[x][0]+=f[v][1];
		f[x][1]+=max(f[v][0],f[v][1]);
	}
}
int main()
{
	int T;
	scanf("%d",&T);
	while (T--)
	{
		size=0,rt=0;
		int n;
		scanf("%d",&n);
		for (int i=1;i<=n;i++)
			scanf("%s",s),insert(s);
		get_fail();
		for (int i=1;i<=size;i++)
			if (deep[fail[i]]==deep[i]-1)
				addedge(fail[i],i);
			else
				addedge(0,i);
		dfs(0);
		for (int i=0;i<=size;i++)
		{
			head[i]=fail[i]=0;
			memest(tr[i],0,sizeof(tr[i]));
		}
	}
	return 0;
}


#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<map>
#include<vector>
#include<ctime>
#include<stack>
#include<cctype>
#include<set>
#define mp make_pair
#define pa pair<int,int>
#define INF 0x3f3f3f3f
#define inf 0x3f
#define fi first
#define se second
#define pb push_back
#define ll long long
#define ull unsigned long long

using namespace std;

inline ll read()
{
	long long f=1,sum=0;
	char c=getchar();
	while (!isdigit(c)){if (c=='-') f=-1;c=getchar();}
	while (isdigit(c)){sum=sum*10+c-'0';c=getchar();}
	return sum*f;
}
const int MAXN=1000010;
const int CHARSET=26;
int rt,size,fail[MAXN*CHARSET],tr[MAXN][CHARSET],deep[MAXN*CHARSET];
void new_node(int x,int ch)
{
	tr[x][ch]=++size;
	memset(tr[size],0,sizeof(tr[size]));
	fail[size]=0;
	deep[size]=deep[x]+1;
}
void insert(char s[])
{
	int now=rt,len=strlen(s);
	for (int i=0;i<len;i++)
	{
		int x=s[i]-'a';
		if (!tr[now][x])
			new_node(now,x);
		now=tr[now][x];
	}
}
queue <int> q;
void get_fail()
{
	while (!q.empty()) q.pop();
	for (int i=0;i<CHARSET;i++)
		if (tr[rt][i]) fail[tr[rt][i]]=rt,q.push(tr[rt][i]);
		else tr[rt][i]=rt;
	while (!q.empty())
	{
		int x=q.front();
		q.pop();
		for (int i=0;i<CHARSET;i++)
			if (tr[x][i]) fail[tr[x][i]]=tr[fail[x]][i],q.push(tr[x][i]);
			else tr[x][i]=tr[fail[x]][i];
	}
}
char s[MAXN];
struct edge
{
	int next,to;
};
edge e[MAXN*2];
int head[MAXN],cnt;
void init()
{
	rt=size=0;
	memset(tr[size],0,sizeof(tr[size]));
	fail[size]=0;
	memset(head,0,sizeof(head));
	cnt=0;
}
void addedge(int u,int v)
{
	e[++cnt].next=head[u];
	e[cnt].to=v;
	head[u]=cnt;
}
int f[MAXN][2];
void dfs(int x)
{
	f[x][0]=1,f[x][1]=0;
	for (int i=head[x];i;i=e[i].next)
	{
		int v=e[i].to;
		dfs(v);
		f[x][0]+=f[v][1];
		f[x][1]+=max(f[v][0],f[v][1]);
	}
}
int main()
{
	int T;
	scanf("%d",&T);
	while (T--)
	{
		size=0,rt=0;
		int n;
		scanf("%d",&n);
		for (int i=1;i<=n;i++)
			scanf("%s",s),insert(s);
		get_fail();
		for (int i=1;i<=size;i++)
			if (deep[fail[i]]==deep[i]-1)
				addedge(fail[i],i);
			else
				addedge(0,i);
		dfs(0);
		for (int i=0;i<=size;i++)
		{
			head[i]=fail[i]=0;
			memset(tr[i],0,sizeof(tr[i]));
		}
	}
	return 0;
}


#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<map>
#include<vector>
#include<ctime>
#include<stack>
#include<cctype>
#include<set>
#define mp make_pair
#define pa pair<int,int>
#define INF 0x3f3f3f3f
#define inf 0x3f
#define fi first
#define se second
#define pb push_back
#define ll long long
#define ull unsigned long long

using namespace std;

inline ll read()
{
	long long f=1,sum=0;
	char c=getchar();
	while (!isdigit(c)){if (c=='-') f=-1;c=getchar();}
	while (isdigit(c)){sum=sum*10+c-'0';c=getchar();}
	return sum*f;
}
const int MAXN=1000010;
const int CHARSET=26;
int rt,size,fail[MAXN*CHARSET],tr[MAXN][CHARSET],deep[MAXN*CHARSET];
void new_node(int x,int ch)
{
	tr[x][ch]=++size;
	memset(tr[size],0,sizeof(tr[size]));
	fail[size]=0;
	deep[size]=deep[x]+1;
}
void insert(char s[])
{
	int now=rt,len=strlen(s);
	for (int i=0;i<len;i++)
	{
		int x=s[i]-'a';
		if (!tr[now][x])
			new_node(now,x);
		now=tr[now][x];
	}
}
queue <int> q;
void get_fail()
{
	while (!q.empty()) q.pop();
	for (int i=0;i<CHARSET;i++)
		if (tr[rt][i]) fail[tr[rt][i]]=rt,q.push(tr[rt][i]);
		else tr[rt][i]=rt;
	while (!q.empty())
	{
		int x=q.front();
		q.pop();
		for (int i=0;i<CHARSET;i++)
			if (tr[x][i]) fail[tr[x][i]]=tr[fail[x]][i],q.push(tr[x][i]);
			else tr[x][i]=tr[fail[x]][i];
	}
}
char s[MAXN];
struct edge
{
	int next,to;
};
edge e[MAXN*2];
int head[MAXN],cnt;
void init()
{
	rt=size=0;
	memset(tr[size],0,sizeof(tr[size]));
	fail[size]=0;
	memset(head,0,sizeof(head));
	cnt=0;
}
void addedge(int u,int v)
{
	e[++cnt].next=head[u];
	e[cnt].to=v;
	head[u]=cnt;
}
int f[MAXN][2];
void dfs(int x)
{
	f[x][0]=1,f[x][1]=0;
	for (int i=head[x];i;i=e[i].next)
	{
		int v=e[i].to;
		dfs(v);
		f[x][0]+=f[v][1];
		f[x][1]+=max(f[v][0],f[v][1]);
	}
}
int main()
{
	int T;
	scanf("%d",&T);
	while (T--)
	{
		size=0,rt=0;
		int n;
		scanf("%d",&n);
		for (int i=1;i<=n;i++)
			scanf("%s",s),insert(s);
		get_fail();
		for (int i=1;i<=size;i++)
			if (deep[fail[i]]==deep[i]-1)
				addedge(fail[i],i);
			else
				addedge(0,i);
		dfs(0);
		cout<<f[0][1]<<endl;
		for (int i=0;i<=size;i++)
		{
			head[i]=fail[i]=0;
			memset(tr[i],0,sizeof(tr[i]));
		}
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int M = 1000005;
int read()
{
    int x=0,flag=1;
    char c;
    while((c=getchar())<'0' || c>'9') if(c=='-') flag=-1;
    while(c>='0' && c<='9') x=(x<<3)+(x<<1)+(c^48),c=getchar();
    return x*flag;
}
int T,n,m;char s[M];string t[M];
struct edge
{
    int v,next;
    edge(int V=0,int N=0) : v(V) , next(N) {}
};
struct trie
{
    int cnt,tot,f[M],ch[M][26],dp[M][2];
    edge e[2*M];int flag[M];
    void init()
    {
        cnt=1;tot=0;
        memset(ch[1],0,sizeof ch[1]);
    }
    int newnode()
    {
        cnt++;
        memset(ch[cnt],0,sizeof ch[cnt]);
        return cnt;
    }
    void add()
    {
        m=strlen(s);
        for(int i=0,p=1;i<m;i++)
        {
            int c=s[i]-'a';
            if(!ch[p][c]) ch[p][c]=newnode();
            p=ch[p][c];
        }
    }
    void link(int u,int v)
    {
        e[++tot]=edge(v,f[u]),f[u]=tot;
        e[++tot]=edge(u,f[v]),f[v]=tot;
    }
    void dfs(int u)
    {
        flag[u]=1;
        dp[u][0]=0;dp[u][1]=1;
        for(int i=f[u];i;i=e[i].next)
        {
            int v=e[i].v;
            if(flag[v]) continue;
            dfs(v);
            dp[u][1]+=dp[v][0];
            dp[u][0]+=max(dp[v][0],dp[v][1]);
        }
    }
    void build()
    {
        for(int i=1;i<=cnt;i++)
            f[i]=flag[i]=0;
        for(int i=1;i<=n;i++)
        {
            int p1=1,p2=ch[1][t[i][0]-'a'];
            m=t[i].length();
            for(int j=1;j<m;j++)
            {
                int c=t[i][j]-'a';
                p1=ch[p1][c];
                p2=ch[p2][c];
                if(!p1) break;
                link(p1,p2);
            }
        }
        int ans=0;
        for(int i=2;i<=cnt;i++)//一定要这么写哟
        {
            if(!flag[i])
            {
                dfs(i);
                ans+=max(dp[i][0],dp[i][1]);
            }
        }
        printf("%d\n",ans);
    }
}tr;
int main()
{
    T=read();
    while(T--)
    {
        tr.init();
        n=read();
        for(int i=1;i<=n;i++)
        {
            scanf("%s",s);
            tr.add();
            t[i]=s;
        }
        tr.build();
    }
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long

const int mxN=1e6, Ms=2;
const ll M[Ms]={1000000007, 1000696969}, B1=29, B2=31;

struct hs {
	ll a[Ms]={};
	hs operator*(const ll &o) const {
		hs r;
		for(int i=0; i<Ms; ++i)
			r.a[i]=(a[i]*o)%M[i];
		return r;
	}
	hs operator+(const ll &o) const {
		hs r;
		for(int i=0; i<Ms; ++i)
			r.a[i]=(a[i]+o)%M[i];
		return r;
	}
	bool operator==(const hs &o) const {
		for(int i=0; i<Ms; ++i)
			if(a[i]!=o.a[i])
				return 0;
		return 1;
	}
};

namespace std {
	template<>
	struct hash<hs> {
		inline size_t operator()(const hs &o) const {
			size_t r=0;
			for(int i=0; i<Ms; ++i)
				r=r*B2+o.a[i];
			return r;
		}
	};
}

vector<int> graph[mxN];
bool vis[mxN];
int ans;
string ss[mxN];
unordered_map<hs, int> mp;

bool dfs(int u) {
	vis[u]=true;
	bool childMarked=0;
	for(int v : graph[u])
		if(!vis[v])
			childMarked|=dfs(v);
	if(!childMarked)
		++ans;
	return !childMarked;
}

inline void solve() {
	int n, n2=0;
	cin >> n;
	for(int i=0; i<n; ++i) {
		cin >> ss[i];
		hs h;
		for(int j=0; j<ss[i].size(); ++j) {
			h=h*B1+ss[i][j];
			if(mp.find(h)==mp.end())
				mp[h]=n2++;
		}
	}
	for(int i=0; i<n; ++i) {
		hs h1, h2;
		h1=h1+ss[i][0];
		for(int j=1; j<ss[i].size(); ++j) {
			h1=h1*B1+ss[i][j];
			h2=h2*B1+ss[i][j];
			auto it1=mp.find(h2);
			if(it1!=mp.end()) {
				auto it2=mp.find(h1);
				graph[it1->second].push_back(it2->second);
				graph[it2->second].push_back(it1->second);
			}
		}
	}
	for(int i=0; i<n2; ++i)
		if(!vis[i])
			dfs(i);
	cout << ans << "\n";
	for(int i=0; i<n2; ++i)
		graph[i].clear();
	memset(vis, 0, n2);
	ans=0;
	mp.clear();
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int t;
	cin >> t;
	while(t--)
		solve();
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long

const int mxN=1e6, Ms=2;
const ll M[Ms]={1e9+7, 1000696969}, B1=29, B2=31;

struct hs {
	ll a[Ms]={};
	hs operator*(const ll &o) const {
		hs r;
		for(int i=0; i<Ms; ++i)
			r.a[i]=(a[i]*o)%M[i];
		return r;
	}
	hs operator+(const ll &o) const {
		hs r;
		for(int i=0; i<Ms; ++i)
			r.a[i]=(a[i]+o)%M[i];
		return r;
	}
	bool operator==(const hs &o) const {
		for(int i=0; i<Ms; ++i)
			if(a[i]!=o.a[i])
				return 0;
		return 1;
	}
};

namespace std {
	template<>
	struct hash<hs> {
		inline size_t operator()(const hs &o) const {
			size_t r=0;
			for(int i=0; i<Ms; ++i)
				r=r*B2+o.a[i];
			return r;
		}
	};
}

vector<int> graph[mxN];
bool vis[mxN];
int ans;
string ss[mxN];
unordered_map<hs, int> mp;

bool dfs(int u) {
	vis[u]=true;
	bool childMarked=0;
	for(int v : graph[u])
		if(!vis[v])
			childMarked|=dfs(v);
	if(!childMarked)
		++ans;
	return !childMarked;
}

inline void solve() {
	int n, n2=0;
	cin >> n;
	for(int i=0; i<n; ++i) {
		cin >> ss[i];
		hs h;
		for(int j=0; j<ss[i].size(); ++j) {
			h=h*B1+ss[i][j];
			if(mp.find(h)==mp.end())
				mp[h]=n2++;
		}
	}
	for(int i=0; i<n; ++i) {
		hs h1, h2;
		h1=h1+ss[i][0];
		for(int j=1; j<ss[i].size(); ++j) {
			h1=h1*B1+ss[i][j];
			h2=h2*B1+ss[i][j];
			auto it1=mp.find(h2);
			if(it1!=mp.end()) {
				auto it2=mp.find(h1);
				graph[it1->second].push_back(it2->second);
				graph[it2->second].push_back(it1->second);
			}
		}
	}
	for(int i=0; i<n2; ++i)
		if(!vis[i])
			dfs(i);
	cout << ans << "\n";
	for(int i=0; i<n2; ++i)
		graph[i].clear();
	memset(vis, 0, n2);
	ans=0;
	mp.clear();
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int t;
	cin >> t;
	while(t--)
		solve();
}


#include <bits/stdc++.h>
using namespace std;

#define MAXL 1000006

int T, N, K;
char buf[MAXL];
int D[MAXL], E[MAXL];
vector <int> con[MAXL]; bool vis[MAXL];

struct NODE {
    int num;
    NODE *nxt[26];
} container[MAXL];

NODE* new_node()
{
    NODE *ret = &container[K]; vis[K] = 0; con[K].clear();
    ret->num = K++;
    memset(ret->nxt, 0, sizeof(ret->nxt));
    return ret;
}

void dp(int n)
{
    D[n] = 1; E[n] = 0;
    for (int t: con[n]) if (!vis[t]){
        vis[t] = 1;
        dp(t);
        D[n] += E[t];
        E[n] += max(D[t], E[t]);
    }
}

int main()
{
    for (scanf("%d", &T);T--;){
        K = 0; NODE *root = new_node();
        scanf("%d", &N);
        vector <string> arr;
        for (int i=1;i<=N;i++){
            scanf("%s", buf); arr.push_back(buf);
            NODE* now = root;
            for (int j=0;buf[j];j++){
                if (!now->nxt[buf[j]-'a'])
                    now->nxt[buf[j]-'a'] = new_node();
                now = now->nxt[buf[j]-'a'];
            }
        }
        for (const auto &s: arr){
            NODE *now = root, *prv = root;
            for (int i=0;i<s.length();i++){
                int c = s[i]-'a';
                if (i > 0 && prv) prv = prv->nxt[c];
                now = now->nxt[c];
                con[prv ? prv->num : 0].push_back(now->num);
            }
        }
        dp(0);
        printf("%d\n", E[0]);
    }
}


#include <bits/stdc++.h>

using namespace std;

#define fr first
#define sc second
#define pb push_back
#define ins insert
#define input freopen("input.txt","r",stdin)
#define output freopen("output.txt","w",stdout)
#define mp make_pair
#define fast_io ios_base::sync_with_stdio(0);
#define form(i,a,n) for (int i=(a);i<(n);i++)
#define forn(i,a,n) for (int i=(a);i<(n);i++)
//iterator , unsigned, begin, end, count, continue
// fixed setprecision

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

string itosm(ll x){
    if(x == 0)
        return "0";
    string ans = "";
    while(x > 0){
        ans +=((x%10) + '0');
        x/=10;
    }
    reverse(ans.begin(), ans.end());
    return ans;
}

ll stoim(string str){
    ll ans = 0;
    ll k = 1;
    for(int i = str.length()-1; i >= 0; i--){
        ans+=(str[i]-'0')*k;
        k*=10;
    }
    return ans;
}

const ll infll = 1e18 + 3;
const int inf = 1009000999;
const double eps = 1e-7;
const int maxn = 1e6 + 777;
const int baseint = 1000200013;
const ll basell = 1e18 + 3;
const ld PI = acos(-1.0);
const ll mod = 1e9 + 7;

set<ll> st;
set<ll> p;
vector<string> ss;
vector<vector<pll> > h1, h2;
vector<int> nd;

void inline solve(){
    int n, m;
    cin >> m;
    string s;
    st.clear();
    p.clear();
    ss.clear();
    h1.clear();
    h2.clear();
    h1.resize(m);
    h2.resize(m);

    for(int jj = 0; jj < m; jj++){
        cin >> s;
        ss.pb(s);
        n = s.length();
        ll mod1 = 1e9 + 7, mod2 = 1000022809;
        h1[jj].pb(mp(s[0] - 'a' + 1, s[0] - 'a' + 1));
        st.insert((h1[jj][0].fr << 31) + h1[jj][0].sc);
        if(n > 1){
            h2[jj].pb(mp(s[1] - 'a' + 1, s[1] - 'a' + 1));
            nd.pb(jj);
        }
        for(int i = 1; i < n; i++){
            h1[jj].pb(mp((h1[jj].back().fr * 29 + (s[i] - 'a' + 1)) % mod1,
                            (h1[jj].back().sc * 31 + (s[i] - 'a' + 1)) % mod2));
        }
        for(int i = 2; i < n; i++){
            h2[jj].pb(mp((h2[jj].back().fr * 29 + (s[i] - 'a' + 1)) % mod1,
                            (h2[jj].back().sc * 31 + (s[i] - 'a' + 1)) % mod2));
        }
    }
    int l = 2;
    while(nd.size() > 0){
        for(int i = 0; i < nd.size(); i++){
            if(ss[nd[i]].size() < l){
                swap(nd[i], nd.back());
                nd.pop_back();
                i--;
            }
        }
        for(int i = 0; i < nd.size(); i++){
            if(!st.count((h2[nd[i]][l - 2].fr << 31) + h2[nd[i]][l - 2].sc) && !p.count((h2[nd[i]][l - 2].fr << 31) + h2[nd[i]][l - 2].sc)){
                st.insert((h1[nd[i]][l - 1].fr << 31) + h1[nd[i]][l - 1].sc);
                p.insert((h1[nd[i]][l - 1].fr << 31) + h1[nd[i]][l - 1].sc);
            }
        }
        l++;
    }
    cout << st.size();
}

int main(){
//    srand(228);
    srand(time(0));
    fast_io;
//    freopen("honeywar.in", "r", stdin);
//    freopen("honeywar.out", "w", stdout);
    int t;
    cin >> t;
//    scanf("%d", &t);
//    int cnt = 1;
    while(t--){
//        printf("Case %d: ", cnt);
        solve();
        cout << "\n";
//        puts("");
//        cnt++;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define fr first
#define sc second
#define pb push_back
#define ins insert
#define input freopen("input.txt","r",stdin)
#define output freopen("output.txt","w",stdout)
#define mp make_pair
#define fast_io ios_base::sync_with_stdio(0);
#define form(i,a,n) for (int i=(a);i<(n);i++)
#define forn(i,a,n) for (int i=(a);i<(n);i++)
//iterator , unsigned, begin, end, count, continue
// fixed setprecision

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

string itosm(ll x){
    if(x == 0)
        return "0";
    string ans = "";
    while(x > 0){
        ans +=((x%10) + '0');
        x/=10;
    }
    reverse(ans.begin(), ans.end());
    return ans;
}

ll stoim(string str){
    ll ans = 0;
    ll k = 1;
    for(int i = str.length()-1; i >= 0; i--){
        ans+=(str[i]-'0')*k;
        k*=10;
    }
    return ans;
}

const ll infll = 1e18 + 3;
const int inf = 1009000999;
const double eps = 1e-7;
const int maxn = 1e6 + 777;
const int baseint = 1000200013;
const ll basell = 1e18 + 3;
const ld PI = acos(-1.0);
const ll mod = 1e9 + 7;

unordered_set<ll> st, p;
vector<string> ss;
vector<vector<pll> > h1, h2;
vector<pii> nd;

void inline solve(){
    int n, m;
    cin >> m;
    string s;
    st.clear();
    p.clear();
    ss.clear();
    h1.clear();
    h2.clear();
    h1.resize(m);
    h2.resize(m);
    nd.clear();

    for(int jj = 0; jj < m; jj++){
        cin >> s;
        ss.pb(s);
        n = s.length();
        ll mod1 = 1e9 + 7, mod2 = 1000022809;
        h1[jj].pb(mp(s[0] - 'a' + 1, s[0] - 'a' + 1));
        nd.pb(mp(n, jj));
        if(n > 1){
            h2[jj].pb(mp(s[1] - 'a' + 1, s[1] - 'a' + 1));
        }
        for(int i = 1; i < n; i++){
            h1[jj].pb(mp((h1[jj].back().fr * 29 + (s[i] - 'a' + 1)) % mod1,
                            (h1[jj].back().sc * 31 + (s[i] - 'a' + 1)) % mod2));
        }
        for(int i = 2; i < n; i++){
            h2[jj].pb(mp((h2[jj].back().fr * 29 + (s[i] - 'a' + 1)) % mod1,
                            (h2[jj].back().sc * 31 + (s[i] - 'a' + 1)) % mod2));
        }
    }
    sort(nd.rbegin(), nd.rend());
    int l = nd[0].fr;
    while(l > 0){
        for(int i = 0; i < nd.size(); i++){
            if(l > nd[i].fr)
                break;
            if(!p.count((h1[nd[i].sc][l - 1].fr << 31) + h1[nd[i].sc][l - 1].sc)
               && (l - 2 < 0 || !st.count((h2[nd[i].sc][l - 2].fr << 31) + h2[nd[i].sc][l - 2].sc))){
                st.insert((h1[nd[i].sc][l - 1].fr << 31) + h1[nd[i].sc][l - 1].sc);
                if(l - 2 >= 0)
                    p.insert((h2[nd[i].sc][l - 2].fr << 31) + h2[nd[i].sc][l - 2].sc);
            }
        }
        l--;
    }
    cout << st.size();
}

int main(){
//    srand(228);
    srand(time(0));
    fast_io;
//    freopen("honeywar.in", "r", stdin);
//    freopen("honeywar.out", "w", stdout);
    int t;
    cin >> t;
//    scanf("%d", &t);
//    int cnt = 1;
    while(t--){
//        printf("Case %d: ", cnt);
        solve();
        cout << "\n";
//        puts("");
//        cnt++;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define fr first
#define sc second
#define pb push_back
#define ins insert
#define input freopen("input.txt","r",stdin)
#define output freopen("output.txt","w",stdout)
#define mp make_pair
#define fast_io ios_base::sync_with_stdio(0);
#define form(i,a,n) for (int i=(a);i<(n);i++)
#define forn(i,a,n) for (int i=(a);i<(n);i++)
//iterator , unsigned, begin, end, count, continue
// fixed setprecision

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

string itosm(ll x){
    if(x == 0)
        return "0";
    string ans = "";
    while(x > 0){
        ans +=((x%10) + '0');
        x/=10;
    }
    reverse(ans.begin(), ans.end());
    return ans;
}

ll stoim(string str){
    ll ans = 0;
    ll k = 1;
    for(int i = str.length()-1; i >= 0; i--){
        ans+=(str[i]-'0')*k;
        k*=10;
    }
    return ans;
}

const ll infll = 1e18 + 3;
const int inf = 1009000999;
const double eps = 1e-7;
const int maxn = 1e6 + 777;
const int baseint = 1000200013;
const ll basell = 1e18 + 3;
const ld PI = acos(-1.0);
const ll mod = 1e9 + 7;

set<ll> st;
vector<string> ss;
vector<vector<pll> > h1, h2;
vector<int> nd;

void inline solve(){
    int n, m;
    cin >> m;
    string s;
    st.clear();
    ss.clear();
    h1.clear();
    h2.clear();
    h1.resize(m);
    h2.resize(m);

    for(int jj = 0; jj < m; jj++){
        cin >> s;
        ss.pb(s);
        n = s.length();
        ll mod1 = 1e9 + 7, mod2 = 1000022809;
        h1[jj].pb(mp(s[0] - 'a' + 1, s[0] - 'a' + 1));
        st.insert((h1[jj][0].fr << 31) + h1[jj][0].sc);
        if(n > 1){
            h2[jj].pb(mp(s[1] - 'a' + 1, s[1] - 'a' + 1));
            nd.pb(jj);
        }
        for(int i = 1; i < n; i++){
            h1[jj].pb(mp((h1[jj].back().fr * 29 + (s[i] - 'a' + 1)) % mod1,
                            (h1[jj].back().sc * 31 + (s[i] - 'a' + 1)) % mod2));
        }
        for(int i = 2; i < n; i++){
            h2[jj].pb(mp((h2[jj].back().fr * 29 + (s[i] - 'a' + 1)) % mod1,
                            (h2[jj].back().sc * 31 + (s[i] - 'a' + 1)) % mod2));
        }
    }
    int l = 2;
    while(nd.size() > 0){
        for(int i = 0; i < nd.size(); i++){
            if(ss[nd[i]].size() < l){
                swap(nd[i], nd.back());
                nd.pop_back();
                i--;
            }
        }
        for(int i = 0; i < nd.size(); i++){
            if(!st.count((h2[nd[i]][l - 2].fr << 31) + h2[nd[i]][l - 2].sc) ){
                st.insert((h1[nd[i]][l - 1].fr << 31) + h1[nd[i]][l - 1].sc);
            }
        }
        l++;
    }
    cout << st.size();
}

int main(){
//    srand(228);
    srand(time(0));
    fast_io;
//    freopen("honeywar.in", "r", stdin);
//    freopen("honeywar.out", "w", stdout);
    int t;
    cin >> t;
//    scanf("%d", &t);
//    int cnt = 1;
    while(t--){
//        printf("Case %d: ", cnt);
        solve();
        cout << "\n";
//        puts("");
//        cnt++;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define fr first
#define sc second
#define pb push_back
#define ins insert
#define input freopen("input.txt","r",stdin)
#define output freopen("output.txt","w",stdout)
#define mp make_pair
#define fast_io ios_base::sync_with_stdio(0);
#define form(i,a,n) for (int i=(a);i<(n);i++)
#define forn(i,a,n) for (int i=(a);i<(n);i++)
//iterator , unsigned, begin, end, count, continue
// fixed setprecision

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

string itosm(ll x){
    if(x == 0)
        return "0";
    string ans = "";
    while(x > 0){
        ans +=((x%10) + '0');
        x/=10;
    }
    reverse(ans.begin(), ans.end());
    return ans;
}

ll stoim(string str){
    ll ans = 0;
    ll k = 1;
    for(int i = str.length()-1; i >= 0; i--){
        ans+=(str[i]-'0')*k;
        k*=10;
    }
    return ans;
}

const ll infll = 1e18 + 3;
const int inf = 1009000999;
const double eps = 1e-7;
const int maxn = 1e6 + 777;
const int baseint = 1000200013;
const ll basell = 1e18 + 3;
const ld PI = acos(-1.0);
const ll mod = 1e9 + 7;

unordered_set<ll> st;
unordered_set<ll> p;
vector<string> ss;
vector<vector<pll> > h1, h2;
vector<int> nd;

void inline solve(){
    int n, m;
    cin >> m;
    string s;
    st.clear();
    p.clear();
    ss.clear();
    h1.clear();
    h2.clear();
    h1.resize(m);
    h2.resize(m);

    for(int jj = 0; jj < m; jj++){
        cin >> s;
        ss.pb(s);
        n = s.length();
        ll mod1 = 1e9 + 7, mod2 = 1000022809;
        h1[jj].pb(mp(s[0] - 'a' + 1, s[0] - 'a' + 1));
        st.insert((h1[jj][0].fr << 31) + h1[jj][0].sc);
        if(n > 1){
            h2[jj].pb(mp(s[1] - 'a' + 1, s[1] - 'a' + 1));
            nd.pb(jj);
        }
        for(int i = 1; i < n; i++){
            h1[jj].pb(mp((h1[jj].back().fr * 29 + (s[i] - 'a' + 1)) % mod1,
                            (h1[jj].back().sc * 31 + (s[i] - 'a' + 1)) % mod2));
        }
        for(int i = 2; i < n; i++){
            h2[jj].pb(mp((h2[jj].back().fr * 29 + (s[i] - 'a' + 1)) % mod1,
                            (h2[jj].back().sc * 31 + (s[i] - 'a' + 1)) % mod2));
        }
    }
    int l = 2;
    while(nd.size() > 0){
        for(int i = 0; i < nd.size(); i++){
            if(ss[nd[i]].size() < l){
                swap(nd[i], nd.back());
                nd.pop_back();
                i--;
            }
        }
        for(int i = 0; i < nd.size(); i++){
            if(!st.count((h2[nd[i]][l - 2].fr << 31) + h2[nd[i]][l - 2].sc) && !p.count((h2[nd[i]][l - 2].fr << 31) + h2[nd[i]][l - 2].sc)){
                st.insert((h1[nd[i]][l - 1].fr << 31) + h1[nd[i]][l - 1].sc);
                p.insert((h2[nd[i]][l - 2].fr << 31) + h2[nd[i]][l - 2].sc);
            }
        }
        l++;
    }
    cout << st.size();
}

int main(){
//    srand(228);
    srand(time(0));
    fast_io;
//    freopen("honeywar.in", "r", stdin);
//    freopen("honeywar.out", "w", stdout);
    int t;
    cin >> t;
//    scanf("%d", &t);
//    int cnt = 1;
    while(t--){
//        printf("Case %d: ", cnt);
        solve();
        cout << "\n";
//        puts("");
//        cnt++;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define fr first
#define sc second
#define pb push_back
#define ins insert
#define input freopen("input.txt","r",stdin)
#define output freopen("output.txt","w",stdout)
#define mp make_pair
#define fast_io ios_base::sync_with_stdio(0);
#define form(i,a,n) for (int i=(a);i<(n);i++)
#define forn(i,a,n) for (int i=(a);i<(n);i++)
//iterator , unsigned, begin, end, count, continue
// fixed setprecision

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

string itosm(ll x){
    if(x == 0)
        return "0";
    string ans = "";
    while(x > 0){
        ans +=((x%10) + '0');
        x/=10;
    }
    reverse(ans.begin(), ans.end());
    return ans;
}

ll stoim(string str){
    ll ans = 0;
    ll k = 1;
    for(int i = str.length()-1; i >= 0; i--){
        ans+=(str[i]-'0')*k;
        k*=10;
    }
    return ans;
}

const ll infll = 1e18 + 3;
const int inf = 1009000999;
const double eps = 1e-7;
const int maxn = 1e6 + 777;
const int baseint = 1000200013;
const ll basell = 1e18 + 3;
const ld PI = acos(-1.0);
const ll mod = 1e9 + 7;

set<ll> st, p;
vector<string> ss;
vector<vector<pll> > h1, h2;
vector<pii> nd;

void inline solve(){
    int n, m;
    cin >> m;
    string s;
    st.clear();
    p.clear();
    ss.clear();
    h1.clear();
    h2.clear();
    h1.resize(m);
    h2.resize(m);
    nd.clear();

    for(int jj = 0; jj < m; jj++){
        cin >> s;
        ss.pb(s);
        n = s.length();
        ll mod1 = 1e9 + 7, mod2 = 1000022809;
        h1[jj].pb(mp(s[0] - 'a' + 1, s[0] - 'a' + 1));
        nd.pb(mp(n, jj));
        if(n > 1){
            h2[jj].pb(mp(s[1] - 'a' + 1, s[1] - 'a' + 1));
        }
        for(int i = 1; i < n; i++){
            h1[jj].pb(mp((h1[jj].back().fr * 29 + (s[i] - 'a' + 1)) % mod1,
                            (h1[jj].back().sc * 31 + (s[i] - 'a' + 1)) % mod2));
        }
        for(int i = 2; i < n; i++){
            h2[jj].pb(mp((h2[jj].back().fr * 29 + (s[i] - 'a' + 1)) % mod1,
                            (h2[jj].back().sc * 31 + (s[i] - 'a' + 1)) % mod2));
        }
    }
    sort(nd.rbegin(), nd.rend());
    int l = nd[0].fr;
    while(l > 0){
        for(int i = 0; i < nd.size(); i++){
            if(l > nd[i].fr)
                break;
            if(!p.count((h1[nd[i].sc][l - 1].fr << 31) + h1[nd[i].sc][l - 1].sc)
               && (l - 2 < 0 || !st.count((h2[nd[i].sc][l - 2].fr << 31) + h2[nd[i].sc][l - 2].sc))){
                st.insert((h1[nd[i].sc][l - 1].fr << 31) + h1[nd[i].sc][l - 1].sc);
                if(l - 2 >= 0)
                    p.insert((h2[nd[i].sc][l - 2].fr << 31) + h2[nd[i].sc][l - 2].sc);
            }
        }
        l--;
    }
    cout << st.size();
}

int main(){
//    srand(228);
    srand(time(0));
    fast_io;
//    freopen("honeywar.in", "r", stdin);
//    freopen("honeywar.out", "w", stdout);
    int t;
    cin >> t;
//    scanf("%d", &t);
//    int cnt = 1;
    while(t--){
//        printf("Case %d: ", cnt);
        solve();
        cout << "\n";
//        puts("");
//        cnt++;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define fr first
#define sc second
#define pb push_back
#define ins insert
#define input freopen("input.txt","r",stdin)
#define output freopen("output.txt","w",stdout)
#define mp make_pair
#define fast_io ios_base::sync_with_stdio(0);
#define form(i,a,n) for (int i=(a);i<(n);i++)
#define forn(i,a,n) for (int i=(a);i<(n);i++)
//iterator , unsigned, begin, end, count, continue
// fixed setprecision

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

string itosm(ll x){
    if(x == 0)
        return "0";
    string ans = "";
    while(x > 0){
        ans +=((x%10) + '0');
        x/=10;
    }
    reverse(ans.begin(), ans.end());
    return ans;
}

ll stoim(string str){
    ll ans = 0;
    ll k = 1;
    for(int i = str.length()-1; i >= 0; i--){
        ans+=(str[i]-'0')*k;
        k*=10;
    }
    return ans;
}

const ll infll = 1e18 + 3;
const int inf = 1009000999;
const double eps = 1e-7;
const int maxn = 1e6 + 777;
const int baseint = 1000200013;
const ll basell = 1e18 + 3;
const ld PI = acos(-1.0);
const ll mod = 1e9 + 7;

set<ll> st;
set<ll> p;
vector<string> ss;
vector<vector<pll> > h1, h2;
vector<int> nd;

void inline solve(){
    int n, m;
    cin >> m;
    string s;
    st.clear();
    p.clear();
    ss.clear();
    h1.clear();
    h2.clear();
    h1.resize(m);
    h2.resize(m);

    for(int jj = 0; jj < m; jj++){
        cin >> s;
        ss.pb(s);
        n = s.length();
        ll mod1 = 1e9 + 7, mod2 = 1000022809;
        h1[jj].pb(mp(s[0] - 'a' + 1, s[0] - 'a' + 1));
        st.insert((h1[jj][0].fr << 31) + h1[jj][0].sc);
        if(n > 1){
            h2[jj].pb(mp(s[1] - 'a' + 1, s[1] - 'a' + 1));
            nd.pb(jj);
        }
        for(int i = 1; i < n; i++){
            h1[jj].pb(mp((h1[jj].back().fr * 29 + (s[i] - 'a' + 1)) % mod1,
                            (h1[jj].back().sc * 31 + (s[i] - 'a' + 1)) % mod2));
        }
        for(int i = 2; i < n; i++){
            h2[jj].pb(mp((h2[jj].back().fr * 29 + (s[i] - 'a' + 1)) % mod1,
                            (h2[jj].back().sc * 31 + (s[i] - 'a' + 1)) % mod2));
        }
    }
    int l = 2;
    while(nd.size() > 0){
        for(int i = 0; i < nd.size(); i++){
            if(ss[nd[i]].size() < l){
                swap(nd[i], nd.back());
                nd.pop_back();
                i--;
            }
        }
        for(int i = 0; i < nd.size(); i++){
            if(!st.count((h2[nd[i]][l - 2].fr << 31) + h2[nd[i]][l - 2].sc) && !p.count((h2[nd[i]][l - 2].fr << 31) + h2[nd[i]][l - 2].sc)){
                st.insert((h1[nd[i]][l - 1].fr << 31) + h1[nd[i]][l - 1].sc);
                p.insert((h2[nd[i]][l - 2].fr << 31) + h2[nd[i]][l - 2].sc);
            }
        }
        l++;
    }
    cout << st.size();
}

int main(){
//    srand(228);
    srand(time(0));
    fast_io;
//    freopen("honeywar.in", "r", stdin);
//    freopen("honeywar.out", "w", stdout);
    int t;
    cin >> t;
//    scanf("%d", &t);
//    int cnt = 1;
    while(t--){
//        printf("Case %d: ", cnt);
        solve();
        cout << "\n";
//        puts("");
//        cnt++;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define fr first
#define sc second
#define pb push_back
#define ins insert
#define input freopen("input.txt","r",stdin)
#define output freopen("output.txt","w",stdout)
#define mp make_pair
#define fast_io ios_base::sync_with_stdio(0);
#define form(i,a,n) for (int i=(a);i<(n);i++)
#define forn(i,a,n) for (int i=(a);i<(n);i++)
//iterator , unsigned, begin, end, count, continue
// fixed setprecision

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

string itosm(ll x){
    if(x == 0)
        return "0";
    string ans = "";
    while(x > 0){
        ans +=((x%10) + '0');
        x/=10;
    }
    reverse(ans.begin(), ans.end());
    return ans;
}

ll stoim(string str){
    ll ans = 0;
    ll k = 1;
    for(int i = str.length()-1; i >= 0; i--){
        ans+=(str[i]-'0')*k;
        k*=10;
    }
    return ans;
}

const ll infll = 1e18 + 3;
const int inf = 1009000999;
const double eps = 1e-7;
const int maxn = 1e6 + 777;
const int baseint = 1000200013;
const ll basell = 1e18 + 3;
const ld PI = acos(-1.0);
const ll mod = 1e9 + 7;

unordered_set<ll> st;
vector<string> ss;
vector<vector<pll> > h1, h2;
vector<int> nd;

void inline solve(){
    int n, m;
    cin >> m;
    string s;
    st.clear();
    ss.clear();
    h1.clear();
    h2.clear();
    h1.resize(m);
    h2.resize(m);

    for(int jj = 0; jj < m; jj++){
        cin >> s;
        ss.pb(s);
        n = s.length();
        ll mod1 = 1e9 + 7, mod2 = 1000022809;
        h1[jj].pb(mp(s[0] - 'a' + 1, s[0] - 'a' + 1));
        st.insert((h1[jj][0].fr << 31) + h1[jj][0].sc);
        if(n > 1){
            h2[jj].pb(mp(s[1] - 'a' + 1, s[1] - 'a' + 1));
            nd.pb(jj);
        }
        for(int i = 1; i < n; i++){
            h1[jj].pb(mp((h1[jj].back().fr * 29 + (s[i] - 'a' + 1)) % mod1,
                            (h1[jj].back().sc * 31 + (s[i] - 'a' + 1)) % mod2));
        }
        for(int i = 2; i < n; i++){
            h2[jj].pb(mp((h2[jj].back().fr * 29 + (s[i] - 'a' + 1)) % mod1,
                            (h2[jj].back().sc * 31 + (s[i] - 'a' + 1)) % mod2));
        }
    }
    int l = 2;
    while(nd.size() > 0){
        for(int i = 0; i < nd.size(); i++){
            if(ss[nd[i]].size() < l){
                swap(nd[i], nd.back());
                nd.pop_back();
                i--;
            }
        }
        for(int i = 0; i < nd.size(); i++){
            if(!st.count((h2[nd[i]][l - 2].fr << 31) + h2[nd[i]][l - 2].sc)){
                st.insert((h1[nd[i]][l - 1].fr << 31) + h1[nd[i]][l - 1].sc);
            }
        }
        l++;
    }
    cout << st.size();
}

int main(){
//    srand(228);
    srand(time(0));
    fast_io;
//    freopen("honeywar.in", "r", stdin);
//    freopen("honeywar.out", "w", stdout);
    int t;
    cin >> t;
//    scanf("%d", &t);
//    int cnt = 1;
    while(t--){
//        printf("Case %d: ", cnt);
        solve();
        cout << "\n";
//        puts("");
//        cnt++;
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

//#define int long long
#define mod 1000000009

struct algo {
    int n;
    map<pair<int, int>, int > v1;
    set<pair<int, int> > czy;
    vector<vector<int> > drogi;
    vector<pair<int, int> > tab;
    vector<pair<int, int> > wy;
    algo() {
        cin >> n;
        string s;
        int li=0;
        tab.push_back( {} );
        for( long long a, b, i=1; i<=n; i++ ) {
            cin >> s;
            a = b = 0;
            for( int j=0; j<s.size(); j++ ) {
                a = (a*33+s[j]-'a'+1)%mod;
                if( j!=0 ) b = (b*33+s[j]-'a'+1)%mod;
                if( czy.find( {a, j+1} ) != czy.end() ) continue;
                czy.insert( {a, j+1} );
                v1[{a, j+1}] = ++li;
                tab.push_back( {b, j} );
                //v2[{b, j+1}].push_back( li );
            }
        }
        drogi.resize( li+10 );
        for( int i=1; i<=li; i++ ) {
            drogi[ v1[ tab[i] ] ].push_back( i );
        }
        wy.resize( li+10 );
        cout << dfs().second << "\n";
    }

    pair<int, int> dfs( int gdzie=0 ) {
        if( wy[gdzie].second != 0 ) return wy[gdzie];
        int x=0, y=0;
        pair<int, int> p;
        for( int i : drogi[gdzie] ) {
            p = dfs( i );
            x += p.first;
            y += p.second;
        }
        return wy[gdzie] = {max(y+1, x), x};
    }
};

int32_t main() {
    ios_base::sync_with_stdio( 0 );
    cin.tie( 0 );

    int t;
    cin >> t;
    while( t-- ) {
        algo p;
    }

    return 0;
}
/*

1 5 4 1 3
            */


#include<bits/stdc++.h>

using namespace std;

//#define int long long
#define mod 1000000007

struct algo {
    int n;
    map<pair<int, int>, int > v1;
    set<pair<int, int> > czy;
    vector<vector<int> > drogi;
    vector<pair<int, int> > tab;
    algo() {
        cin >> n;
        string s;
        int li=0;
        tab.push_back( {} );
        for( long long a, b, i=1; i<=n; i++ ) {
            cin >> s;
            a = b = 0;
            for( int j=0; j<s.size(); j++ ) {
                a = (a*31+s[j]-'a')%mod;
                if( j!=0 ) b = (b*31+s[j]-'a')%mod;
                if( czy.find( {a, j+1} ) != czy.end() ) continue;
                czy.insert( {a, j+1} );
                v1[{a, j+1}] = ++li;
                tab.push_back( {b, j} );
                //v2[{b, j+1}].push_back( li );
            }
        }
        drogi.resize( li+10 );
        for( int i=1; i<=li; i++ ) {
            drogi[ v1[ tab[i] ] ].push_back( i );
        }
        cout << dfs().second << "\n";
    }

    pair<int, int> dfs( int gdzie=0 ) {
        int x=0, y=0;
        pair<int, int> p;
        for( int i : drogi[gdzie] ) {
            p = dfs( i );
            x += p.first;
            y += p.second;
        }
        return {max(y+1, x), x};
    }
};

int32_t main() {
    ios_base::sync_with_stdio( 0 );
    cin.tie( 0 );

    int t;
    cin >> t;
    while( t-- ) {
        algo p;
    }

    return 0;
}
/*

1 5 4 1 3
            */


#include<bits/stdc++.h>

using namespace std;

//#define int long long
#define mod 1000000009

struct algo {
    int n;
    map<pair<int, int>, int > v1;
    set<pair<int, int> > czy;
    vector<vector<int> > drogi;
    vector<pair<int, int> > tab;
    algo() {
        cin >> n;
        string s;
        int li=0;
        tab.push_back( {} );
        for( long long a, b, i=1; i<=n; i++ ) {
            cin >> s;
            a = b = 0;
            for( int j=0; j<s.size(); j++ ) {
                a = (a*33+s[j]-'a')%mod;
                if( j!=0 ) b = (b*33+s[j]-'a')%mod;
                if( czy.find( {a, j+1} ) != czy.end() ) continue;
                czy.insert( {a, j+1} );
                v1[{a, j+1}] = ++li;
                tab.push_back( {b, j} );
                //v2[{b, j+1}].push_back( li );
            }
        }
        drogi.resize( li+10 );
        for( int i=1; i<=li; i++ ) {
            drogi[ v1[ tab[i] ] ].push_back( i );
        }
        cout << dfs().second << "\n";
    }

    pair<int, int> dfs( int gdzie=0 ) {
        int x=0, y=0;
        pair<int, int> p;
        for( int i : drogi[gdzie] ) {
            p = dfs( i );
            x += p.first;
            y += p.second;
        }
        return {max(y+1, x), x};
    }
};

int32_t main() {
    ios_base::sync_with_stdio( 0 );
    cin.tie( 0 );

    int t;
    cin >> t;
    while( t-- ) {
        algo p;
    }

    return 0;
}
/*

1 5 4 1 3
            */


#include<bits/stdc++.h>

using namespace std;

//#define int long long
#define mod 1000000009

struct algo {
    int n;
    map<pair<int, int>, int > v1;
    //set<pair<int, int> > czy;
    vector<vector<int> > drogi;
    vector<pair<int, int> > tab;
    vector<pair<int, int> > wy;
    algo() {
        cin >> n;
        string s;
        int li=0;
        tab.push_back( {} );
        for( long long a, b, i=1; i<=n; i++ ) {
            cin >> s;
            a = b = 0;
            for( int j=0; j<s.size(); j++ ) {
                a = (a*33+s[j]-'a'+1)%mod;
                if( j!=0 ) b = (b*33+s[j]-'a'+1)%mod;
                if( v1.find( {a, j+1} ) != v1.end() ) continue;
                //czy.insert( {a, j+1} );
                v1[{a, j+1}] = ++li;
                tab.push_back( {b, j} );
                //v2[{b, j+1}].push_back( li );
            }
        }
        drogi.resize( li+10 );
        for( int i=1; i<=li; i++ ) {
            drogi[ v1[ tab[i] ] ].push_back( i );
        }
        wy.resize( li+10 );
        cout << dfs().second << "\n";
    }

    pair<int, int> dfs( int gdzie=0 ) {
        if( wy[gdzie].second != 0 ) return wy[gdzie];
        int x=0, y=0;
        pair<int, int> p;
        for( int i : drogi[gdzie] ) {
            p = dfs( i );
            x += p.first;
            y += p.second;
        }
        return wy[gdzie] = {max(y+1, x), x};
    }
};

int32_t main() {
    ios_base::sync_with_stdio( 0 );
    cin.tie( 0 );

    int t;
    cin >> t;
    while( t-- ) {
        algo p;
    }

    return 0;
}
/*

1 5 4 1 3
            */


#include<bits/stdc++.h>

using namespace std;

#define int long long
#define mod 1000000007

struct algo {
    int n;
    map<pair<int, int>, int > v1;
    set<pair<int, int> > czy;
    vector<vector<int> > drogi;
    vector<pair<int, int> > tab;
    algo() {
        cin >> n;
        string s;
        int li=0;
        tab.push_back( {} );
        for( int a, b, i=1; i<=n; i++ ) {
            cin >> s;
            a = b = 0;
            for( int j=0; j<s.size(); j++ ) {
                a = (a*31+s[j]-'a')%mod;
                if( j!=0 ) b = (b*31+s[j]-'a')%mod;
                if( czy.find( {a, j+1} ) != czy.end() ) continue;
                czy.insert( {a, j+1} );
                v1[{a, j+1}] = ++li;
                tab.push_back( {b, j} );
                //v2[{b, j+1}].push_back( li );
            }
        }
        drogi.resize( li+10 );
        for( int i=1; i<=li; i++ ) {
            drogi[ v1[ tab[i] ] ].push_back( i );
        }
        cout << dfs().second << "\n";
    }

    pair<int, int> dfs( int gdzie=0 ) {
        int x=0, y=0;
        pair<int, int> p;
        for( int i : drogi[gdzie] ) {
            p = dfs( i );
            x += p.first;
            y += p.second;
        }
        return {max(y+1, x), x};
    }
};

int32_t main() {
    ios_base::sync_with_stdio( 0 );
    cin.tie( 0 );

    int t;
    cin >> t;
    while( t-- ) {
        algo p;
    }

    return 0;
}
/*

1 5 4 1 3
            */


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mkp make_pair
#define fi first
#define se second
#define ll long long
#define M 1000000007
#define all(a) a.begin(), a.end()

const int maxn = 1000010;
int go[maxn][26], fail[maxn], cnt[maxn], root = 1, tot = 1;
vector<int> g[maxn];
int dp[maxn][2], dep[maxn];
bool vis[maxn];
int T, n;
char s[maxn];

void insert(char *str){
	int cur = root, i = 0;
	while(str[i]){
		int idx = str[i] - 'a';
		if(!go[cur][idx]){
			go[cur][idx] = ++tot;
			memset(go[tot], 0, sizeof(go[tot])); fail[tot] = 0;
			dep[tot] = dep[cur] + 1;
			g[tot].clear();
			vis[tot] = 0;
		}
		cur = go[cur][idx];
		i++;
	}
}

void build(){
	queue<int> q;
	q.push(root);
	while(!q.empty()){
		int t = q.front(); q.pop();
		for(int i = 0; i < 26; ++i)
			if(!go[t][i]){
				go[t][i] = t == root ? 1 : go[fail[t]][i];
			}else{
				fail[go[t][i]] = t == root ? 1 : go[fail[t]][i];
				if(fail[go[t][i]] != 1 && dep[go[t][i]] == dep[fail[go[t][i]]] + 1)
					g[fail[go[t][i]]].pb(go[t][i]), vis[go[t][i]] = 1;
				q.push(go[t][i]);
			}
	}
}

void dfs(int t){
	dp[t][0] = 0;
	dp[t][1] = 1;
	for(int v : g[t]){
		dfs(v);
		dp[t][1] += dp[v][0];
		dp[t][0] += max(dp[v][1], dp[v][0]);
	}
}

int main(){
	scanf("%d", &T);
	while(T--){
		scanf("%d", &n);
		root = tot = 1; memset(go[1], 0, sizeof(go[1])); cnt[1] = fail[1] = 0; g[1].clear();
		for(int i = 0; i < n; ++i){
			scanf("%s", s);
			insert(s);
		}
		build();
		int ans = 0;
		for(int i = 2; i <= tot; ++i)
			if(!vis[i]){
				dfs(i);
				ans += max(dp[i][0], dp[i][1]);
			}
		printf("%d\n", ans);
	}

#ifndef ONLINE_JUDGE
	system("pause");
#endif
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int N = 1e6 + 5;
const long long MOD_1 = 1e9 + 7;
const long long MOD_2 = 1e9 + 9;
const long long PODST = 29;
int t, n;
char wcz[N];
string s[N];
vector<pair<long long,long long>> hasz[N];
long long pot_1[N], pot_2[N];

int main()
{
  pot_1[0] = pot_2[0] = 1;
  for (int i = 1; i < N; ++ i)
  {
    pot_1[i] = (pot_1[i-1] * PODST) % MOD_1;
    pot_2[i] = (pot_2[i-1] * PODST) % MOD_2;
  }

  scanf("%d", &t);
  for (int ii = 0; ii < t; ++ ii)
  {
    scanf("%d", &n);
    for (int i = 0; i < n; ++ i)
    {
      scanf("%s", wcz);
      s[i] = wcz;

      hasz[i].push_back({0, 0});
      for (int j = 0; j < (int) s[i].size(); ++ j)
        hasz[i].push_back({ (hasz[i].back().first * PODST + (s[i][j] - 'a' + 1)) % MOD_1, (hasz[i].back().second * PODST + (s[i][j] - 'a' + 1)) % MOD_2 });
    }

    priority_queue<pair<int,int>> Q;
    for (int i = 0; i < n; ++ i)
      Q.push(make_pair(s[i].size(), i));

    unordered_set<long long> zak;
    int wynik = 0;

    while (!Q.empty())
    {
      int i = Q.top().second, d = Q.top().first;
      Q.pop();
      if (d != 1) Q.push(make_pair(d - 1, i));

      if (zak.find(hasz[i][d].first * MOD_2 + hasz[i][d].second) == zak.end())
      {
        zak.insert(hasz[i][d].first * MOD_2 + hasz[i][d].second);
        ++ wynik;
        pair<long long, long long> nowy_hasz = { (hasz[i][d].first - (s[i][0] - 'a' + 1) * pot_1[d-1] + MOD_1 * MOD_1) % MOD_1,
                                                 (hasz[i][d].second - (s[i][0] - 'a' + 1) * pot_2[d-1] + MOD_2 * MOD_2) % MOD_2};

         zak.insert(nowy_hasz.first * MOD_2 + nowy_hasz.second);
      }
    }

    printf("%d\n", wynik);

    for (int i = 0; i < n; ++ i)
    {
      hasz[i].clear();
      s[i].clear();
    }
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int N = 1e6 + 5;
const long long MOD_1 = 1e9 + 7;
const long long MOD_2 = 1e9 + 9;
const long long PODST = 29;
int t, n;
char wcz[N];
string s[N];
vector<pair<long long,long long>> hasz[N];
long long pot_1[N], pot_2[N];

int main()
{
  pot_1[0] = pot_2[0] = 1;
  for (int i = 1; i < N; ++ i)
  {
    pot_1[i] = (pot_1[i-1] * PODST) % MOD_1;
    pot_2[i] = (pot_2[i-1] * PODST) % MOD_2;
  }

  scanf("%d", &t);
  for (int ii = 0; ii < t; ++ ii)
  {
    scanf("%d", &n);
    for (int i = 0; i < n; ++ i)
    {
      scanf("%s", wcz);
      s[i] = wcz;

      hasz[i].push_back({0, 0});
      for (int j = 0; j < (int) s[i].size(); ++ j)
        hasz[i].push_back({ (hasz[i].back().first * PODST + (s[i][j] - 'a' + 1)) % MOD_1, (hasz[i].back().second * PODST + (s[i][j] - 'a' + 1)) % MOD_2 });
    }

    priority_queue<pair<int,int>> Q;
    for (int i = 0; i < n; ++ i)
      Q.push(make_pair(s[i].size(), i));

    set<pair<long long, long long>> zak;
    int wynik = 0;

    while (!Q.empty())
    {
      int i = Q.top().second, d = Q.top().first;
      Q.pop();
      if (d != 1) Q.push(make_pair(d - 1, i));

      if (zak.find(hasz[i][d]) == zak.end())
      {
        zak.insert(hasz[i][d]);
        ++ wynik;
        pair<long long, long long> nowy_hasz = { (hasz[i][d].first - (s[i][0] - 'a' + 1) * pot_1[d-1] + MOD_1 * MOD_1) % MOD_1,
                                                 (hasz[i][d].second - (s[i][0] - 'a' + 1) * pot_2[d-1] + MOD_2 * MOD_2) % MOD_2};

         zak.insert(nowy_hasz);
      }
    }

    printf("%d\n", wynik);

    for (int i = 0; i < n; ++ i)
    {
      hasz[i].clear();
      s[i].clear();
    }
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int N = 1e6 + 5;
const long long MOD_1 = 1e9 + 7;
const long long MOD_2 = 1e9 + 9;
const long long PODST = 29;
int t, n;
char wcz[N];
string s[N];
vector<pair<long long,long long>> hasz[N];
long long pot_1[N], pot_2[N];

int main()
{
  pot_1[0] = pot_2[0] = 1;
  for (int i = 1; i < N; ++ i)
  {
    pot_1[i] = (pot_1[i-1] * PODST) % MOD_1;
    pot_2[i] = (pot_2[i-1] * PODST) % MOD_2;
  }

  scanf("%d", &t);
  for (int ii = 0; ii < t; ++ ii)
  {
    scanf("%d", &n);
    for (int i = 0; i < n; ++ i)
    {
      scanf("%s", wcz);
      s[i] = wcz;

      hasz[i].push_back({0, 0});
      for (int j = 0; j < (int) s[i].size(); ++ j)
        hasz[i].push_back({ (hasz[i].back().first * PODST + (s[i][j] - 'a' + 1)) % MOD_1, (hasz[i].back().second * PODST + (s[i][j] - 'a' + 1)) % MOD_2 });
    }

    priority_queue<pair<int,int>> Q;
    for (int i = 0; i < n; ++ i)
      Q.push(make_pair(s[i].size(), i));

    unordered_set<pair<long long, long long>> zak;
    int wynik = 0;

    while (!Q.empty())
    {
      int i = Q.top().second, d = Q.top().first;
      Q.pop();
      if (d != 1) Q.push(make_pair(d - 1, i));

      if (zak.find(hasz[i][d]) == zak.end())
      {
        zak.insert(hasz[i][d]);
        ++ wynik;
        pair<long long, long long> nowy_hasz = { (hasz[i][d].first - (s[i][0] - 'a' + 1) * pot_1[d-1] + MOD_1 * MOD_1) % MOD_1,
                                                 (hasz[i][d].second - (s[i][0] - 'a' + 1) * pot_2[d-1] + MOD_2 * MOD_2) % MOD_2};

         zak.insert(nowy_hasz);
      }
    }

    printf("%d\n", wynik);

    for (int i = 0; i < n; ++ i)
    {
      hasz[i].clear();
      s[i].clear();
    }
  }
  return 0;
}


#include <iostream>
#include <algorithm>
#define MAXN 2000010

using namespace std;

int n;
string s;
int t[MAXN][26],num;
int par[MAXN];
bool pre[MAXN];
vector<int> ch[MAXN];
int dp[MAXN][2];

int go(int u, char ch) {
    int c = ch - 'a';
    if (t[u][c] == 0) {
        for (int i = 0; i < 26; i++) t[num][i] = 0;
        pre[num] = 0;
        t[u][c] = num++;
    }
    return t[u][c];
}

void dfs(int u) {
    dp[u][0] = 0;
    dp[u][1] = pre[u];
    for (int v : ch[u]) {
        dfs(v);
        dp[u][0] += max(dp[v][0], dp[v][1]);
        dp[u][1] += dp[v][0];
    }
}

int main() {
    ios::sync_with_stdio(0);
    int T;
    cin >> T;
    while (T--) {
        cin >> n;

        num = 1;
        for (int i = 0; i < 26; i++) t[0][i] = 0;

        for (int i = 0; i < n; i++) {
            cin >> s;
            int u = 0, v = 0;
            for (int j = 0; j < s.size(); j++) {
                if (j > 0)
                    v = go(v, s[j]);
                u = go(u, s[j]);
                pre[u] = 1;
                par[u] = v;
            }
        }

        for (int i = 0; i < num; i++)
            ch[i].clear();
        for (int i = 1; i < num; i++)
            ch[par[i]].push_back(i);

        dfs(0);

        cout << dp[0][0] << '\n';
    }
}


#include <iostream>
#include <algorithm>
#define MAXN 2000010

using namespace std;

int n;
string s;
int t[MAXN][26],num;
int par[MAXN];
bool pre[MAXN];
vector<int> ch[MAXN];
int dp[MAXN][2];

int go(int u, char ch) {
    int c = ch - 'a';
    if (t[u][c] == 0) {
        for (int i = 0; i < 26; i++) t[num][i] = 0;
        pre[num] = 0;
        par[num] = 0;
        t[u][c] = num++;
    }
    return t[u][c];
}

void dfs(int u) {
    dp[u][0] = 0;
    dp[u][1] = pre[u];
    for (int v : ch[u]) {
        dfs(v);
        dp[u][0] += max(dp[v][0], dp[v][1]);
        dp[u][1] += dp[v][0];
    }
}

int main() {
    ios::sync_with_stdio(0);
    int T;
    cin >> T;
    while (T--) {
        cin >> n;

        num = 1;
        for (int i = 0; i < 26; i++) t[0][i] = 0;

        for (int i = 0; i < n; i++) {
            cin >> s;
            int u = 0, v = 0;
            for (int j = 0; j < s.size(); j++) {
                if (j > 0)
                    v = go(v, s[j]);
                u = go(u, s[j]);
                pre[u] = 1;
                par[u] = v;
            }
        }

        for (int i = 0; i < num; i++)
            ch[i].clear();
        for (int i = 1; i < num; i++)
            ch[par[i]].push_back(i);

        dfs(0);

        cout << dp[0][0] << '\n';
    }
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <cassert>
#include <string.h>
//#include <unordered_set>
//#include <unordered_map>
//#include <bits/stdc++.h>

#define MP(A,B) make_pair(A,B)
#define PB(A) push_back(A)
#define SIZE(A) ((int)A.size())
#define LEN(A) ((int)A.length())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define fi first
#define se second

using namespace std;

template<typename T>inline bool upmin(T &x,T y) { return y<x?x=y,1:0; }
template<typename T>inline bool upmax(T &x,T y) { return x<y?x=y,1:0; }

typedef long long ll;
typedef unsigned long long ull;
typedef long double lod;
typedef pair<int,int> PR;
typedef vector<int> VI;

const lod eps=1e-11;
const lod pi=acos(-1);
const int oo=1<<30;
const ll loo=1ll<<62;
const int mods=1e9+7;
const int SZ=127;
const int MAXN=1000050;
const int INF=0x3f3f3f3f;//1061109567
/*--------------------------------------------------------------------*/
inline int read()
{
	int f=1,x=0; char c=getchar();
	while (c<'0'||c>'9') { if (c=='-') f=-1; c=getchar(); }
	while (c>='0'&&c<='9') { x=(x<<3)+(x<<1)+(c^48); c=getchar(); }
	return x*f;
}
ull pw[MAXN];
pair<ull,ull> a[MAXN];
map<ull,int> Map;
int num=0,Num=0,d[MAXN],f[MAXN][2];
char st[MAXN];
vector<int> e[MAXN];
void Init()
{
	Map.clear();
	for (int i=1;i<=num;i++) e[i].clear(),f[i][0]=f[i][1]=d[i]=0;
	Num=0,Map[0]=num=1;
}
void tree_dp(int x)
{
	for (auto v:e[x]) tree_dp(v);
	for (auto v:e[x]) f[x][0]+=max(f[v][0],f[v][1]),f[x][1]+=f[v][0];
}
signed main()
{
	int Case=read();
	pw[0]=1;
	for (int i=1;i<=1e6;i++) pw[i]=pw[i-1]*SZ;
	while (Case--)
	{
		int n=read();
		Init();
		for (int i=1;i<=n;i++)
		{
			scanf("%s",st);
			int len=strlen(st); ull t=0;
			for (int j=0;j<len;j++)
			{
				t=t*SZ+st[j]-'a'+1,a[++Num]=MP(t,t-pw[j]*(st[0]-'a'+1));
				if (!Map[a[Num].fi]) Map[a[Num].fi]=++num,f[num][1]=1;
				if (!Map[a[Num].se]) Map[a[Num].se]=++num;
			}
		}
		sort(a+1,a+Num+1);
		Num=unique(a+1,a+Num+1)-a-1;
		int ans=0;
		for (int i=1;i<=Num;i++) e[Map[a[i].se]].PB(Map[a[i].fi]),d[Map[a[i].fi]]++;
		for (int i=1;i<=num;i++) if (!d[i]) tree_dp(i),ans+=f[i][0];
		printf("%d\n",ans);
	}
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <cassert>
#include <string.h>
//#include <unordered_set>
//#include <unordered_map>
//#include <bits/stdc++.h>

#define MP(A,B) make_pair(A,B)
#define PB(A) push_back(A)
#define SIZE(A) ((int)A.size())
#define LEN(A) ((int)A.length())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define fi first
#define se second

using namespace std;

template<typename T>inline bool upmin(T &x,T y) { return y<x?x=y,1:0; }
template<typename T>inline bool upmax(T &x,T y) { return x<y?x=y,1:0; }

typedef long long ll;
typedef unsigned long long ull;
typedef long double lod;
typedef pair<int,int> PR;
typedef vector<int> VI;

const lod eps=1e-11;
const lod pi=acos(-1);
const int oo=1<<30;
const ll loo=1ll<<62;
const int mods=1e9+7;
const int SZ=127;
const int MAXN=1000050;
const int INF=0x3f3f3f3f;//1061109567
/*--------------------------------------------------------------------*/
inline int read()
{
	int f=1,x=0; char c=getchar();
	while (c<'0'||c>'9') { if (c=='-') f=-1; c=getchar(); }
	while (c>='0'&&c<='9') { x=(x<<3)+(x<<1)+(c^48); c=getchar(); }
	return x*f;
}
int nodenum=0,vis[MAXN],f[MAXN][2],ch[MAXN][26],n,len[MAXN];
char st[MAXN],ST[MAXN];
vector<int> e[MAXN];
void Init()
{
	for (int i=1;i<=nodenum;i++) e[i].clear();
	memset(ch,0,(nodenum+10)*sizeof ch[0]);
	memset(vis,0,(nodenum+10)*sizeof vis[0]);
	nodenum=1;
}
void tree_dp(int x,int father)
{
	int lst=-1;
	vis[x]=1;
	for (auto v:e[x]) if (v!=father&&v!=lst) tree_dp(v,x),lst=v;
	f[x][0]=0,f[x][1]=1;
	lst=-1;
	for (auto v:e[x]) if (v!=father&&v!=lst) f[x][0]+=max(f[v][0],f[v][1]),f[x][1]+=f[v][0],lst=v;
}
void insert(char *st)
{
	int len=strlen(st),nw=1;
	for (int i=0;i<len;i++)
	{
		if (!ch[nw][st[i]-'a']) ch[nw][st[i]-'a']=++nodenum;
		nw=ch[nw][st[i]-'a'];
	}
}
void build()
{
	for (int i=1;i<=n;i++)
	{
		int nw0=ch[1][ST[len[i-1]]-'a'],nw1=1;
		for (int j=len[i-1]+1;j<len[i];j++)
		{
			nw0=ch[nw0][ST[j]-'a'],nw1=ch[nw1][ST[j]-'a'];
			if (!nw0||!nw1) break;
			e[nw0].PB(nw1),e[nw1].PB(nw0);
		}
	}
	for (int i=1;i<=nodenum;i++) sort(e[i].begin(),e[i].end());
}
signed main()
{
	int Case=read();
	while (Case--)
	{
		n=read(),Init();
		for (int i=1;i<=n;i++)
		{
			scanf("%s",st);
			insert(st);
			len[i]=strlen(st);
			for (int j=len[i-1];j<len[i-1]+len[i];j++) ST[j]=st[j-len[i-1]];
			len[i]+=len[i-1];
		}
		build();
		int ans=0;
		for (int i=2;i<=nodenum;i++)
			if (!vis[i]) tree_dp(i,0),ans+=max(f[i][0],f[i][1]);
		printf("%d\n",ans);
	}
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <cassert>
#include <string.h>
//#include <unordered_set>
//#include <unordered_map>
//#include <bits/stdc++.h>

#define MP(A,B) make_pair(A,B)
#define PB(A) push_back(A)
#define SIZE(A) ((int)A.size())
#define LEN(A) ((int)A.length())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define fi first
#define se second

using namespace std;

template<typename T>inline bool upmin(T &x,T y) { return y<x?x=y,1:0; }
template<typename T>inline bool upmax(T &x,T y) { return x<y?x=y,1:0; }

typedef long long ll;
typedef unsigned long long ull;
typedef long double lod;
typedef pair<int,int> PR;
typedef vector<int> VI;

const lod eps=1e-11;
const lod pi=acos(-1);
const int oo=1<<30;
const ll loo=1ll<<62;
const int mods=1e9+7;
const int SZ=127;
const int MAXN=1000050;
const int INF=0x3f3f3f3f;//1061109567
/*--------------------------------------------------------------------*/
inline int read()
{
	int f=1,x=0; char c=getchar();
	while (c<'0'||c>'9') { if (c=='-') f=-1; c=getchar(); }
	while (c>='0'&&c<='9') { x=(x<<3)+(x<<1)+(c^48); c=getchar(); }
	return x*f;
}
int nodenum=0,vis[MAXN],f[MAXN][2],ch[MAXN][26],n,len[MAXN];
char st[MAXN],ST[MAXN];
vector<int> e[MAXN];
void Init()
{
	for (int i=1;i<=nodenum;i++) e[i].clear();
	memset(ch,0,(nodenum+10)*sizeof ch[0]);
	memset(vis,0,(nodenum+10)*sizeof vis[0]);
	nodenum=1;
}
void tree_dp(int x,int father)
{
	vis[x]=1;
	for (auto v:e[x]) if (v!=father) tree_dp(v,x);
	f[x][0]=0,f[x][1]=1;
	for (auto v:e[x]) if (v!=father) f[x][0]+=max(f[v][0],f[v][1]),f[x][1]+=f[v][0];
}
void insert(char *st)
{
	int len=strlen(st),nw=1;
	for (int i=0;i<len;i++)
	{
		if (!ch[nw][st[i]-'a']) ch[nw][st[i]-'a']=++nodenum;
		nw=ch[nw][st[i]-'a'];
	}
}
void build()
{
	for (int i=1;i<=n;i++)
	{
		int nw0=ch[1][ST[len[i-1]]-'a'],nw1=1;
		for (int j=len[i-1]+1;j<len[i];j++)
		{
			nw0=ch[nw0][ST[j]-'a'],nw1=ch[nw1][ST[j]-'a'];
			if (!nw0||!nw1) break;
			e[nw0].PB(nw1),e[nw1].PB(nw0);
		}
	}
}
signed main()
{
	int Case=read();
	while (Case--)
	{
		n=read(),Init();
		for (int i=1;i<=n;i++)
		{
			scanf("%s",st);
			insert(st);
			len[i]=strlen(st);
			for (int j=len[i-1];j<len[i-1]+len[i];j++) ST[j]=st[j-len[i-1]];
			len[i]+=len[i-1];
		}
		build();
		int ans=0;
		for (int i=2;i<=nodenum;i++)
			if (!vis[i]) tree_dp(i,0),ans+=max(f[i][0],f[i][1]);
		printf("%d\n",ans);
	}
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <cassert>
#include <string.h>
//#include <unordered_set>
//#include <unordered_map>
//#include <bits/stdc++.h>

#define MP(A,B) make_pair(A,B)
#define PB(A) push_back(A)
#define SIZE(A) ((int)A.size())
#define LEN(A) ((int)A.length())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define fi first
#define se second

using namespace std;

template<typename T>inline bool upmin(T &x,T y) { return y<x?x=y,1:0; }
template<typename T>inline bool upmax(T &x,T y) { return x<y?x=y,1:0; }

typedef long long ll;
typedef unsigned long long ull;
typedef long double lod;
typedef pair<int,int> PR;
typedef vector<int> VI;

const lod eps=1e-11;
const lod pi=acos(-1);
const int oo=1<<30;
const ll loo=1ll<<62;
const int mods=1e9+7;
const int SZ=127;
const int MAXN=1000050;
const int INF=0x3f3f3f3f;//1061109567
/*--------------------------------------------------------------------*/
inline int read()
{
	int f=1,x=0; char c=getchar();
	while (c<'0'||c>'9') { if (c=='-') f=-1; c=getchar(); }
	while (c>='0'&&c<='9') { x=(x<<3)+(x<<1)+(c^48); c=getchar(); }
	return x*f;
}
ull pw[MAXN];
pair<ull,ull> a[MAXN];
map<ull,int> Map;
int num=0,Num=0,d[MAXN],f[MAXN][2];
char st[MAXN];
vector<int> e[MAXN];
void Init()
{
	Map.clear();
	for (int i=1;i<=num;i++) e[i].clear(),f[i][0]=f[i][1]=d[i]=0;
	f[1][1]=Num=0,Map[0]=num=1;
}
void tree_dp(int x)
{
	for (auto v:e[x]) tree_dp(v);
	for (auto v:e[x]) f[x][0]+=max(f[v][0],f[v][1]),f[x][1]+=f[v][0];
}
signed main()
{
	int Case=read();
	pw[0]=1;
	for (int i=1;i<=1e6;i++) pw[i]=pw[i-1]*SZ;
	while (Case--)
	{
		int n=read();
		Init();
		for (int i=1;i<=n;i++)
		{
			scanf("%s",st);
			int len=strlen(st); ull t=0;
			for (int j=0;j<len;j++)
			{
				t=t*SZ+st[j]-'a'+1,a[++Num]=MP(t,t-pw[j]*(st[0]-'a'+1));
				if (!Map[a[Num].fi]) Map[a[Num].fi]=++num,f[num][1]=1;
			}
		}
		sort(a+1,a+Num+1);
		Num=unique(a+1,a+Num+1)-a-1;
		int ans=0;
		for (int i=1;i<=Num;i++)
			if (Map[a[i].fi]&&Map[a[i].se]) e[Map[a[i].se]].PB(Map[a[i].fi]),d[Map[a[i].fi]]++;
		for (int i=1;i<=num;i++)
			if (!d[i]) tree_dp(i),ans+=max(f[i][0],f[i][1]);
		printf("%d\n",ans);
	}
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <cassert>
#include <string.h>
//#include <unordered_set>
//#include <unordered_map>
//#include <bits/stdc++.h>

#define MP(A,B) make_pair(A,B)
#define PB(A) push_back(A)
#define SIZE(A) ((int)A.size())
#define LEN(A) ((int)A.length())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define fi first
#define se second

using namespace std;

template<typename T>inline bool upmin(T &x,T y) { return y<x?x=y,1:0; }
template<typename T>inline bool upmax(T &x,T y) { return x<y?x=y,1:0; }

typedef long long ll;
typedef unsigned long long ull;
typedef long double lod;
typedef pair<int,int> PR;
typedef vector<int> VI;

const lod eps=1e-11;
const lod pi=acos(-1);
const int oo=1<<30;
const ll loo=1ll<<62;
const int mods=1e9+7;
const int SZ=127;
const int MAXN=1000050;
const int INF=0x3f3f3f3f;//1061109567
/*--------------------------------------------------------------------*/
inline int read()
{
	int f=1,x=0; char c=getchar();
	while (c<'0'||c>'9') { if (c=='-') f=-1; c=getchar(); }
	while (c>='0'&&c<='9') { x=(x<<3)+(x<<1)+(c^48); c=getchar(); }
	return x*f;
}
ull pw[MAXN];
pair<ull,ull> a[MAXN];
map<ull,int> Map;
int num=0,Num=0,f[MAXN][2];
char st[MAXN];
vector<int> e[MAXN];
void Init()
{
	Map.clear();
	for (int i=1;i<=Num;i++) e[i].clear();
	Num=0;
	Map[0]=num=1;
}
void tree_dp(int x)
{
	for (auto v:e[x]) tree_dp(v);
	f[x][0]=0,f[x][1]=1;
	for (auto v:e[x]) f[x][0]+=max(f[v][0],f[v][1]),f[x][1]+=f[v][0];
}
signed main()
{
	int Case=read();
	pw[0]=1;
	for (int i=1;i<=1e6;i++) pw[i]=pw[i-1]*SZ;
	while (Case--)
	{
		int n=read();
		Init();
		for (int i=1;i<=n;i++)
		{
			scanf("%s",st);
			int len=strlen(st),t=0,p=0;
			for (int j=0;j<len;j++)
			{
				t=t*SZ+st[j]-'a'+1,a[++Num]=MP(t,t-pw[j]*(st[0]-'a'+1));
				if (!Map[t]) Map[t]=++num;
			}
		}
		sort(a+1,a+Num+1);
		Num=unique(a+1,a+Num+1)-a-1;
		int ans=0;
		for (int i=1;i<=Num;i++)
			if (Map[a[i].fi]&&Map[a[i].se]) e[Map[a[i].se]].PB(Map[a[i].fi]);
			else ans++;
		tree_dp(1);
		printf("%d\n",f[1][0]+ans);
	}
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <cassert>
#include <string.h>
//#include <unordered_set>
//#include <unordered_map>
//#include <bits/stdc++.h>

#define MP(A,B) make_pair(A,B)
#define PB(A) push_back(A)
#define SIZE(A) ((int)A.size())
#define LEN(A) ((int)A.length())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define fi first
#define se second

using namespace std;

template<typename T>inline bool upmin(T &x,T y) { return y<x?x=y,1:0; }
template<typename T>inline bool upmax(T &x,T y) { return x<y?x=y,1:0; }

typedef long long ll;
typedef unsigned long long ull;
typedef long double lod;
typedef pair<int,int> PR;
typedef vector<int> VI;

const lod eps=1e-11;
const lod pi=acos(-1);
const int oo=1<<30;
const ll loo=1ll<<62;
const int mods=1e9+7;
const int SZ=127;
const int MAXN=1000050;
const int INF=0x3f3f3f3f;//1061109567
/*--------------------------------------------------------------------*/
inline int read()
{
	int f=1,x=0; char c=getchar();
	while (c<'0'||c>'9') { if (c=='-') f=-1; c=getchar(); }
	while (c>='0'&&c<='9') { x=(x<<3)+(x<<1)+(c^48); c=getchar(); }
	return x*f;
}
ull pw[MAXN];
pair<ull,ull> a[MAXN];
map<ull,int> Map;
int num=0,Num=0,f[MAXN][2];
char st[MAXN];
vector<int> e[MAXN];
void Init()
{
	Map.clear();
	for (int i=1;i<=Num;i++) e[i].clear();
	Num=0;
	Map[0]=num=1;
}
void tree_dp(int x)
{
	for (auto v:e[x]) tree_dp(v);
	f[x][0]=0,f[x][1]=1;
	for (auto v:e[x]) f[x][0]+=max(f[v][0],f[v][1]),f[x][1]+=f[v][0];
}
signed main()
{
	int Case=read();
	pw[0]=1;
	for (int i=1;i<=1e6;i++) pw[i]=pw[i-1]*SZ;
	while (Case--)
	{
		int n=read();
		Init();
		for (int i=1;i<=n;i++)
		{
			scanf("%s",st);
			int len=strlen(st); ull t=0,p=0;
			for (int j=0;j<len;j++)
			{
				t=t*SZ+st[j]-'a'+1,a[++Num]=MP(t,t-pw[j]*(st[0]-'a'+1));
				if (!Map[t]) Map[t]=++num;
			}
		}
		sort(a+1,a+Num+1);
		Num=unique(a+1,a+Num+1)-a-1;
		int ans=0;
		for (int i=1;i<=Num;i++)
			if (Map[a[i].fi]&&Map[a[i].se]) e[Map[a[i].se]].PB(Map[a[i].fi]);
			else ans++;
		tree_dp(1);
		printf("%d\n",f[1][0]+ans);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int M = 1e6+1;

namespace AC{
    int nxt[M][26], fail[M], dep[M];
    int root, L;
    int node(int d){
        memset(nxt[L], 0, sizeof nxt[L]);
        dep[L] = d;
        return L ++;
    }
    void init(){
        L = 0;
        root = node(0);
    }
    void insert(char c[]){
        int len = strlen(c+1);
        int now = root;
        for(int i = 1; i <= len; i ++){
            int& x = nxt[now][c[i]-'a'];
            if(!x) x = node(i);
            now = x;
        }
    }
    int Q[M], tail;
    void build(){
        tail = 0;
        for(int i = 0; i < 26; i ++){
            int x = nxt[0][i];
            if(x){
                fail[x] = 0;
                Q[++ tail] = x;
            }
        }
        for(int i = 1; i <= tail; i ++){
            int u = Q[i], fu = fail[u];
            for(int j = 0; j < 26; j ++){
                int& v = nxt[u][j], p = fu;
                if(!v) continue;
                for(; p && !nxt[p][j]; p = fail[p]);
                //if(!v) v = nxt[p][j];
                //else{
                    fail[v] = nxt[p][j];
                    Q[++ tail] = v;
                //}
            }
        }
    }
    int dp[M][2];
    void work(){
        build();
        for(int i = 0; i <= tail; i ++) dp[i][0] = dp[i][1] = 0;
        for(int i = tail; i > 0; i --){
            int u = Q[i], p = fail[u];
            int w = max(dp[u][0], ++ dp[u][1]);
            dp[p][0] += w;
            dp[p][1] += (dep[p] + 1 < dep[u]) ? w : dp[u][0];
        }
        printf("%d\n", dp[0][0]);
    }
}
char c[M];

int main(){
    int T;
    scanf("%d", &T);
    while(T --){
        int n;
        scanf("%d", &n);
        AC::init();
        while(n --){
            scanf("%s", c+1);
            AC::insert(c);
        }
        AC::work();
    }
}


#include<bits/stdc++.h>
using namespace std;
const int M = 1e6+1;

namespace AC{
    int nxt[M][26], fail[M], dep[M];
    int root, L;
    int node(int d){
        memset(nxt[L], -1, sizeof nxt[L]);
        dep[L] = d;
        return L ++;
    }
    void init(){
        L = 0;
        root = node(0);
    }
    void insert(char c[]){
        int len = strlen(c+1);
        int now = root;
        for(int i = 1; i <= len; i ++){
            int& x = nxt[now][c[i]-'a'];
            if(x == -1) x = node(i);
            now = x;
        }
    }
    int Q[M], tail;
    void build(){
        tail = 0;
        for(int i = 0; i < 26; i ++){
            if(nxt[0][i] != -1){
                fail[nxt[0][i]] = 0;
                Q[++ tail] = nxt[0][i];
            }
        }
        for(int i = 1; i <= tail; i ++){
            int u = Q[i], fu = fail[u];
            for(int j = 0; j < 26; j ++){
                int& v = nxt[u][j], p = fu;
                for(; p && nxt[p][j] == -1; p = fail[p]);
                if(v == -1) v = nxt[p][j];
                else{
                    fail[v] = nxt[p][j];
                    Q[++ tail] = v;
                }
            }
        }
    }
    int dp[M][2];
    void work(){
        build();
        for(int i = 0; i <= tail; i ++) dp[i][0] = dp[i][1] = 0;
        for(int i = tail; i > 0; i --){
            int u = Q[i], p = fail[u];
            int w = max(dp[u][0], ++ dp[u][1]);
            dp[p][0] += w;
            dp[p][1] += (dep[p] + 1 < dep[u]) ? w : dp[u][0];
        }
        printf("%d\n", dp[0][0]);
    }
}
char c[M];

int main(){
    int T;
    scanf("%d", &T);
    while(T --){
        int n;
        scanf("%d", &n);
        AC::init();
        while(n --){
            scanf("%s", c+1);
            AC::insert(c);
        }
        AC::work();
    }
}


#include<bits/stdc++.h>
using namespace std;
const int M = 1e6+1;

namespace AC{
    int nxt[M][26], fail[M], dep[M];
    int root, L;
    int node(int d){
        memset(nxt[L], 0, sizeof nxt[L]);
        dep[L] = d;
        return L ++;
    }
    void init(){
        L = 0;
        root = node(0);
    }
    void insert(char c[]){
        int len = strlen(c+1);
        int now = root;
        for(int i = 1; i <= len; i ++){
            int& x = nxt[now][c[i]-'a'];
            if(!x) x = node(i);
            now = x;
        }
    }
    int Q[M], tail;
    void build(){
        tail = 0;
        for(int i = 0; i < 26; i ++){
            int x = nxt[0][i];
            if(x){
                fail[x] = 0;
                Q[++ tail] = x;
            }
        }
        for(int i = 1; i <= tail; i ++){
            int u = Q[i], fu = fail[u];
            for(int j = 0; j < 26; j ++){
                int& v = nxt[u][j], p = fu;
                for(; p && !nxt[p][j]; p = fail[p]);
                if(!v) continue;
                else{
                    fail[v] = nxt[p][j];
                    Q[++ tail] = v;
                }
            }
        }
    }
    int dp[M][2];
    void work(){
        build();
        for(int i = 0; i <= tail; i ++) dp[i][0] = dp[i][1] = 0;
        for(int i = tail; i > 0; i --){
            int u = Q[i], p = fail[u];
            int w = max(dp[u][0], ++ dp[u][1]);
            dp[p][0] += w;
            dp[p][1] += (dep[p] + 1 < dep[u]) ? w : dp[u][0];
        }
        printf("%d\n", dp[0][0]);
    }
}
char c[M];

int main(){
    int T;
    scanf("%d", &T);
    while(T --){
        int n;
        scanf("%d", &n);
        AC::init();
        while(n --){
            scanf("%s", c+1);
            AC::insert(c);
        }
        AC::work();
    }
}


#include<bits/stdc++.h>
using namespace std;
const int M = 1e6+10;

namespace AC{
    int nxt[M][26], fail[M], dep[M];
    int root, L;
    int node(int d){
        memset(nxt[L], -1, sizeof nxt[L]);
        dep[L] = d;
        return L ++;
    }
    void init(){
        L = 0;
        root = node(0);
    }
    void insert(char c[]){
        int len = strlen(c+1);
        int now = root;
        for(int i = 1; i <= len; i ++){
            int& x = nxt[now][c[i]-'a'];
            if(x == -1) x = node(i);
            now = x;
        }
    }
    int Q[M], tail;
    void build(){
        tail = 0;
        for(int i = 0; i < 26; i ++){
            if(nxt[0][i] != -1){
                fail[nxt[0][i]] = 0;
                Q[++ tail] = nxt[0][i];
            }
        }
        for(int i = 1; i <= tail; i ++){
            int u = Q[i], fu = fail[u];
            for(int j = 0; j < 26; j ++){
                int& v = nxt[u][j], p = fu;
                for(; p && nxt[p][j] == -1; p = fail[p]);
                if(v == -1) v = nxt[p][j];
                else{
                    fail[v] = nxt[p][j];
                    Q[++ tail] = v;
                }
            }
        }
    }
    int dp[M][2];
    void work(){
        build();
        for(int i = 0; i <= tail; i ++) dp[i][0] = dp[i][1] = 0;
        for(int i = tail; i > 0; i --){
            int u = Q[i], p = fail[u];
            int w = max(dp[u][0], ++ dp[u][1]);
            dp[p][0] += w;
            dp[p][1] += (dep[p] + 1 < dep[u]) ? w : dp[u][0];
        }
        printf("%d\n", dp[0][0]);
    }
}
char c[M];

int main(){
    int T;
    scanf("%d", &T);
    while(T --){
        int n;
        scanf("%d", &n);
        AC::init();
        while(n --){
            scanf("%s", c+1);
            AC::insert(c);
        }
        AC::work();
    }
}


#include<bits/stdc++.h>
using namespace std;
const int M = 1e6+1;

namespace AC{
    int nxt[M][26], fail[M], dep[M];
    int root, L;
    int node(int d){
        memset(nxt[L], 0, sizeof nxt[L]);
        dep[L] = d;
        return L ++;
    }
    void init(){
        L = 0;
        root = node(0);
    }
    void insert(char c[]){
        int len = strlen(c+1);
        int now = root;
        for(int i = 1; i <= len; i ++){
            int& x = nxt[now][c[i]-'a'];
            if(!x) x = node(i);
            now = x;
        }
    }
    int Q[M], tail;
    void build(){
        tail = 0;
        for(int i = 0; i < 26; i ++){
            int x = nxt[0][i];
            if(x){
                fail[x] = 0;
                Q[++ tail] = x;
            }
        }
        for(int i = 1; i <= tail; i ++){
            int u = Q[i], fu = fail[u];
            for(int j = 0; j < 26; j ++){
                int& v = nxt[u][j], p = fu;
                for(; p && !nxt[p][j]; p = fail[p]);
                if(!v) v = nxt[p][j];
                else{
                    fail[v] = nxt[p][j];
                    Q[++ tail] = v;
                }
            }
        }
    }
    int dp[M][2];
    void work(){
        build();
        for(int i = 0; i <= tail; i ++) dp[i][0] = dp[i][1] = 0;
        for(int i = tail; i > 0; i --){
            int u = Q[i], p = fail[u];
            int w = max(dp[u][0], ++ dp[u][1]);
            dp[p][0] += w;
            dp[p][1] += (dep[p] + 1 < dep[u]) ? w : dp[u][0];
        }
        printf("%d\n", dp[0][0]);
    }
}
char c[M];

int main(){
    int T;
    scanf("%d", &T);
    while(T --){
        int n;
        scanf("%d", &n);
        AC::init();
        while(n --){
            scanf("%s", c+1);
            AC::insert(c);
        }
        AC::work();
    }
}


#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <string>
#include <bitset>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <sstream>
#include <stack>
#include <iomanip>
using namespace std;
#define pb push_back
#define mp make_pair
typedef pair<int,int> pii;
typedef long long ll;
typedef double ld;
typedef vector<int> vi;
#define fi first
#define se second
#define fe first
#define FO(x) {freopen(#x".in","r",stdin);freopen(#x".out","w",stdout);}
#define Edg int M=0,fst[SZ],vb[SZ],nxt[SZ];void ad_de(int a,int b){++M;nxt[M]=fst[a];fst[a]=M;vb[M]=b;}void adde(int a,int b){ad_de(a,b);ad_de(b,a);}
#define Edgc int M=0,fst[SZ],vb[SZ],nxt[SZ],vc[SZ];void ad_de(int a,int b,int c){++M;nxt[M]=fst[a];fst[a]=M;vb[M]=b;vc[M]=c;}void adde(int a,int b,int c){ad_de(a,b,c);ad_de(b,a,c);}
#define es(x,e) (int e=fst[x];e;e=nxt[e])
#define esb(x,e,b) (int e=fst[x],b=vb[e];e;e=nxt[e],b=vb[e])
#define VIZ {printf("digraph G{\n"); for(int i=1;i<=n;i++) for es(i,e) printf("%d->%d;\n",i,vb[e]); puts("}");}
#define VIZ2 {printf("graph G{\n"); for(int i=1;i<=n;i++) for es(i,e) if(vb[e]>=i)printf("%d--%d;\n",i,vb[e]); puts("}");}
#define SZ 1234567
int T,n; char s[SZ];
int an=1,ch[SZ][26];
void ins(char* s)
{
	int cur=1;
	for(int i=0;s[i];++i)
	{
		int c=s[i]-'a';
		if(!ch[cur][c])
		{
			ch[cur][c]=++an;
			memset(ch[an],0,sizeof ch[0]);
		}
		cur=ch[cur][c];
	}
}
int fa[SZ];
vector<int> chs[SZ];
void dfs(int x,int g,int l=0)
{
	int gg=(g==1)?0:g;
	fa[x]=gg; if(gg) chs[gg].pb(x);
	for(int j=0;j<26;++j)
	{
		if(!ch[x][j]) continue;
		if(l) dfs(ch[x][j],ch[g][j],l+1);
		else dfs(ch[x][j],g,l+1);
	}
}
int f[SZ][2];
void dd(int x)
{
	f[x][0]=0; f[x][1]=1;
	for(auto b:chs[x])
	{
		dd(b); f[x][1]+=f[b][0];
		f[x][0]+=max(f[b][1],f[b][0]);
	}
}
void sol()
{
	scanf("%d",&n); an=1;
	memset(ch[1],0,sizeof ch[1]);
	for(int i=1;i<=n;++i)
		scanf("%s",s), ins(s);
	for(int i=1;i<=an;++i) fa[i]=0,chs[i].clear();
	dfs(1,1);
	int ans=0;
	for(int i=2;i<=an;++i) if(!fa[i])
		dd(i), ans+=max(f[i][0],f[i][1]);
	printf("%d\n",ans);
}
int main()
{
	scanf("%d",&T);
	while(T--) sol();
}


#include <iostream>
#include <queue>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <map>
#include <vector>
#include <cstring>
#include <iomanip>
#include <set>
#include <sstream>
#include <ctime>

#define rust(a, b, c, d) sqrt(sqr(a - c) + sqr(b - d))

#define sqr(a) (a)*(a)
#define m_p make_pair
#define fi first
#define se second
#define fast_io ios_base::sync_with_stdio(0); cin.tie(0)
#define endl '\n'

typedef long long ll;
const int MAXINT=2147483640;
const ll MAXLL=9223372036854775800LL;
const ll MAXN=1000005;
const ll MOD = 1123456769;
const ll magic = 27;

using namespace std;

set <ll> s;
ll step[MAXN];

struct String{
    ll hsh, pre, len;
} a[MAXN];

bool comp(String a, String b) {return (a.len > b.len);}

int main()
{

    fast_io;

    ll t, i, j;
    step[0] = 1;
    for (ll i = 1; i <= 1e6; ++i) step[i] = step[i - 1] * magic, step[i] %= MOD;

    cin >> t;
    while(t--) {
        ll n, sz = 0;
        cin >> n;
        s.clear();
        string s1;
        for (i = 1; i <= n; ++i) {
            cin >> s1;
            ll h = s1[0], z = h, h2 = 0, j;
            ++sz;
            a[sz].pre = -1, a[sz].hsh = z, a[sz].len = 1;
            s.insert(a[sz].hsh);
            for (j = 1; j < s1.size(); ++j) {
                h = (h * magic + s1[j]) % MOD;
                h2 = (h2 * magic + s1[j]) % MOD;
                ++sz, a[sz].pre = h2, a[sz].hsh = h, a[sz].len = j + 1;
                s.insert(a[sz].hsh);

            }
        }
        sort(a + 1, a + sz + 1, comp);
        for (i = 1; i <= sz; ++i) if (s.find(a[i].hsh) != s.end()) s.erase(a[i].pre);

        cout << s.size() << endl;
    }

    return 0;
}


#include <iostream>
#include <queue>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <map>
#include <vector>
#include <cstring>
#include <iomanip>
#include <set>
#include <sstream>
#include <ctime>

#define rust(a, b, c, d) sqrt(sqr(a - c) + sqr(b - d))

#define sqr(a) (a)*(a)
#define m_p make_pair
#define fi first
#define se second
#define fast_io ios_base::sync_with_stdio(0); cin.tie(0)
#define endl '\n'

typedef long long ll;
const int MAXINT=2147483640;
const ll MAXLL=9223372036854775800LL;
const ll MAXN=1000005;
const ll MOD = 1e9 + 7;
const ll magic = 27;

using namespace std;

set <ll> s;
ll step[MAXN];

struct String{
    ll hsh, pre, len;
} a[MAXN];

bool comp(String a, String b) {return (a.len > b.len);}

int main()
{

    fast_io;

    ll t, i, j;
    step[0] = 1;
    for (ll i = 1; i <= 1e6; ++i) step[i] = step[i - 1] * magic, step[i] %= MOD;

    cin >> t;
    while(t--) {
        ll n, sz = 0;
        cin >> n;
        s.clear();
        string s1;
        for (i = 1; i <= n; ++i) {
            cin >> s1;
            ll h = s1[0], z = h, h2 = 0, j;
            ++sz;
            a[sz].pre = -1, a[sz].hsh = z, a[sz].len = 1;
            s.insert(a[sz].hsh);
            for (j = 1; j < s1.size(); ++j) {
                h = (h * magic + s1[j]) % MOD;
                h2 = (h2 * magic + s1[j]) % MOD;
                ++sz, a[sz].pre = h2, a[sz].hsh = h, a[sz].len = j + 1;
                s.insert(a[sz].hsh);

            }
        }
        sort(a + 1, a + sz + 1, comp);
        for (i = 1; i <= sz; ++i) if (s.find(a[i].hsh) != s.end()) s.erase(a[i].pre);

        cout << s.size() << endl;
    }

    return 0;
}


#include <iostream>
#include <queue>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <map>
#include <vector>
#include <cstring>
#include <iomanip>
#include <set>
#include <sstream>
#include <ctime>

#define rust(a, b, c, d) sqrt(sqr(a - c) + sqr(b - d))

#define sqr(a) (a)*(a)
#define m_p make_pair
#define fi first
#define se second
#define fast_io ios_base::sync_with_stdio(0); cin.tie(0)
#define endl '\n'

typedef long long ll;
const int MAXINT=2147483640;
const ll MAXLL=9223372036854775800LL;
const ll MAXN=1000005;
const ll MOD = 1e9 + 7;
const ll MOD1 = 1e9 + 9;
const ll magic = 727;

using namespace std;

set <ll> s;
ll step[MAXN];

struct String{
    ll hsh, pre, len;
} a[MAXN];

bool comp(String a, String b) {return (a.len > b.len);}

int main()
{

    fast_io;

    ll t, i, j;
    step[0] = 1;
    for (ll i = 1; i <= 1e6; ++i) step[i] = step[i - 1] * magic, step[i] %= MOD;

    cin >> t;
    while(t--) {
        ll n, sz = 0;
        cin >> n;
        s.clear();
        string s1;
        for (i = 1; i <= n; ++i) {
            cin >> s1;
            ll h = s1[0], z = h, h2 = 0, j, H = s1[0], H2 = 0;
            ++sz;
            a[sz].pre = -1, a[sz].hsh = h * MOD1 + H, a[sz].len = 1;
            s.insert(a[sz].hsh);
            for (j = 1; j < s1.size(); ++j) {
                h = (h * magic + s1[j]) % MOD;
                H = (H * magic + s1[j]) % MOD1;
                h2 = (h2 * magic + s1[j]) % MOD;
                H2 = (H2 * magic + s1[j]) % MOD1;
                ++sz, a[sz].pre = h2 * MOD1 * H2, a[sz].hsh = h * MOD1 * H, a[sz].len = j + 1;
                s.insert(a[sz].hsh);
            }
        }
        sort(a + 1, a + sz + 1, comp);
        for (i = 1; i <= sz; ++i) if (s.find(a[i].hsh) != s.end()) s.erase(a[i].pre);

        cout << s.size() - 1 << endl;
    }

    return 0;
}


#include <iostream>
#include <queue>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <map>
#include <vector>
#include <cstring>
#include <iomanip>
#include <set>
#include <sstream>
#include <ctime>

#define rust(a, b, c, d) sqrt(sqr(a - c) + sqr(b - d))

#define sqr(a) (a)*(a)
#define m_p make_pair
#define fi first
#define se second
#define fast_io ios_base::sync_with_stdio(0); cin.tie(0)
#define endl '\n'

typedef long long ll;
const int MAXINT=2147483640;
const ll MAXLL=9223372036854775800LL;
const ll MAXN=1000005;
const ll MOD = 1123456769;
const ll magic = 727;

using namespace std;

set <ll> s;
ll step[MAXN];

struct String{
    ll hsh, pre, len;
} a[MAXN];

bool comp(String a, String b) {return (a.len > b.len);}

int main()
{

    fast_io;

    ll t, i, j;
    step[0] = 1;
    for (ll i = 1; i <= 1e6; ++i) step[i] = step[i - 1] * magic, step[i] %= MOD;

    cin >> t;
    while(t--) {
        ll n, sz = 0;
        cin >> n;
        s.clear();
        string s1;
        for (i = 1; i <= n; ++i) {
            cin >> s1;
            ll h = s1[0], z = h, h2 = 0, j;
            ++sz;
            a[sz].pre = -1, a[sz].hsh = z, a[sz].len = 1;
            s.insert(a[sz].hsh);
            for (j = 1; j < s1.size(); ++j) {
                h = (h * magic + s1[j]) % MOD;
                h2 = (h2 * magic + s1[j]) % MOD;
                ++sz, a[sz].pre = h2, a[sz].hsh = h, a[sz].len = j + 1;
                s.insert(a[sz].hsh);

            }
        }
        sort(a + 1, a + sz + 1, comp);
        for (i = 1; i <= sz; ++i) if (s.find(a[i].hsh) != s.end()) s.erase(a[i].pre);

        cout << s.size() << endl;
    }

    return 0;
}


#include <iostream>
#include <queue>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <map>
#include <vector>
#include <cstring>
#include <iomanip>
#include <set>
#include <sstream>
#include <ctime>

#define rust(a, b, c, d) sqrt(sqr(a - c) + sqr(b - d))

#define sqr(a) (a)*(a)
#define m_p make_pair
#define fi first
#define se second
#define fast_io ios_base::sync_with_stdio(0); cin.tie(0)
#define endl '\n'

typedef long long ll;
const int MAXINT=2147483640;
const ll MAXLL=9223372036854775800LL;
const ll MAXN=1000005;
const ll MOD = 1e9 + 7;
const ll MOD1 = 1e9 + 9;
const ll magic = 727;

using namespace std;

set <ll> s;
ll step[MAXN];

struct String{
    ll hsh, pre, len;
} a[MAXN];

bool comp(String a, String b) {return (a.len > b.len);}

int main()
{

    fast_io;

    ll t, i, j;

    cin >> t;
    while(t--) {
        ll n, sz = 0;
        cin >> n;
        s.clear();
        string s1;
        for (i = 1; i <= n; ++i) {
            cin >> s1;
            ll h = s1[0], h2 = 0, j, H = s1[0], H2 = 0;
            ++sz;
            a[sz].pre = -1, a[sz].hsh = h * MOD1 + H, a[sz].len = 1;
            s.insert(a[sz].hsh);
            for (j = 1; j < s1.size(); ++j) {
                h = (h * magic + s1[j]) % MOD;
                H = (H * magic + s1[j]) % MOD1;
                h2 = (h2 * magic + s1[j]) % MOD;
                H2 = (H2 * magic + s1[j]) % MOD1;
                ++sz, a[sz].pre = h2 * MOD1 + H2, a[sz].hsh = h * MOD1 + H, a[sz].len = j + 1;
                s.insert(a[sz].hsh);
            }
        }
        sort(a + 1, a + sz + 1, comp);
        for (i = 1; i <= sz; ++i) if (s.find(a[i].hsh) != s.end()) s.erase(a[i].pre);

        cout << s.size() << endl;
    }

    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <complex>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cassert>
#include<string>
using namespace std;
typedef long long ll;
#define NN 1000500
#define gson(i,root) for(int i=ptx[root];~i;i=ed[i].next)
struct edge{
    int v,next,w;
    edge(){}
    edge(int v,int next,int w){
        this->v=v;
        this->next=next;
        this->w=w;
    }
}ed[NN*2];
int ptx[NN],lnum;
void addline(int x,int y,int w){
    ed[lnum]=edge(y,ptx[x],w);
    ptx[x]=lnum++;
}

int n;
map<string,int>mp;
string s;
int tot=0;
int dp[1005000][2];
int mark[1005000];
int degree[1005000];
void dfs(int now,int fa){
	mark[now]=1;
	dp[now][0]=0;
	dp[now][1]=1;
	gson(i,now){
		int v=ed[i].v;
		if(v==fa) continue;
		if(mark[v]==1) continue;
		dfs(v,now);
		dp[now][0]=dp[now][0]+max(dp[v][1],dp[v][0]);
		dp[now][1]=dp[now][1]+dp[v][0];
	}
}
int main(void){

	int t;
	scanf("%d",&t);
	while(t--){
		scanf("%d",&n);
		mp.clear();
		tot=0;
		int ans=0;
		memset(ptx,-1,sizeof(ptx));

		lnum=0;
		for(int i=0;i<n;i++){
			cin>>s;
			string t="";
			for(int j=0;j<s.length();j++){
				t=t+s[j];
				if(mp[t]==0) mp[t]=++tot;
			}
		}
		for(int i=0;i<=tot;i++){
			dp[i][0]=dp[i][1]=mark[i]=degree[i]=0;
		}
		for(map<string,int>::iterator it=mp.begin();it!=mp.end();it++){
			string tem=it->first;
			string tt=tem;
			string tem2=tt.erase(0,1);
		//	cout<<tem<<endl<<tem2<<endl;
			if(mp[tem2]!=0&&tem2!=""&&mp[tem]!=0){
				addline(mp[tem2],mp[tem],0);
				degree[mp[tem]]++;
			}
		}
		for(int i=1;i<=tot;i++){
			if(mark[i]==0&&degree[i]==0){
				dfs(i,-1);
				ans+=max(dp[i][0],dp[i][1]);
			}
		}
		printf("%d\n",ans);
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;

const int maxn = 1e6 + 5;
const int maxm = 26;

int tot, n;
int ch[maxn][maxm];

int newNode()
{
    memset(ch[tot], 0, sizeof ch[tot]);
    return tot++;
}

char s[maxn];

void add(char *s)
{
    int len = strlen(s);
    int cur = 0;
    for(int i = 0; i < len; ++ i)
    {
        int v = s[i] - 'a';
        if(ch[cur][v] == 0) ch[cur][v] = newNode();
        cur = ch[cur][v];
    }
}

string str[maxn];
vector<int> G[maxn];

void buildGraph(const string &s)
{
    int a = 0;
    int b = ch[0][s[0]-'a'];
    for(int i = 1; i < s.size(); ++ i)
    {
        int v = s[i] - 'a';
        a = ch[a][v];
        b = ch[b][v];
        if(a == 0 || b == 0) return;
        G[a].push_back(b);
        G[b].push_back(a);
    }
}

int f[maxn][2];
bool vis[maxn];

void dfs(int u)
{
    vis[u] = true;
    f[u][0] = 0;
    f[u][1] = 1;
    for(auto v : G[u])
    {
        if(vis[v]) continue;
        dfs(v);
        f[u][0] += max(f[v][0], f[v][1]);
        f[u][1] += f[v][0];
    }
}

int main()
{
    int T;
    cin >> T;
    while(T--)
    {
        tot = 1;
        cin >> n;
        memset(ch[0], 0, sizeof ch[0]);
        for(int i = 0; i < n; ++ i)
        {
            scanf("%s", s);
            add(s);
            str[i] = s;
        }

        for(int i = 1; i < tot; ++ i) G[i].clear();
        for(int i = 0; i < n; ++ i) buildGraph(str[i]);
        for(int i = 1; i <= tot; ++ i) vis[i] = false;

        int res = 0;
        for(int i = 1; i < tot; ++ i)
        {
            if(!vis[i])
            {
                dfs(i);
                res += max(f[i][0], f[i][1]);
            }
        }
        cout << res << endl;
    }
    return 0;
}