#include <algorithm>
#include <iostream>
#include <cstring>
#include <complex>
#include <cassert>
#include <cstdlib>
#include <cstdio>
#include <bitset>
#include <vector>
#include <string>
#include <cmath>
#include <ctime>
#include <queue>
#include <list>
#include <map>
#include <set>
#define all(x) (x).begin(), (x).end()
#define type(x) __typeof((x).begin())
#define foreach(it, x) for(type(x) it = (x).begin(); it != (x).end(); it++)
#ifdef KAZAR
#define eprintf(...) fprintf(stderr,__VA_ARGS__)
#else
#define eprintf(...) 0
#endif
using namespace std;
template<class T> inline void umax(T &a,T b){if(a<b) a = b ; }
template<class T> inline void umin(T &a,T b){if(a>b) a = b ; }
template<class T> inline T abs(T a){return a>0 ? a : -a;}
template<class T> inline T gcd(T a,T b){return __gcd(a, b);}
template<class T> inline T lcm(T a,T b){return a/gcd(a,b)*b;}
const int inf = 1e9 + 143;
const long long longinf = 1e18 + 143;
inline int read(){int x;scanf(" %d",&x);return x;}
const int N = 55;
int n, m;
unsigned a[N][N], res[N][N], t[N][N];
void mul(unsigned a[N][N],unsigned b[N][N]){
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
t[i][j] = 0;
for(int k = 0; k < n; k++)
t[i][j] += a[i][k] * b[k][j];
}
}
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
a[i][j] = t[i][j];
}
void solve(){
string alp, s;
int d = read();
cin >> alp >> s;
m = alp.size();
n = s.size();
memset(a, 0, sizeof a);
for(int len = 0; len < n; len++){
for(int add = 0; add < m; add++){
string temp = s.substr(0, len);
temp += alp[add];
for(int nlen = len + 1; nlen >= 0; nlen--){
if(nlen == 0 || temp.substr(len + 1 - nlen, nlen) == s.substr(0, nlen)){
if(nlen != n)
a[len][nlen]++;
break;
}
}
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
res[i][j] = (i == j)? 1 : 0;
}
}
while(d > 0){
if(d & 1)
mul(res, a);
mul(a, a);
d >>= 1;
}
unsigned ans = 0;
for(int i = 0; i < n; i++)
ans += res[0][i];
cout << ans << endl;
}
int main(){
#ifdef KAZAR
freopen("f.input","r",stdin);
freopen("f.output","w",stdout);
freopen("error","w",stderr);
#endif
int tc = read();
for(int t = 0; t < tc; t++){
printf("Case %d: ", t + 1);
solve();
}
return 0;
}
I2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y3N0cmluZz4KI2luY2x1ZGUgPGNvbXBsZXg+CiNpbmNsdWRlIDxjYXNzZXJ0PgojaW5jbHVkZSA8Y3N0ZGxpYj4KI2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPGJpdHNldD4KI2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPHN0cmluZz4KI2luY2x1ZGUgPGNtYXRoPgojaW5jbHVkZSA8Y3RpbWU+CiNpbmNsdWRlIDxxdWV1ZT4KI2luY2x1ZGUgPGxpc3Q+CiNpbmNsdWRlIDxtYXA+CiNpbmNsdWRlIDxzZXQ+CiAKI2RlZmluZSBhbGwoeCkgKHgpLmJlZ2luKCksICh4KS5lbmQoKQojZGVmaW5lIHR5cGUoeCkgX190eXBlb2YoKHgpLmJlZ2luKCkpCiNkZWZpbmUgZm9yZWFjaChpdCwgeCkgZm9yKHR5cGUoeCkgaXQgPSAoeCkuYmVnaW4oKTsgaXQgIT0gKHgpLmVuZCgpOyBpdCsrKQogCiNpZmRlZiBLQVpBUgogICAgI2RlZmluZSBlcHJpbnRmKC4uLikgZnByaW50ZihzdGRlcnIsX19WQV9BUkdTX18pCiNlbHNlCiAgICAjZGVmaW5lIGVwcmludGYoLi4uKSAwCiNlbmRpZgogCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiAKdGVtcGxhdGU8Y2xhc3MgVD4gaW5saW5lIHZvaWQgdW1heChUICZhLFQgYil7aWYoYTxiKSBhID0gYiA7IH0KdGVtcGxhdGU8Y2xhc3MgVD4gaW5saW5lIHZvaWQgdW1pbihUICZhLFQgYil7aWYoYT5iKSBhID0gYiA7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gIG1lbXNldChhLCAwLCBzaXplb2YgYSk7CiAgICBmb3IoaW50IGxlbiA9IDA7IGxlbiA8IG47IGxlbisrKXsKICAgICAgICBmb3IoaW50IGFkZCA9IDA7IGFkZCA8IG07IGFkZCsrKXsKICAgICAgICAgICAgc3RyaW5nIHRlbXAgPSBzLnN1YnN0cigwLCBsZW4pOwogICAgICAgICAgICB0ZW1wICs9IGFscFthZGRdOwogICAgICAgICAgICBmb3IoaW50IG5sZW4gPSBsZW4gKyAxOyBubGVuID49IDA7IG5sZW4tLSl7CiAgICAgICAgICAgICAgICBpZihubGVuID09IDAgfHwgdGVtcC5zdWJzdHIobGVuICsgMSAtIG5sZW4sIG5sZW4pID09IHMuc3Vic3RyKDAsIG5sZW4pKXsKICAgICAgICAgICAgICAgICAgICBpZihubGVuICE9IG4pCiAgICAgICAgICAgICAgICAgICAgICAgIGFbbGVuXVtubGVuXSsrOwogICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQogICAgZm9yKGludCBpID0gMDsgaSA8IG47IGkrKyl7CiAgICAgICAgZm9yKGludCBqID0gMDsgaiA8IG47IGorKyl7CiAgICAgICAgICAgIHJlc1tpXVtqXSA9IChpID09IGopPyAxIDogMDsKICAgICAgICB9CiAgICB9CiAgICB3aGlsZShkID4gMCl7CiAgICAgICAgaWYoZCAmIDEpCiAgICAgICAgICAgIG11bChyZXMsIGEpOwogICAgICAgIG11bChhLCBhKTsKICAgICAgICBkID4+PSAxOwogICAgfQogICAgdW5zaWduZWQgYW5zID0gMDsKICAgIGZvcihpbnQgaSA9IDA7IGkgPCBuOyBpKyspCiAgICAgICAgYW5zICs9IHJlc1swXVtpXTsKICAgIGNvdXQgPDwgYW5zIDw8IGVuZGw7Cn0KIAppbnQgbWFpbigpewogCiNpZmRlZiBLQVpBUgogICAgZnJlb3BlbigiZi5pbnB1dCIsInIiLHN0ZGluKTsKICAgIGZyZW9wZW4oImYub3V0cHV0IiwidyIsc3Rkb3V0KTsKICAgIGZyZW9wZW4oImVycm9yIiwidyIsc3RkZXJyKTsKI2VuZGlmCiAKICAgIGludCB0YyA9IHJlYWQoKTsKIAogICAgZm9yKGludCB0ID0gMDsgdCA8IHRjOyB0KyspewogICAgICAgIHByaW50ZigiQ2FzZSAlZDogIiwgdCArIDEpOwogICAgICAgIHNvbHZlKCk7CiAgICB9CiAKICAgIHJldHVybiAwOwp9