#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define FOR(i, a, b) for (int i = a, _b = b; i <= _b; ++i)
#define FORD(i, a, b) for (int i = a, _b = b; i >= _b; --i)
#define FORLL(i, a, b) for (ll i = a, _b = b; i <= _b; ++i)
#define FORDLL(i, a, b) for (ll i = a, _b = b; i >= _b; --i)
#define all(x) x.begin(), x.end()
#define uni(x) sort(all(x)), x.erase(unique(all(x)), x.end())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define dbg(...) debug(#__VA_ARGS__, __VA_ARGS__)
template<typename T>
void __print_one(const char *&s, const T &x)
{
while (*s == ' ') ++s;
const char *p = s;
int bal = 0;
while (*s)
{
if (*s == '(') ++bal;
else if (*s == ')') --bal;
else if (*s == ',' && bal == 0) break;
++s;
}
cerr.write(p, s - p) << " = " << x;
if (*s == ',')
{
++s;
cerr << " , ";
}
}
template<typename... Args>
void debug(const char *s, Args... args)
{
cerr << "[ ";
int dummy[] = { 0 , ( __print_one(s, args) , 0 )... };
(void)dummy;
cerr << " ]\n\n";
}
template<class X>
bool maximize(X &a, X b)
{
if (a < b)
{
a = b;
return true;
}
return false;
}
template<class X>
bool minimize(X &a, X b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
// --------------------------------------------------------------------------------------------
const int maxn = 1e5 + 3;
int n, q, ans[maxn];
string St[maxn], revSt[maxn];
// --------------------------------------------------------------------------------------------
int get_type(const char &ch)
{
if (ch == 'A') return 0;
if (ch == 'G') return 1;
if (ch == 'U') return 2;
return 3;
}
char get_char(int type)
{
if (type == 0) return 'A';
if (type == 1) return 'G';
if (type == 2) return 'U';
return 'C';
}
struct Node
{
int to[4], cnt, iS, hv, sz;
vector<pair<string, int>> qrs;
Node()
{
memset(to, 0, sizeof to);
cnt = 0;
iS = 0;
hv = -1;
sz = 0;
}
};
vector<Node> trie, revTrie;
void addString(vector<Node> &trie, const string &s, int iS = 0, int delta = 1)
{
int u = 0;
for (const char &ch : s)
{
int cur = get_type(ch);
if (!trie[u].to[cur])
{
trie.push_back(Node());
trie[u].to[cur] = int(trie.size()) - 1;
}
u = trie[u].to[cur];
trie[u].sz += delta;
}
trie[u].cnt += delta;
trie[u].iS = iS;
}
int findString(vector<Node> &trie, const string &s)
{
int u = 0;
for (const &ch : s)
{
int cur = get_type(ch);
if (!trie[u].to[cur]) return -1;
u = trie[u].to[cur];
}
return u;
}
int dfs_sz(int u)
{
int sz = int(St[trie[u].iS].size());
int mx_sz = 0;
FOR(i, 0, 3)
{
if (trie[u].to[i] == 0) continue;
int con_sz = dfs_sz(trie[u].to[i]);
if (con_sz > mx_sz)
{
mx_sz = con_sz;
trie[u].hv = i;
}
sz += con_sz;
}
return sz;
}
void ChangeTrie(int u, int delta)
{
if (trie[u].iS)
addString(revTrie, revSt[trie[u].iS], trie[u].iS, trie[u].cnt * delta);
FOR(i, 0, 3)
{
if (trie[u].to[i] == 0) continue;
ChangeTrie(trie[u].to[i], delta);
}
}
void sack(int u, bool keep)
{
FOR(i, 0, 3)
{
if (i == trie[u].hv || trie[u].to[i] == 0) continue;
sack(trie[u].to[i], false);
}
if (trie[u].hv != -1)
sack(trie[u].to[trie[u].hv], true);
if (trie[u].iS)
{
addString(revTrie, revSt[trie[u].iS], trie[u].iS, trie[u].cnt);
}
// Them trong cay con
FOR(i, 0, 3)
{
if (i == trie[u].hv || trie[u].to[i] == 0) continue;
ChangeTrie(trie[u].to[i], 1);
}
// Answer
for (auto &e : trie[u].qrs)
{
string &s = e.fi; int id = e.se;
int cur = findString(revTrie, s);
if (cur == -1) continue;
ans[id] = revTrie[cur].sz;
}
if (keep) return;
ChangeTrie(u, -1);
}
void solve()
{
cin >> n >> q;
trie.push_back(Node());
revTrie.push_back(Node());
FOR(i, 1, n)
{
cin >> St[i];
revSt[i] = St[i];
reverse(all(revSt[i]));
addString(trie, St[i], i, 1);
}
dfs_sz(0);
FOR(iq, 1, q)
{
string P, Q; cin >> P >> Q;
reverse(all(Q));
int id = findString(trie, P);
if (id != -1)
{
trie[id].qrs.push_back({Q, iq});
}
}
sack(0, true);
FOR(iq, 1, q)
cout << ans[iq] << '\n';
}
signed main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define TASK "TEST"
if (fopen(TASK".INP", "r"))
{
freopen(TASK".INP", "r", stdin);
freopen(TASK".OUT", "w", stdout);
}
solve();
return 0;
}