#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pl = pair<ll,ll>;
using vl = vector<ll>;
#define all(a) a.begin(),a.end()
#define sor(a) sort(all(a));
#define sz size()
#define pb push_back
#define f first
#define s second
#define ft front()
#define nl "\n"
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define F0R(i,a) FOR(i,0,a)
#define rep(a) F0R(_,a)
#define each(i,a) for(auto i:a)
void solve(){
ll n,m;
cin>>n>>m;
vl t(n);
string st;
bitset<200000>s[n],sr[n],tp,tpr,k;
F0R(i,n){
cin>>st;
t[i]=st.sz;
F0R(j,t[i]){
if(st[j]=='A')s[i][j*2]=0,s[i][j*2+1]=0;
if(st[j]=='G')s[i][j*2]=0,s[i][j*2+1]=1;
if(st[j]=='C')s[i][j*2]=1,s[i][j*2+1]=0;
if(st[j]=='U')s[i][j*2]=1,s[i][j*2+1]=1;
if(st[t[i]-1-j]=='A')sr[i][j*2]=0,sr[i][j*2+1]=0;
if(st[t[i]-1-j]=='G')sr[i][j*2]=0,sr[i][j*2+1]=1;
if(st[t[i]-1-j]=='C')sr[i][j*2]=1,sr[i][j*2+1]=0;
if(st[t[i]-1-j]=='U')sr[i][j*2]=1,sr[i][j*2+1]=1;
}
}
rep(m){
string p,q;
cin>>p>>q;
ll ps=p.sz,qs=q.sz;
bitset<200000>bt,btr;
F0R(i,ps*2)bt[i]=1;
F0R(i,qs*2)btr[i]=1;
F0R(j,ps){
if(p[j]=='A')tp[j*2]=0,tp[j*2+1]=0;
if(p[j]=='G')tp[j*2]=0,tp[j*2+1]=1;
if(p[j]=='C')tp[j*2]=1,tp[j*2+1]=0;
if(p[j]=='U')tp[j*2]=1,tp[j*2+1]=1;
}
F0R(j,qs){
if(q[qs-1-j]=='A')tpr[j*2]=0,tpr[j*2+1]=0;
if(q[qs-1-j]=='G')tpr[j*2]=0,tpr[j*2+1]=1;
if(q[qs-1-j]=='C')tpr[j*2]=1,tpr[j*2+1]=0;
if(q[qs-1-j]=='U')tpr[j*2]=1,tpr[j*2+1]=1;
}
tp&=bt;
tpr&=btr;
ll r=0;
F0R(i,n){
if(max(ps,qs)<=t[i]&&tp==(s[i]&bt)&&tpr==(sr[i]&btr))r++;
}
cout<<r<<nl;
}
}
int main(){
cin.tie(0)->sync_with_stdio(0);
freopen("test.in","r",stdin),freopen("test.out","w",stdout);
solve();
return 1<<32;
}