#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> PII;
#define pb emplace_back
//#define int ll
#define all(a) a.begin(),a.end()
#define x first
#define y second
#define ps push_back
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
void solve();
const int N = 1e6 + 10;
int days[]={0,31,29,31,30,31,30,31,31,30,31,30,31};
signed main() {
IOS;
ll t;
cin >> t;
while(t--)
solve();
return 0;
}
ll ctoll(char a,char b)
{
return (a-'0')*10+b-'0';
}
void solve() {
ll n,m;
string s,t;
cin >> n >> m >> s;
vector<string> name;
for(int i = 1; i <= n; ++i)
{
cin >> t; name.pb(t);
}
map<char,vector<ll>> pos_char,pos_int;
vector<ll> dp(m+1,0);
for(int i = 0; i < m; ++ i)
{
if(s[i] >= 'a' && s[i] <= 'z') pos_char[s[i]].ps(i);
else pos_int[s[i]].ps(i);
}
for(char d2 = '0'; d2 <= '9'; ++ d2)
{
if(pos_int[d2].empty()) continue;//没有这个数字
ll pos_d2 = pos_int[d2].back();//选取最后一个
for(char d1 = '0'; d1 <= '3'; ++ d1)
{
ll day = ctoll(d1,d2);
if(day<1 || day>31) continue;//不可能的日期
if(pos_int[d1].empty()) continue;//没有
auto to_pos = lower_bound(all(pos_int[d1]),pos_d2);//寻找小于的最后一个
if(to_pos == pos_int[d1].begin()) continue;
ll pos_d1 = *(--to_pos);
// cout << "day:" << day << endl;
for(char m2 = '0'; m2 <= '9'; ++ m2)
{
if(pos_int[m2].empty()) continue;//没有
to_pos = lower_bound(all(pos_int[m2]),pos_d1);
if(to_pos == pos_int[m2].begin()) continue;//没有前面的满足条件的数字
ll pos_m2 = *(--to_pos);
for(char m1 = '0'; m1 <= '1'; ++ m1)
{
ll month = ctoll(m1,m2);
if(month<1 || month>12) continue;//不满足的月份
if(days[month] < day) continue;//月与日不匹配
if(pos_int[m1].empty()) continue;//没有
to_pos = lower_bound(all(pos_int[m1]),pos_m2);
if(to_pos == pos_int[m1].begin()) continue;
ll pos_m1 = *(--to_pos);
dp[pos_m1]++;
// cout << "sb:" << month << day << endl;
}
}
}
}
ll ans = 0;
for(int i = m-1; i >= 0; -- i) dp[i] += dp[i+1];
for(int i = 0; i < name.size(); ++ i)
{
ll ix = -1;//第一个匹配的下标
ll f = 1;
for(int j = 0; j < name[i].size(); ++j)
{
auto c = name[i][j];
auto it = upper_bound(all(pos_char[c]),ix);
if(it == pos_char[c].end())
{f = 0;break;}//匹配不了了
ix = *it;
}
if(f) ans += dp[ix];
}
cout << ans << endl;
}