#include <bits/stdc++.h>
using namespace std;
int trie[800010][3], k, ans;
long long dp[10010];
bool visited[10010];
string s;
map<char, string> Map;
void insert(string s1){
int index, node = 0;
string temp;
for(int i=0 ; s1[i] ; i++){
for(int j=0 ; Map[s1[i]][j] ; j++){
if(Map[s1[i]][j] == '.')
index = 0;
else
index = 1;
if(trie[node][index] == -1){
trie[node][index] = k++;
}
node = trie[node][index];
}
}
if(trie[node][2] == -1)
trie[node][2] = 1;
else
trie[node][2]++;
}
void search(int i, int len, int node, int last){
int index;
if(i == len){
if(trie[node][2] != -1)
dp[last] += trie[node][2];
return;
}
if(s[i] == '.')
index = 0;
else
index = 1;
if(trie[node][2] != -1){
if(!visited[i]){
visited[i] = true;
if(trie[0][index] != -1){
search(i+1, len, trie[0][index], i);
dp[last] += dp[i];
}
} else {
dp[last] += dp[i];
}
}
if(trie[node][index] != -1){
search(i+1, len, trie[node][index], last);
}
}
void initMap(){
Map['A'] = ".-", Map['B'] = "-...", Map['C'] = "-.-.", Map['D'] = "-..";
Map['E'] = "." , Map['F'] = "..-.", Map['G'] = "--.", Map['H'] = "....";
Map['I'] = "..", Map['J'] = ".---", Map['K'] = "-.-", Map['L'] = ".-..";
Map['M'] = "--", Map['N'] = "-.", Map['O'] = "---", Map['P'] = ".--.";
Map['Q'] = "--.-", Map['R'] = ".-.", Map['S'] = "...", Map['T'] = "-";
Map['U'] = "..-", Map['V'] = "...-", Map['W'] = ".--", Map['X'] = "-..-";
Map['Y'] = "-.--", Map['Z'] = "--..";
}
int main(){
int t;
cin>>t;
initMap();
while(t--){
string s1;
cin>>s;
k = 1;
int l = s.length();
memset(trie, -1, sizeof(trie));
memset(dp, 0, sizeof(dp));
int n;
cin>>n;
for(int i=0 ; i<n ; i++){
cin>>s1;
insert(s1);
}
search(0, l, 0, 0);
cout<<dp[0]<<endl;
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IHRyaW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