#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1000;
char t[N+5],m;
int n;
char s[N+5];
int ch[N*500+5][10];
int tot;
int cnt[N*500+5];
int res;
bitset<N+5> vis[500*N+5];
int change(char s){
if(s=='A')
return 1;
if(s=='T')
return 2;
if(s=='C')
return 3;
if(s=='G')
return 4;
if(s=='?')
return 5;
return 6;
}
void insert(char s[N+5]){
int n=strlen(s+1);
int u=0;
for(int i=1;i<=n;i++){
int k=change(s[i]);
if(!ch[u][k])
ch[u][k]=++tot;
u=ch[u][k];
}
++cnt[u];
return ;
}
void dfs(int x,int trie){
if(x==m+1){
res+=cnt[trie];
cnt[trie]=0;
return ;
}
if(vis[x][trie])
return ;
vis[x][trie]=1;
int k=change(t[x]);
if(k>=1&&k<=4){
if(ch[trie][k])
dfs(x+1,ch[trie][k]);
return ;
}
if(k==5){
if(ch[trie][1])
dfs(x+1,ch[trie][1]);
if(ch[trie][2])
dfs(x+1,ch[trie][2]);
if(ch[trie][3])
dfs(x+1,ch[trie][3]);
if(ch[trie][4])
dfs(x+1,ch[trie][4]);
return ;
}
if(k==6){
dfs(x+1,trie);
if(ch[trie][1])
dfs(x+1,ch[trie][1]),dfs(x,ch[trie][1]);
if(ch[trie][2])
dfs(x+1,ch[trie][2]),dfs(x,ch[trie][2]);
if(ch[trie][3])
dfs(x+1,ch[trie][3]),dfs(x,ch[trie][3]);
if(ch[trie][4])
dfs(x+1,ch[trie][4]),dfs(x,ch[trie][4]);
}
return ;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>t+1>>n;
m=strlen(t+1);
for(int i=1;i<=n;i++){
cin>>s+1;
insert(s);
}
dfs(1,0);
cout<<n-res<<"\n";
return 0;
}