#include "bits/stdc++.h"
using namespace std;
const int maxn = 300300;
int trie[maxn][3], nd = 1;
char str[44];
int value[maxn], stp[maxn];
int dp[44][44];
int ok(int l, int r){
if(l == r) return 1;
if(l + 1 == r && str[l] == str[r]) return 1;
if(l + 1 == r) return 0;
if(dp[l][r] != -1) return dp[l][r];
int v = ok(l + 1, r - 1);
if(v == 0) return dp[l][r] = 0;
if(str[l] == str[r]) return dp[l][r] = 1;
return dp[l][r] = 0;
}
int count(){
memset(dp, -1, sizeof(dp));
int len = strlen(str);
int ans = 0;
for(int e = 0; e < len; e++)
for(int f = e; f < len; f++)
ans += ok(e, f);
return ans;
}
//Tested
template<typename T, bool fastQuery = true> class sparse_table{
private:
vector<vector<T> > table;
int size, lg;
void build(){
for(int bit = 1; bit < lg; bit++)
for(int ptr = 0; ptr + (1<<bit) <= size; ptr++)
table[bit][ptr] = table[bit-1][ptr] + table[bit-1][ptr + (1<<(bit-1))];
}
public:
sparse_table(int size){
assert(size > 0);
this->size = size;
lg = log2(size) + 2;
table.assign(lg, vector<T>(size));
}
sparse_table(vector<T> & initial){
*this = sparse_table(initial.size());
for(int e = 0; e < size; e++)
table[0][e] = initial[e];
build();
}
T get(int from, int to){
assert(from <= to);
T ans = T();
if(fastQuery){
int nLG = __builtin_clz(1) - __builtin_clz(to - from + 1);
ans = table[nLG][from] + table[nLG][to - (1<<nLG) + 1];
} else {
for(int e = lg-1; e >= 0; e--)
if((to + 1 - from)&(1<<e)){
ans = ans + table[e][from];
from += 1<<e;
}
}
return ans;
}
};
struct to_rm{
int ind, v;
to_rm(){
}
to_rm(int a, int b){
ind = a;
v = b;
}
to_rm operator + (to_rm other){
if(v < other.v) return other;
if(v > other.v) return * this;
if(ind < other.ind) return * this;
return other;
}
};
int main(){
int cs;
scanf("%d", &cs);
while(cs--){
int n, q;
scanf("%d %d", &n, &q);
nd = 1;
for(int e = 0; e < 3; e++) trie[0][e] = 0;
for(int e = 0; e < n; e++){
scanf("%s", str);
value[e] = count();
int cur = 0;
for(int f = 0; str[f]; f++){
int & v = trie[cur][str[f]-'a'];
if(v == 0){
for(int g = 0; g < 3; g++) trie[nd][g] = 0;
v = nd++;
}
cur = v;
}
// cout <<e << " " << value[e] << endl;
stp[cur] = e;
}
vector<to_rm> lol;
for(int e = 0; e < n;e++) lol.push_back(to_rm(e, value[e]));
sparse_table<to_rm> sss(lol);
for(int e = 0; e < q; e++){
char s1[44], s2[44];
int l, r;
scanf("%s %s", s1, s2);
int cur = 0;
for(int f = 0; s1[f]; f++){
cur = trie[cur][s1[f]-'a'];
}
l = stp[cur];
cur = 0;
for(int f = 0; s2[f]; f++){
cur = trie[cur][s2[f]-'a'];
}
r = stp[cur];
if(l > r) swap(l, r);
// cout << l << " " << r << endl;
printf("%d\n", sss.get(l, r).ind + 1);
}
}
return 0;
}
I2luY2x1ZGUgImJpdHMvc3RkYysrLmgiCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmNvbnN0IGludCBtYXhuID0gMzAwMzAwOwppbnQgdHJpZVttYXhuXVszXSwgbmQgPSAxOwpjaGFyIHN0cls0NF07CmludCB2YWx1ZVttYXhuXSwgc3RwW21heG5dOwppbnQgZHBbNDRdWzQ0XTsKaW50IG9rKGludCBsLCBpbnQgcil7CglpZihsID09IHIpIHJldHVybiAxOwoJaWYobCArIDEgPT0gciAmJiBzdHJbbF0gPT0gc3RyW3JdKSByZXR1cm4gMTsKCWlmKGwgKyAxID09IHIpIHJldHVybiAwOwoJaWYoZHBbbF1bcl0gIT0gLTEpIHJldHVybiBkcFtsXVtyXTsKCWludCB2ID0gb2sobCArIDEsIHIgLSAxKTsKCWlmKHYgPT0gMCkgcmV0dXJuIGRwW2xdW3JdID0gMDsKCWlmKHN0cltsXSA9PSBzdHJbcl0pIHJldHVybiBkcFtsXVtyXSA9IDE7CglyZXR1cm4gZHBbbF1bcl0gPSAwOwp9CmludCBjb3VudCgpewoJbWVtc2V0KGRwLCAtMSwgc2l6ZW9mKGRwKSk7CglpbnQgbGVuID0gc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Zyb20sIGludCB0byl7CiAgICAgICAgYXNzZXJ0KGZyb20gPD0gdG8pOwogICAgICAgIFQgYW5zID0gVCgpOwogICAgICAgIGlmKGZhc3RRdWVyeSl7CiAgICAgICAgICAgIGludCBuTEcgPSBfX2J1aWx0aW5fY2x6KDEpIC0gX19idWlsdGluX2Nseih0byAtIGZyb20gKyAxKTsKICAgICAgICAgICAgYW5zID0gdGFibGVbbkxHXVtmcm9tXSArIHRhYmxlW25MR11bdG8gLSAoMTw8bkxHKSArIDFdOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIGZvcihpbnQgZSA9IGxnLTE7IGUgPj0gMDsgZS0tKQogICAgICAgICAgICAgICAgaWYoKHRvICsgMSAtIGZyb20pJigxPDxlKSl7CiAgICAgICAgICAgICAgICAgICAgYW5zID0gYW5zICsgdGFibGVbZV1bZnJvbV07CiAgICAgICAgICAgICAgICAgICAgZnJvbSArPSAxPDxlOwogICAgICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICByZXR1cm4gYW5zOwogICAgfQp9OwpzdHJ1Y3QgdG9fcm17CglpbnQgaW5kLCB2OwoJdG9fcm0oKXsKCgl9Cgl0b19ybShpbnQgYSwgaW50IGIpewoJCWluZCA9IGE7CgkJdiA9IGI7Cgl9Cgl0b19ybSBvcGVyYXRvciArICh0b19ybSBvdGhlcil7CgkJaWYodiA8IG90aGVyLnYpIHJldHVybiBvdGhlcjsKCQlpZih2ID4gb3RoZXIudikgcmV0dXJuICogdGhpczsKCQlpZihpbmQgPCBvdGhlci5pbmQpIHJldHVybiAqIHRoaXM7CgkJcmV0dXJuIG90aGVyOwoJfQp9OwppbnQgbWFpbigpewoJaW50IGNzOwoJc2NhbmYoIiVkIiwgJmNzKTsKCXdoaWxlKGNzLS0pewoJCWludCBuLCBxOwoJCXNjYW5mKCIlZCAlZCIsICZuLCAmcSk7CgkJbmQgPSAxOwoJCWZvcihpbnQgZSA9IDA7IGUgPCAzOyBlKyspIHRyaWVbMF1bZV0gP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