#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main(void)
{
freopen("hotela.in","r",stdin);
freopen("hotela.out","w",stdout);
int i,j,k,n,c,lc,minlc,lt;
char core[52],tar[52];
bool f[52][52];
while (cin.peek()!=EOF)
{
c=0;
scanf("%s\n",&core);
lc=strlen(core);
for (i=1;i<lc;i++)
if (core[i-1]=='*'&&core[i]=='*')
{
for (j=i;j<lc;j++)
core[j-1]=core[j];
lc--;
i--;
}
minlc=0;
for (i=0;i<lc;i++)
if (core[i]!='*')
minlc++;
scanf("%d\n",&n);
for (i=1;i<=n;i++)
{
scanf("%s\n",&tar);
lt=strlen(tar);
if (lt<minlc)
continue;
f[0][0]=true;
for (j=1;j<=lc;j++)
for (k=1;k<=lt;k++)
{
if (core[j-1]!='*'&&core[j-1]!='?')
f[j][k]=f[j-1][k-1]&&(core[j-1]==tar[k-1]);
else if (core[j-1]=='?')
f[j][k]=f[j-1][k-1];
else// if (core[j-1]=='*')
f[j][k]=f[j-1][k-1]||f[j][k-1];
}
if (f[lc][lt]==1)
c++;
}
printf("%d\n",c);
}
return(0);
}