#include<bits/stdc++.h>
using namespace std;
#define forn(i,n) for(int i=0;i<(int)(n); i++)
#define forsn(i,s,n) for(int i=(s);i<(int)(n); i++)
#define debug(x) cout << #x << " = " << x << endl
#define pb push_back
#define mp make_pair
typedef long long tint;
vector<int> rmq, arr;
int NEUTRO=-1;
int res(int nodo, int l, int r, int i, int j){
if(j<=l || i>=r)return NEUTRO;
if(i<=l && j>=r){
return rmq[nodo];
}
int i1=res(nodo*2, l, (l+r)/2, i, j);
int i2=res(nodo*2+1, (l+r)/2, r, i, j);
if(i1==-1)return i2;
if(i2==-1)return i1;
if(arr[i1]>=arr[i2])return i1;
return i2;
}
void build(){
for(int i=0; i<2*(int)(arr.size()); i++)rmq.push_back(0);
for(int i=(int)(arr.size()); i<2*(int)(arr.size()); i++){
rmq[i]=i-arr.size();
}
for(int i=(int)arr.size()-1; i>=1; i--){
if(arr[rmq[2*i]]>=arr[rmq[2*i+1]]){
rmq[i]=rmq[2*i];
}else{
rmq[i]=rmq[2*i+1];
}
}
}
int CountPS(char str[], int n){
int dp[n][n];
memset(dp, 0, sizeof(dp));
bool P[n][n];
memset(P, false , sizeof(P));
for (int i= 0; i< n; i++)
P[i][i] = true;
for (int i=0; i<n-1; i++){
if (str[i] == str[i+1]){
P[i][i+1] = true;
dp[i][i+1] = 1 ;
}
}
for (int gap=2 ; gap<n; gap++){
for (int i=0; i<n-gap; i++){
int j = gap + i;
if (str[i] == str[j] && P[i+1][j-1] )
P[i][j] = true;
if (P[i][j] == true)
dp[i][j] = dp[i][j-1] + dp[i+1][j] + 1 - dp[i+1][j-1];
else
dp[i][j] = dp[i][j-1] + dp[i+1][j] - dp[i+1][j-1];
}
}
return dp[0][n-1];
}
void solve(){
arr.clear();
rmq.clear();
int n,m;
scanf("%d %d", &n, &m);
map<string, int> id;
forn(i, n){
char tmp[44];
scanf("%s", tmp);
int nu=CountPS(tmp, sizeof(tmp));
arr.pb(nu+sizeof(tmp));
id[tmp]=i;
}
int p2=1;
while(p2<(int)arr.size())p2*=2;
while(p2>(int)arr.size())arr.pb(0);
build();
while(m--){
char s1[44], s2[44];
scanf("%s %s", s1, s2);
int l=id[s1];
int r=id[s2];
r++;
cout<<res(1, 0, (int)arr.size(), l, r)+1<<endl;
}
}
int main(){
int T;
scanf("%d", &T);
while(T--)solve();
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgojZGVmaW5lIGZvcm4oaSxuKSBmb3IoaW50IGk9MDtpPChpbnQpKG4pOyBpKyspCiNkZWZpbmUgZm9yc24oaSxzLG4pIGZvcihpbnQgaT0ocyk7aTwoaW50KShuKTsgaSsrKQojZGVmaW5lIGRlYnVnKHgpIGNvdXQgPDwgI3ggPDwgIiA9ICIgIDw8IHggPDwgZW5kbAojZGVmaW5lIHBiIHB1c2hfYmFjawojZGVmaW5lIG1wIG1ha2VfcGFpcgoKdHlwZWRlZiBsb25nIGxvbmcgdGludDsKCnZlY3RvcjxpbnQ+IHJtcSwgYXJyOwoKaW50IE5FVVRSTz0tMTsKCmludCByZXMoaW50IG5vZG8sIGludCBsLCBpbnQgciwgaW50IGksIGludCBqKXsKCWlmKGo8PWwgfHwgaT49cilyZXR1cm4gTkVVVFJPOwoJaWYoaTw9bCAmJiBqPj1yKXsKCQlyZXR1cm4gcm1xW25vZG9dOwoJfQoJaW50IGkxPXJlcyhub2RvKjIsIGwsIChsK3IpLzIsIGksIGopOwoJaW50IGkyPXJlcyhub2RvKjIrMSwgKGwrcikvMiwgciwgaSwgaik7CglpZihpMT09LTEpcmV0dXJuIGkyOwoJaWYoaTI9PS0xKXJldHVybiBpMTsKCWlmKGFycltpMV0+PWFycltpMl0pcmV0dXJuIGkxOwoJcmV0dXJuIGkyOwp9CgoKCnZvaWQgYnVpbGQoKXsKCWZvcihpbnQgaT0wOyBpPDIqKGludCkoYXJyLnNpemUoKSk7IGkrKylybXEucHVzaF9iYWNrKDApOwoJZm9yKGludCBpPShpbnQpKGFyci5zaXplKCkpOyBpPDIqKGludCkoYXJyLnNpemUoKSk7IGkrKyl7CgkJcm1xW2ldPWktYXJyLnNpemUoKTsKCX0KCWZvcihpbnQgaT0oaW50KWFyci5zaXplKCktMTsgaT49MTsgaS0tKXsKCQlpZihhcnJbcm1xWzIqaV1dPj1hcnJbcm1xWzIqaSsxXV0pewoJCQlybXFbaV09cm1xWzIqaV07CgkJfWVsc2V7CgkJCXJtcVtpXT1ybXFbMippKzFdOwoJCX0KCX0KfQoKaW50IENvdW50UFMoY2hhciBzdHJbXSwgaW50IG4pewoKICAgIGludCBkcFtuXVtuXTsKICAgIG1lbXNldChkcCwgMCwgc2l6ZW9mKGRwKSk7CiAgICBib29sIFBbbl1bbl07CiAgICBtZW1zZXQoUCwgZmFsc2UgLCBzaXplb2YoUCkpOwogICAgZm9yIChpbnQgaT0gMDsgaTwgbjsgaSsrKQogICAgICAgIFBbaV1baV0gPSB0cnVlOwoKICAgIGZvciAoaW50IGk9MDsgaTxuLTE7IGkrKyl7CiAgICAgICAgaWYgKHN0cltpXSA9PSBzdHJbaSsxXSl7CiAgICAgICAgICAgIFBbaV1baSsxXSA9IHRydWU7CiAgICAgICAgICAgIGRwW2ldW2krMV0gPSAxIDsKICAgICAgICB9CiAgICB9CiAgICBmb3IgKGludCBnYXA9MiA7IGdhcDxuOyBnYXArKyl7CiAgICAgICAgZm9yIChpbnQgaT0wOyBpPG4tZ2FwOyBpKyspewogICAgICAgICAgICBpbnQgaiA9IGdhcCArIGk7CiAgICAgICAgICAgIGlmIChzdHJbaV0gPT0gc3RyW2pdICYmIFBbaSsxXVtqLTFdICkKICAgICAgICAgICAgICAgIFBbaV1bal0gPSB0cnVlOwoKICAgICAgICAgICAgaWYgKFBbaV1bal0gPT0gdHJ1ZSkKICAgICAgICAgICAgICAgIGRwW2ldW2pdID0gZHBbaV1bai0xXSArIGRwW2krMV1bal0gKyAxIC0gZHBbaSsxXVtqLTFdOwogICAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgICBkcFtpXVtqXSA9IGRwW2ldW2otMV0gKyBkcFtpKzFdW2pdIC0gZHBbaSsxXVtqLTFdOwogICAgICAgIH0KICAgIH0KIAogICAgcmV0dXJuIGRwWzBdW24tMV07Cn0KCnZvaWQgc29sdmUoKXsKCWFyci5jbGVhcigpOwoJcm1xLmNsZWFyKCk7CglpbnQgbixtOwoJc2NhbmYoIiVkICVkIiwgJm4sICZtKTsKCW1hcDxzdHJpbmcsIGludD4gaWQ7Cglmb3JuKGksIG4pewoJCWNoYXIgdG1wWzQ0XTsKCQlzY2FuZigiJXMiLCB0bXApOwoJCWludCBudT1Db3VudFBTKHRtcCwgc2l6ZW9mKHRtcCkpOwoJCWFyci5wYihudStzaXplb2YodG1wKSk7CgkJaWRbdG1wXT1pOwoJfQoJaW50IHAyPTE7Cgl3aGlsZShwMjwoaW50KWFyci5zaXplKCkpcDIqPTI7Cgl3aGlsZShwMj4oaW50KWFyci5zaXplKCkpYXJyLnBiKDApOwoJYnVpbGQoKTsKCXdoaWxlKG0tLSl7CgkJY2hhciBzMVs0NF0sIHMyWzQ0XTsKCQlzY2FuZigiJXMgJXMiLCBzMSwgczIpOwoJCWludCBsPWlkW3MxXTsKCQlpbnQgcj1pZFtzMl07CgkJcisrOwoJCWNvdXQ8PHJlcygxLCAwLCAoaW50KWFyci5zaXplKCksIGwsIHIpKzE8PGVuZGw7Cgl9Cn0KCgppbnQgbWFpbigpewoJaW50IFQ7CglzY2FuZigiJWQiLCAmVCk7Cgl3aGlsZShULS0pc29sdmUoKTsKfQo=
Main.java:1: error: illegal character: '#'
#include<bits/stdc++.h>
^
Main.java:1: error: class, interface, or enum expected
#include<bits/stdc++.h>
^
Main.java:5: error: illegal character: '#'
#define forn(i,n) for(int i=0;i<(int)(n); i++)
^
Main.java:5: error: class, interface, or enum expected
#define forn(i,n) for(int i=0;i<(int)(n); i++)
^
Main.java:5: error: class, interface, or enum expected
#define forn(i,n) for(int i=0;i<(int)(n); i++)
^
Main.java:5: error: class, interface, or enum expected
#define forn(i,n) for(int i=0;i<(int)(n); i++)
^
Main.java:6: error: illegal character: '#'
#define forsn(i,s,n) for(int i=(s);i<(int)(n); i++)
^
Main.java:6: error: class, interface, or enum expected
#define forsn(i,s,n) for(int i=(s);i<(int)(n); i++)
^
Main.java:6: error: class, interface, or enum expected
#define forsn(i,s,n) for(int i=(s);i<(int)(n); i++)
^
Main.java:7: error: illegal character: '#'
#define debug(x) cout << #x << " = " << x << endl
^
Main.java:7: error: illegal character: '#'
#define debug(x) cout << #x << " = " << x << endl
^
Main.java:8: error: illegal character: '#'
#define pb push_back
^
Main.java:9: error: illegal character: '#'
#define mp make_pair
^
Main.java:13: error: class, interface, or enum expected
vector<int> rmq, arr;
^
Main.java:15: error: class, interface, or enum expected
int NEUTRO=-1;
^
Main.java:17: error: class, interface, or enum expected
int res(int nodo, int l, int r, int i, int j){
^
Main.java:19: error: class, interface, or enum expected
if(i<=l && j>=r){
^
Main.java:21: error: class, interface, or enum expected
}
^
Main.java:23: error: class, interface, or enum expected
int i2=res(nodo*2+1, (l+r)/2, r, i, j);
^
Main.java:24: error: class, interface, or enum expected
if(i1==-1)return i2;
^
Main.java:25: error: class, interface, or enum expected
if(i2==-1)return i1;
^
Main.java:26: error: class, interface, or enum expected
if(arr[i1]>=arr[i2])return i1;
^
Main.java:27: error: class, interface, or enum expected
return i2;
^
Main.java:28: error: class, interface, or enum expected
}
^
Main.java:33: error: class, interface, or enum expected
for(int i=0; i<2*(int)(arr.size()); i++)rmq.push_back(0);
^
Main.java:33: error: class, interface, or enum expected
for(int i=0; i<2*(int)(arr.size()); i++)rmq.push_back(0);
^
Main.java:34: error: class, interface, or enum expected
for(int i=(int)(arr.size()); i<2*(int)(arr.size()); i++){
^
Main.java:34: error: class, interface, or enum expected
for(int i=(int)(arr.size()); i<2*(int)(arr.size()); i++){
^
Main.java:34: error: class, interface, or enum expected
for(int i=(int)(arr.size()); i<2*(int)(arr.size()); i++){
^
Main.java:36: error: class, interface, or enum expected
}
^
Main.java:37: error: class, interface, or enum expected
for(int i=(int)arr.size()-1; i>=1; i--){
^
Main.java:37: error: class, interface, or enum expected
for(int i=(int)arr.size()-1; i>=1; i--){
^
Main.java:40: error: class, interface, or enum expected
}else{
^
Main.java:42: error: class, interface, or enum expected
}
^
Main.java:49: error: class, interface, or enum expected
memset(dp, 0, sizeof(dp));
^
Main.java:50: error: class, interface, or enum expected
bool P[n][n];
^
Main.java:51: error: class, interface, or enum expected
memset(P, false , sizeof(P));
^
Main.java:52: error: class, interface, or enum expected
for (int i= 0; i< n; i++)
^
Main.java:52: error: class, interface, or enum expected
for (int i= 0; i< n; i++)
^
Main.java:52: error: class, interface, or enum expected
for (int i= 0; i< n; i++)
^
Main.java:55: error: class, interface, or enum expected
for (int i=0; i<n-1; i++){
^
Main.java:55: error: class, interface, or enum expected
for (int i=0; i<n-1; i++){
^
Main.java:55: error: class, interface, or enum expected
for (int i=0; i<n-1; i++){
^
Main.java:58: error: class, interface, or enum expected
dp[i][i+1] = 1 ;
^
Main.java:59: error: class, interface, or enum expected
}
^
Main.java:61: error: class, interface, or enum expected
for (int gap=2 ; gap<n; gap++){
^
Main.java:61: error: class, interface, or enum expected
for (int gap=2 ; gap<n; gap++){
^
Main.java:62: error: class, interface, or enum expected
for (int i=0; i<n-gap; i++){
^
Main.java:62: error: class, interface, or enum expected
for (int i=0; i<n-gap; i++){
^
Main.java:67: error: class, interface, or enum expected
if (str[i] == str[j] && P[i+1][j-1] )
^
Main.java:73: error: class, interface, or enum expected
if (P[i][j] == true)
^
Main.java:75: error: class, interface, or enum expected
else
^
Main.java:77: error: class, interface, or enum expected
}
^
Main.java:82: error: class, interface, or enum expected
}
^
Main.java:86: error: class, interface, or enum expected
rmq.clear();
^
Main.java:87: error: class, interface, or enum expected
int n,m;
^
Main.java:88: error: class, interface, or enum expected
scanf("%d %d", &n, &m);
^
Main.java:89: error: class, interface, or enum expected
map<string, int> id;
^
Main.java:90: error: class, interface, or enum expected
forn(i, n){
^
Main.java:92: error: class, interface, or enum expected
scanf("%s", tmp);
^
Main.java:93: error: class, interface, or enum expected
int nu=CountPS(tmp, sizeof(tmp));
^
Main.java:94: error: class, interface, or enum expected
arr.pb(nu+sizeof(tmp));
^
Main.java:95: error: class, interface, or enum expected
id[tmp]=i;
^
Main.java:96: error: class, interface, or enum expected
}
^
Main.java:98: error: class, interface, or enum expected
while(p2<(int)arr.size())p2*=2;
^
Main.java:99: error: class, interface, or enum expected
while(p2>(int)arr.size())arr.pb(0);
^
Main.java:100: error: class, interface, or enum expected
build();
^
Main.java:101: error: class, interface, or enum expected
while(m--){
^
Main.java:103: error: class, interface, or enum expected
scanf("%s %s", s1, s2);
^
Main.java:104: error: class, interface, or enum expected
int l=id[s1];
^
Main.java:105: error: class, interface, or enum expected
int r=id[s2];
^
Main.java:106: error: class, interface, or enum expected
r++;
^
Main.java:107: error: class, interface, or enum expected
cout<<res(1, 0, (int)arr.size(), l, r)+1<<endl;
^
Main.java:108: error: class, interface, or enum expected
}
^
Main.java:114: error: class, interface, or enum expected
scanf("%d", &T);
^
Main.java:115: error: class, interface, or enum expected
while(T--)solve();
^
Main.java:116: error: class, interface, or enum expected
}
^
77 errors