#pragma comment(linker, "/stack:16777216") #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define FOR(i,a,b) for(int i = (a); i < (b); ++i) #define RFOR(i,b,a) for(int i = (b) - 1; i >= (a); --i) #define REP(i,N) FOR(i, 0, N) #define RREP(i,N) RFOR(i, N, 0) #define FILL(A,value) memset(A,value,sizeof(A)) #define ALL(V) V.begin(), V.end() #define SZ(V) (int)V.size() #define PB push_back #define MP make_pair #define Pi 3.14159265358979 typedef long long Int; //typedef unsigned long long UINT; typedef vector VI; typedef pair PII; const int INF = 1000000000; const int MAX = 64; const int MAX2 = 1000000; const int BASE = 1000000000; const int CNT = 20; #define MOD 1000000007 struct Test { int n; string s; string t; Test(){} int R[MAX]; int solve() { int m = SZ(s); int maskS = 0; FOR (i,0,m) if (s[i] == 'B') maskS += (1 << (m-i-1)); int maskT = 0; FOR (i,0,m) if (t[i] == 'B') maskT += (1 << (m-i-1)); // For each length len, let R[len] be the number of strings of A and B of length len that are good (simply brute force) R[0] = 1; FOR (len,1,n+1) { R[len] = 0; if (len < m) { R[len] = (1 << len); continue; } FOR (mask,0,(1 << len)) { bool ok = 1; for (int j = 0; j+m-1 < len; ++j) { int t = (mask >> j) % (1 << m); if (t >= maskS && t <= maskT) { ok = 0; break; } } R[len] += ok; } } Int res = 0; // Now lets iterate where non-AB characters are. All position between them can be filled using the array R. FOR (mask,0,(1 << n)) { Int p = 1; int last = -1, cnt = 0; FOR (i,0,n) { if ((mask & (1 << i)) != 0) { p = p * R[i-last-1]; last = i; ++ cnt; } } p = p * R[n-last-1]; FOR (i,0,cnt) p = (p * 24) % MOD; res += p; res %= MOD; } return res; } void writeTest() { cout << n << ' ' << SZ(s) << endl; cout << s << endl; cout << t << endl; } void writeRes() { cout << solve() << endl; } void readTest() { int m; cin >> n >> m; cin >> s >> t; } }; int main() { int cnt; cin >> cnt; FOR (i,0,cnt) { Test t; t.readTest(); t.writeRes(); } return 0; }