#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const int maxn = 1e5 + 5;
int T,n,m,val,ans;
char c;
int a[3*maxn];
int main() {
scanf("%d", &T);
while (T--) {
scanf("%d %d", &n, &m);
//防止最后一张牌是白板
a[n] = 3 * m + 1;
int index = -1;
for (int i = 0; i < n; i++) {
cin >> c;
if (c == 'W') {
index = i;
a[i] = 0;
} else {
cin >> val;
}
if (c == 'C') a[i] = val;
else if (c == 'B') a[i] = m + val;
else if (c == 'D') a[i] = 2 * m + val;
}
if (n == 1) { //只有一张牌
ans = 3 * m;
} else {
if (index == -1) {
if (a[0] > a[1]) //有多张牌,没有白板,且第一张比第二张牌大,幸运牌为第一张
ans = 1;
else //有多张牌,没有白板,且按顺序排列,幸运牌为总牌数减去n加上第一张牌
ans = 3 * m - n + 1;
} else {
if (index == 0) {
ans = a[1] - 1; //有多张牌,有白板,白板在第一张,幸运牌数为第二张牌-1
} else {
if (a[0] > a[1] && index != 1) { //有多张牌,有白板,白板不在第二张牌,且第一张牌比第二张牌大
ans = 1;
} else {
ans = a[index+1] - a[index-1] - 1; //有多张牌,有白板,第一张牌比第二张小,白板后一张减去白板前一张-1
if (index == 1) ans++; //有多张牌,有白板,白板在第二张
}
}
}
}
cout << ans << endl;
}
return 0;
}