#include<bits/stdc++.h>
#include<fstream>
using namespace std;
ifstream fin("WINTER.inp");
ofstream fout("WINTER.out");
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
const ld PI = 3.14159265359;
const ll mod = 1e9 + 7;
const int mxn = 1e5, mxm = 1e5;
int n, m, ans = 0;
string s[mxn + 1], t[mxn + 1];
int to[1005];
struct Node1{
Node1 *nxt[4];
int l = n + 1, r = -1;
Node1(){
for(int i = 0; i < 4; i++)nxt[i] = NULL;
}
};
struct Node2{
Node2 *nxt[4];
vt<int>pos;
Node2(){
forr(i, 0, 4)nxt[i] = NULL;
}
};
Node1 *root = new Node1();
Node2 *root2 = new Node2();
void add1(string s, int id){
Node1 *nd = root;
for(int i = 0; i < s.size(); i++){
int c = to[s[i]];
if(nd->nxt[c] == NULL){
nd->nxt[c] = new Node1();
}
nd->nxt[c]->l = min(nd->nxt[c]->l, id); nd->nxt[c]->r = max(nd->nxt[c]->r, id);
nd = nd->nxt[c];
}
}
void add2(string s, int id){
Node2 *nd = root2;
for(int i = 0; i < s.size(); i++){
int c = to[s[i]];
if(nd->nxt[c] == NULL){
nd->nxt[c] = new Node2();
}
nd->nxt[c]->pos.pb(id);
nd = nd->nxt[c];
}
}
pii get1(string s){
Node1 *nd = root;
for(int i = 0; i < s.size(); i++){
int c = to[s[i]];
if(nd->nxt[c] == NULL)return(make_pair(-1, -1));
nd = nd->nxt[c];
}
return(make_pair(nd->l, nd->r));
}
int get2(string s, int l, int r){
Node2 *nd = root2;
for(int i = 0; i < s.size(); i++){
int c = to[s[i]];
if(nd->nxt[c] == NULL){
return(0);
}
nd = nd->nxt[c];
}
vt<int>v = nd->pos;
return(upper_bound(v.begin(), v.end(), r) - lower_bound(v.begin(), v.end(), l));
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
forr(i, 0, n)cin >> s[i];
to['A'] = 0; to['U'] = 1; to['C'] = 2; to['G'] = 3;
sort(s, s + n);
for(int i = 0; i < n; i++){
add1(s[i], i);
reverse(s[i].begin(), s[i].end());
add2(s[i], i);
}
forr(i, 0, m){
string p, q; cin >> p >> q;
auto [l, r] = get1(p);
cout << get2(q, l, r) << "\n";
}
return(0);
}