#include<bits/stdc++.h>
using namespace std;
typedef long long int Long;
//typedef long long int ll;
typedef int ll;
typedef ll ft;
typedef set< int > si;
typedef long long Long;
typedef vector< int > vi;
typedef vector< vi> vii;
typedef vector< Long> vl;
typedef pair< int ,int > pii;
typedef pair< Long,Long> pll;
typedef pair< string,int > psi;
typedef pair< double ,double > pdd;
#define get getchar_unlocked
#define put putchar_unlocked
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define sz size()
#define ln length()
#define repstl(i, s) for (__typeof((s).end())i=(s).begin();i!=(s).end();++i)
#define debug1(s,a) cout << s << " " << a << " " << endl;
#define debug2(s,a,b) cout << s << " " << a << " " << b << " " << endl
#define debug3(s,a,b,c) cout << s << " " << a << " " << b << " " << c << " " << endl;
#define debug4(s,a,b,c,d) cout << s << " " << a << " " << b << " " << c << " " << d << " " << endl;
#define debug5(s,a,b,c,d,e) cout << s << " " << a << " " << b << " " << c << " " << d << " " << e << " " << endl;
#define PI 3.1415926535897932384626433832795
#define FO freopen ("out.txt", "w", stdout)
#define FI freopen ("in.txt", "r", stdin)
#define ref(i,a,n) for(int i=a;i<=n;i++)
#define reb(i,n,a) for(int i=n;i>=a;i--)
#define rep(i,n) for(int i=0;i<n;i++)
#define all(a) a.begin(),a.end()
#define gi(n) scanf("%d",&n)
#define gii(n) scanf("%lld",&n)
#define gc(c) scanf(" %c",&c)
#define gs(s) scanf(" %s",s);
#define pi(n) printf("%d",n)
#define pii(n) printf("%lld",n)
#define pc(c) printf("%c",c)
#define ps printf(" ")
#define pn printf("\n")
#define pl(a) printf("%s",a)
#define l(a) 2*a+1
#define r(a) 2*a+2
#define left(a,b) a,(a+b)/2
#define right(a,b) (a+b)/2+1,b
#define mid(a,b) (a+b)/2
void gl( char * str) { register char c= 0 ; register int i= 0 ; while ( c< 33 ) c= get( ) ; while ( c! = '\n ' ) { str[ i] = c; c= get( ) ; i= i+ 1 ; } str[ i] = '\0 ' ; }
void gfi( ft & x) { register ft c = get( ) ; x = 0 ; ft sn= 1 ; for ( ; ( c< 48 || c> 57 ) ; c = get( ) ) if ( c== '-' ) sn= - 1 ; for ( ; c> 47 && c< 58 ; c = get( ) ) { x = ( x<< 1 ) + ( x<< 3 ) + c - 48 ; } x* = sn; }
//int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
//int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction
//int dx[]={2,1,-1,-2,-1,1};int dy[]={0,1,1,0,-1,-1}; //Hexagonal Direction
string s[ 26 ] = { "01" ,"1000" ,"1010" ,"100" ,"0" ,"0010" ,"110" ,"0000" ,"00" ,"0111" ,"101" ,"0100" ,"11" ,"10" ,"111" ,"0110" ,"1101" ,"010" ,"000" ,"1" ,"001" ,"0001" ,"011" ,"1001" ,"1011" ,"1100" } ;
struct node { ll val; struct node * l,* r; } ;
typedef struct node nod;
nod * create( ) {
nod * tem= ( nod * ) malloc ( sizeof ( nod) ) ;
tem- > l= tem- > r= NULL ;
tem- > val= 0 ;
return tem;
}
#define N 100000
string str,temp;
ll l1,l2,dp[ N] ;
void trie( nod * head,ll ind) {
if ( ind== l2) {
head- > val= 1 ;
return ;
}
nod * tem= create( ) ;
if ( temp[ ind] == '0' ) {
if ( head- > l== NULL ) head- > l= tem;
trie( head- > l,ind+ 1 ) ;
} else {
if ( head- > r== NULL ) head- > r= tem;
trie( head- > r,ind+ 1 ) ;
}
}
ll go( nod * Head,nod * head,ll ind) {
if ( ind== l1) {
if ( head- > val== 1 ) return 1 ; else return 0 ;
}
ll ans= 0 ;
if ( str[ ind] == '.' ) {
ans= head- > l== NULL ? ans: ans+ go( Head,head- > l,ind+ 1 ) ;
} else {
ans= head- > r== NULL ? ans: ans+ go( Head,head- > r,ind+ 1 ) ;
}
if ( head- > val== 1 ) {
if ( dp[ ind] ! = - 1 ) return dp[ ind] ;
ans= ans+ go( Head,Head,ind) ;
return dp[ ind] = ans;
}
return ans;
}
void solve( ) {
cin >> str;
l1= str.ln ;
ll n;
gfi( n) ;
nod * head;
head= create( ) ;
rep( i,n) {
string temp1;
cin >> temp1;
ll l= temp1.ln ;
temp= "" ;
rep( i,l) temp+ = s[ temp1[ i] - 'A' ] ;
l2= temp.ln ;
trie( head,0 ) ;
}
rep( i,l1+ 1 ) dp[ i] = - 1 ;
pi( go( head,head,0 ) ) ; pn;
}
int main( ) {
ll t;
gfi( t) ;
while ( t-- ) {
solve( ) ;
}
return 0 ;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKdHlwZWRlZiBsb25nIGxvbmcgaW50IExvbmc7Ci8vdHlwZWRlZiBsb25nIGxvbmcgaW50IGxsOwp0eXBlZGVmIGludCBsbDsKdHlwZWRlZiBsbCBmdDsKdHlwZWRlZiBzZXQ8aW50PiBzaTsKdHlwZWRlZiBsb25nIGxvbmcgTG9uZzsKdHlwZWRlZiB2ZWN0b3I8aW50PiB2aTsKdHlwZWRlZiB2ZWN0b3I8dmk+IHZpaTsKdHlwZWRlZiB2ZWN0b3I8TG9uZz52bDsKdHlwZWRlZiBwYWlyPGludCxpbnQ+cGlpOwp0eXBlZGVmIHBhaXI8TG9uZyxMb25nPnBsbDsKdHlwZWRlZiBwYWlyPHN0cmluZyxpbnQ+cHNpOwp0eXBlZGVmIHBhaXI8ZG91YmxlLGRvdWJsZT5wZGQ7CiNkZWZpbmUgZ2V0IGdldGNoYXJfdW5sb2NrZWQKI2RlZmluZSBwdXQgcHV0Y2hhcl91bmxvY2tlZAojZGVmaW5lIHBiIHB1c2hfYmFjawojZGVmaW5lIG1wIG1ha2VfcGFpcgojZGVmaW5lIGZmIGZpcnN0CiNkZWZpbmUgc3Mgc2Vjb25kCiNkZWZpbmUgc3ogc2l6ZSgpCiNkZWZpbmUgbG4gbGVuZ3RoKCkKI2RlZmluZSByZXBzdGwoaSw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