#include "bits/stdc++.h"
using namespace std;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x,y) (lower_bound(all(x),y)-x.begin())
#define ubd(x,y) (upper_bound(all(x),y)-x.begin())
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) { return (rng() % (y+1-x)) + x; } //inclusivesss
string inline to_string(char c) {string s(1,c);return s;} template<typename T> inline T gcd(T a,T b){ return a==0?llabs(b):gcd(b%a,a); }
typedef /*long long*/int ll;
typedef long double ld;
#define FOR(i,s,e) for(ll i=s;i<=ll(e);++i)
#define DEC(i,s,e) for(ll i=s;i>=ll(e);--i)
typedef pair<ll,ll>pi; typedef pair<ll,pi>spi; typedef pair<pi,pi>dpi;
#define LLINF ((long long)1e18)
#define INF int(1e9+1e6)
#define MAXN (2000006)
ll n,m;
string A[MAXN];
vector<int> fw[MAXN],v[MAXN];
struct fen {
fen() {
for(auto&i:fw)i.clear();
for(auto&i:v)i.clear();
}
void put(int x,int y){
for(;x<MAXN;x+=x&(-x)) v[x].pb(y);
}
void done(){
FOR(i,0,MAXN-1) {
v[i].pb(-INF);
sort(all(v[i])), v[i].resize(unique(all(v[i]))-v[i].begin());
fw[i].resize(v[i].size(), 0);
}
}
void update(int x,int y,ll nval){
ll oy=y;
for(;x<MAXN;x+=x&(-x)) {
y=lbd(v[x],oy);
assert(v[x][y]==oy);
for(;y<fw[x].size();y+=y&(-y)) fw[x][y] += nval;
}
}
ll sum(ll a,ll b,ll c,ll d){
return sum(b, c, d) - sum(a-1, c, d);
}
ll sum(ll x,ll a,ll b){
ll res=0;
for(;x;x-=x&(-x)) {
res += sum(x,b) - sum(x,a-1);
}
return res;
}
ll sum(ll f,ll x){
ll res=0;
ll ox=x;
x=ubd(v[f],x)-1;
assert(v[f][x]<=ox);
for(;x;x-=x&(-x)) res+=fw[f][x];
return res;
}
} fen;
ll ind(char ch){if(ch=='A')return 0;if(ch=='G')return 1;if(ch=='C')return 2;return 3;}
struct trie {
char ch='*';
ll c[4]={-1,-1,-1,-1};
void init(char x){
ch=x;
mmst(c,-1);
}
}t[2][MAXN];
int co[2]={1,1},at[2][MAXN],st[2][MAXN],en[2][MAXN],order;
int tins(int alt,int id,int i,int j){
if(t[alt][id].ch=='*'){
t[alt][id].init(A[i][j]);
}
if(A[i].size() == j+1) return id;
ll num=ind(A[i][j+1]);
if(t[alt][id].c[num] == -1) t[alt][id].c[num] = co[alt] ++;
return tins(alt,t[alt][id].c[num],i,j+1);
}
void dfs(ll alt,ll x){
st[alt][x]=order++;
for(auto i:t[alt][x].c) if(~i) {
dfs(alt,i);
}
en[alt][x]=order-1;
}
string p[2];
int find(int alt,int id,int i){
if(id == -1 || t[alt][id].ch=='*')return -1;
if(i+1==p[alt].size())return id;
int num=ind(p[alt][i+1]);
return find(alt,t[alt][id].c[num],i+1);
}
int main(){
FAST
cin>>n>>m;
FOR(i,1,n)cin>>A[i];
t[0][0].init('!'),t[1][0].init('!');
FOR(i,1,n)reverse(all(A[i]));
FOR(i,1,n)at[1][i]=tins(1,0,i,-1);
FOR(i,1,n)reverse(all(A[i]));
FOR(i,1,n)at[0][i]=tins(0,0,i,-1);
dfs(0,0), order=0, dfs(1,0);
FOR(i,1,n)fen.put(st[0][at[0][i]],st[1][at[1][i]]);
fen.done();
FOR(i,1,n)fen.update(st[0][at[0][i]],st[1][at[1][i]],1);
FOR(i,1,m){
cin>>p[0]>>p[1];
reverse(all(p[1]));
int ind[2] = {find(0,0,-1), find(1,0,-1)};
if((~ind[0])&&(~ind[1])) cout<<fen.sum(st[0][ind[0]],en[0][ind[0]],st[1][ind[1]],en[1][ind[1]])<<'\n';
else cout<<"0\n";
}
}
/*
1 1
AGA
AG GA
*/
Compilation message
Compilation timeout while compiling selling_rna