HDU2222
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<queue>
using namespace std;
struct Trie
{
Trie *next[26];
Trie *fail;
int sum;
Trie()
{
sum=0;
fail=NULL;
for(int i=0;i<26;i++)
next[i]=NULL;
}
};
Trie *root=NULL;
int ans=0;
void CreatTrie(char *str)
{
int len=strlen(str);
Trie *p=root;
for(int i=0;i<len;i++)
{
int id=str[i]-'a';
if(p->next[id]==NULL)
{
Trie *q=new Trie();
p->next[id]=q;
}
p=p->next[id];
}
p->sum++;
return;
}
void BuildFail()
{
queue<Trie*> q;
q.push(root);
Trie *p,*tmp;
while(!q.empty())
{
tmp=q.front();
q.pop();
for(int i=0;i<26;i++)
{
if(tmp->next[i]!=NULL)
{
if(tmp==root)
tmp->next[i]->fail=root;
else
{
p=tmp->fail;
while(p!=NULL)
{
if(p->next[i]!=NULL)
{
tmp->next[i]->fail=p->next[i];
break;
}
p=p->fail;
}
if(p==NULL)
tmp->next[i]->fail=root;
}
q.push(tmp->next[i]);
}
}
}
return;
}
void AC(char *str)
{
Trie *p=root;
int len=strlen(str);
for(int i=0;i<len;i++)
{
int id=str[i]-'a';
while(p->next[id]==NULL&&p!=root)
p=p->fail;
p=p->next[id];
if(p==NULL)
p=root;
Trie *tmp=p;
while(tmp!=root)
{
if(tmp->sum>=0)
{
ans+=tmp->sum;
tmp->sum=-1;
}
else
break;
tmp=tmp->fail;
}
}
}
char String[100],str[10000100];
int main()
{
int T,n;
scanf("%d",&T);
while(T--)
{
root=new Trie();
scanf("%d",&n);
getchar();
for(int i=1;i<=n;i++)
{
gets(String);
CreatTrie(String);
}
gets(str);
ans=0;
BuildFail();
AC(str);
printf("%d\n",ans);
}
return 0;
}