#include <queue>
#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 2 * 1e6 + 9 ;
int trie[ maxn] [ 26 ] ;
int cntword[ maxn] ;
int fail[ maxn] ;
int cnt = 0 ;
void insertWords ( string s) {
int root = 0 ;
for ( int i= 0 ; i< s. size ( ) ; i++ ) {
int next = s[ i] - 'a' ;
if ( ! trie[ root] [ next] )
trie[ root] [ next] = ++ cnt;
root = trie[ root] [ next] ;
}
cntword[ root] ++ ;
}
void getFail ( ) {
queue < int > q;
for ( int i= 0 ; i< 26 ; i++ ) {
if ( trie[ 0 ] [ i] ) {
fail[ trie[ 0 ] [ i] ] = 0 ;
q. push ( trie[ 0 ] [ i] ) ;
}
}
while ( ! q. empty ( ) ) {
int now = q. front ( ) ;
q. pop ( ) ;
for ( int i= 0 ; i< 26 ; i++ ) {
if ( trie[ now] [ i] ) {
fail[ trie[ now] [ i] ] = trie[ fail[ now] ] [ i] ;
q. push ( trie[ now] [ i] ) ;
}
else
trie[ now] [ i] = trie[ fail[ now] ] [ i] ;
}
}
}
int query ( string s) {
int now = 0 , ans = 0 ;
for ( int i= 0 ; i< s. size ( ) ; i++ ) {
now = trie[ now] [ s[ i] - 'a' ] ;
for ( int j= now; j && cntword[ j] != - 1 ; j= fail[ j] ) {
ans + = cntword[ j] ;
cntword[ j] = - 1 ;
}
}
return ans;
}
void init ( )
{
cnt= 0 ;
memset ( trie, 0 , sizeof trie) ;
memset ( cntword, 0 , sizeof cntword) ;
memset ( fail, 0 , sizeof fail) ;
}
int main ( )
{
ios:: sync_with_stdio ( false ) ;
cin. tie ( 0 ) ;
int t;
cin>> t;
while ( t-- )
{
int n;
init ( ) ;
string s;
cin >> n;
for ( int i= 0 ; i< n; i++ )
{
cin >> s ;
insertWords ( s) ;
}
fail[ 0 ] = 0 ;
getFail ( ) ;
cin >> s ;
cout << query ( s) << endl;
}
return 0 ;
}
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int N= 26 ;
const int MAXN= 500005 ;
struct Trie{
int next[ MAXN] [ N] , fail[ MAXN] , end[ MAXN] ;
int root;
int tot;
int newnode ( )
{
for ( int i= 0 ; i< N; i++ )
next[ tot] [ i] = - 1 ;
end[ tot++ ] = 0 ;
return tot- 1 ;
}
void init ( )
{
tot= 0 ;
root= newnode ( ) ;
}
void insert ( char buf[ ] )
{
int len= strlen ( buf) ;
int now= root;
for ( int i= 0 ; i< len; i++ )
{
int k= buf[ i] - 'a' ;
if ( next[ now] [ k] == - 1 )
next[ now] [ k] = newnode ( ) ;
now= next[ now] [ k] ;
}
end[ now] ++ ;
}
void build ( )
{
queue< int > que;
fail[ root] = root;
for ( int i= 0 ; i< N; i++ )
if ( next[ root] [ i] == - 1 )
next[ root] [ i] = root;
else
{
fail[ next[ root] [ i] ] = root;
que. push ( next[ root] [ i] ) ;
}
while ( ! que. empty ( ) )
{
int now = que. front ( ) ;
que. pop ( ) ;
for ( int i= 0 ; i< N; i++ )
if ( next[ now] [ i] == - 1 )
next[ now] [ i] = next[ fail[ now] ] [ i] ;
else
{
fail[ next[ now] [ i] ] = next[ fail[ now] ] [ i] ;
que. push ( next[ now] [ i] ) ;
}
}
}
int query ( char buf[ ] )
{
int len= strlen ( buf) ;
int now= root;
int res= 0 ;
for ( int i= 0 ; i< len; i++ )
{
now= next[ now] [ buf[ i] - 'a' ] ;
int temp= now;
while ( temp!= root)
{
res+ = end[ temp] ;
end[ temp] = 0 ;
temp= fail[ temp] ;
}
}
return res;
}
} ;
Trie ac;
char buf[ MAXN+ MAXN] ;
int main ( )
{
int T;
scanf ( "%d" , & T) ;
while ( T-- )
{
int n;
scanf ( "%d" , & n) ;
ac. init ( ) ;
for ( int i= 0 ; i< n; i++ )
{
scanf ( "%s" , buf) ;
ac. insert ( buf) ;
}
ac. build ( ) ;
scanf ( "%s" , buf) ;
printf ( "%d\n" , ac. query ( buf) ) ;
}
return 0 ;
}