#include <stdio.h>
#include <cstring>
#include <queue>
using namespace std;
const int N=26;
const int maxN=50005;
struct Trie{
int fail[maxN],nxt[maxN][N],end[maxN];
int root,tot;
int new_node()
{
for (int i=0; i<N; i++)
nxt[tot][i]=-1;
end[tot++]=0;
return tot-1;
}
void init()
{
tot=0;
root=new_node();
}
void insert(char buf[])
{
int len=strlen(buf);
int now=root;
for (int i=0; i<len; i++)
{
int k=buf[i]-'a';
if (nxt[now][k]==-1) nxt[now][k]=new_node();
now=nxt[now][k];
}
end[now]++;
}
void build()
{
queue <int> p;
fail[root]=root;
for (int i=0; i<N; i++)
{
if (nxt[root][i]==-1) nxt[root][i]=root;
else
{
fail[nxt[root][i]]=root;
p.push(nxt[root][i]);
}
}
while (!p.empty())
{
int now=p.front();
p.pop();
for (int i=0; i<N; i++)
{
if (nxt[now][i]!=-1)
{
fail[nxt[now][i]]=nxt[fail[now]][i];
p.push(nxt[now][i]);
} else
nxt[now][i]=nxt[fail[now]][i];
}
}
}
int query(char buf[])
{
int len=strlen(buf);
int now=root;
int ans=0;
for (int i=0; i<len; i++)
{
now=nxt[now][buf[i]-'a'];
int temp=now;
while (temp!=root)
{
ans+=end[temp];
temp=fail[temp];
}
}
return ans;
}
}ac;
char buf[maxN+maxN];
int main()
{
freopen("AC自动机.in","r",stdin);
freopen("AC自动机.out","w",stdout);
int T;
scanf("%d",&T);
while (T--)
{
int n;
scanf("%d",&n);
ac.init();
for (int i=1; i<=n; i++)
{
scanf("%s",&buf);
ac.insert(buf);
}
ac.build();
scanf("%s",&buf);
printf("%d\n",ac.query(buf));
}
return 0;
}
09-12