AC自动机模板
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <cmath>
#define INF 1e9
#define LL long long
#define ull unsigned long long
using namespace std;
const LL N=1e6+100,M=1010;
int n,t,tot,ans,q[N],nt[N],a[N][41];
int f[N];
string s;
void trie ( string s )
{
int len=s.size ( ),rt=0;
for ( int i=0;i<len;i++ ) !a[rt][s[i]-'a']?rt=a[rt][s[i]-'a']=++tot:rt=a[rt][s[i]-'a'];
f[rt]++;
}
void kmp ( )
{
int l=1,r=0;
for ( int i=0;i<26;i++ ) if ( a[0][i] ) q[++r]=a[0][i];
for ( ;l<=r;l++ )
{
int x=q[l];
for ( int i=0;i<26;i++ ) !a[x][i]?a[x][i]=a[nt[x]][i] : q[++r]=a[x][i],nt[a[x][i]]=a[nt[x]][i];
}
}
int find ( string s )
{
int len=s.size ( ),rt=0,ans=0;
for ( int i=0;i<len;i++ )
{
int c=s[i]-'a',x=a[rt][c];
while ( x ) ans+=f[x],f[x]=0,x=nt[x];
rt=a[rt][c];
}
return ans;
}
int main ( )
{
scanf ( "%d",&t );
while ( t-- )
{
tot=ans=0;
memset ( a,0,sizeof ( a ) );
memset ( f,0,sizeof ( f ) );
memset ( nt,0,sizeof ( nt ) );
scanf ( "%d",&n );
for ( int i=1;i<=n;i++ )
{
cin >> s;
trie ( s );
}
kmp ( );
cin >>s;
int ans=find ( s );
printf ( "%d\n",ans );
}
return 0;
}