This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define sz(a) (int)a.size()
#define all(a) begin(a),end(a)
const int N = (int)2e6+10;
vector<int> V[N];
int T[2][4][N];
int L[N], R[N];
int n, m, I[2];
string s[N];
 
void add(string &s, int j, int t, bool r=0){
	int v = 1;
	for(int i = 0; i < sz(s); i++){
		char u = s[r?sz(s)-1-i:i];
		int c = (u=='A'?0:u=='C'?1:u=='G'?2:3);
		if(!T[t][c][v]) T[t][c][v]=I[t]++;
		v = T[t][c][v];
		if(!t){
			if(!L[v]) L[v]=j;
			R[v]=max(R[v],j);
		}
		else V[v].push_back(j);
	}
}
 
int get(string &s, int t, bool r=0){
	int v = 1;
	for(int i = 0; i < sz(s); i++){
		char u = s[r?sz(s)-1-i:i];
		int c = (u=='A'?0:u=='C'?1:u=='G'?2:3);
		v = T[t][c][v]; if(!v) return 0;
	}
	return v;
}
 
int32_t main()
{
	cin >> n >> m; I[0]=I[1]=2;
	for(int i = 0; i < n; i++) 
		cin >> s[i]; sort(s,s+n);
	for(int i = 0; i < n; i++)
		add(s[i],i+1,0),add(s[i],i+1,1,1);
	for(int i = 0; i < m; i++){
		string s, t; cin >> s >> t;
		int I = get(s,0), I2=get(t,1,1);
		int l = L[I], r=R[I];
		int p1 = lower_bound(all(V[I2]),l)-begin(V[I2]);
		int p2 = upper_bound(all(V[I2]),r)-begin(V[I2]);
		cout << max(0, p2-p1) << "\n";
	}
}
Compilation message (stderr)
selling_rna.cpp: In function 'int32_t main()':
selling_rna.cpp:40:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   40 |  for(int i = 0; i < n; i++)
      |  ^~~
selling_rna.cpp:41:16: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   41 |   cin >> s[i]; sort(s,s+n);
      |                ^~~~| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |