#include <bits/stdc++.h>
using namespace std;
#define fast_io ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define int long long
#define pb push_back
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((int)(x).size())
#define endl '\n'
#define yes cout << "yes\n"
#define no cout << "no\n"
#define rep(i,a,b) for(int i=a;i<b;++i)
#define per(i,a,b) for(int i=b-1;i>=a;--i)
#define each(x, a) for (auto& x : a)
const int INF = 1e18;
const int MOD = 1e9+7;
const int N = 2e5 + 5;
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int lcm(int a, int b) { return (a / gcd(a, b)) * b; }
int power(int a, int b, int m = MOD) {
int res = 1;
while (b > 0) {
if (b & 1) res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
int modinv(int a, int m = MOD) {
return power(a, m - 2, m);
}
void solve() {
int n, q;
cin >> n >> q;
string s;
cin >> s;
int bv = 0;
int bp = 0;
int c2 = 0;
int cl = 0;
int ch = 0;
int ts = 0;
int cq = 0;
auto ish = [](char c) { return c == 'V' || c == 'X'; };
auto isl = [](char c) { return c == 'I'; };
auto vc = [](char c) {
if (c == 'X') return 10;
if (c == 'V') return 5;
if (c == 'I') return 1;
return 0;
};
rep(i, 0, n) {
if (s[i] != '?') {
bv += vc(s[i]);
} else {
cq++;
}
}
rep(i, 0, n - 1) {
if (s[i] == 'I' && (s[i+1] == 'V' || s[i+1] == 'X')) {
bp++;
}
}
for (int i = 0; i < n; ) {
if (s[i] == '?') {
int j = i;
while (j < n && s[j] == '?') j++;
int len = j - i;
char l = (i == 0) ? 'X' : s[i-1];
char r = (j == n) ? 'I' : s[j];
bool ll = isl(l);
bool rh = ish(r);
if (ll && rh) {
if (len == 1) {
c2++;
} else {
cl++;
ch++;
ts += (len - 2) / 2;
}
} else if (ll && !rh) {
ch++;
ts += (len - 1) / 2;
} else if (!ll && rh) {
cl++;
ts += (len - 1) / 2;
} else {
ts += len / 2;
}
i = j;
} else {
i++;
}
}
while(q--) {
int cx, cv, ci;
cin >> cx >> cv >> ci;
int ui = min(cq, ci);
int rem = cq - ui;
int uv = min(rem, cv);
int ux = rem - uv;
int nl = ui;
int nh = uv + ux;
int cp = bp;
int t2 = c2;
int sl = max(0LL, nl - cl);
int tl = min(t2, sl);
cp += tl;
nl -= tl;
t2 -= tl;
int sh = max(0LL, nh - ch);
int th = min(t2, sh);
cp += th;
nh -= th;
t2 -= th;
if (t2 > 0) {
int fl = min(t2, nl);
cp += fl;
nl -= fl;
t2 -= fl;
}
if (t2 > 0) {
int fh = min(t2, nh);
cp += fh;
nh -= fh;
t2 -= fh;
}
int ul = min(cl, nl);
cp += ul;
nl -= ul;
int uh = min(ch, nh);
cp += uh;
nh -= uh;
int us = min({nl, nh, ts});
cp += us;
int an = bv + ux * 10 + uv * 5 + ui * 1 - 2 * cp;
cout << an << endl;
}
}
int32_t main() {
fast_io;
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgojZGVmaW5lIGZhc3RfaW8gaW9zOjpzeW5jX3dpdGhfc3RkaW8oMCk7IGNpbi50aWUoMCk7IGNvdXQudGllKDApCgojZGVmaW5lIGludCBsb25nIGxvbmcKI2RlZmluZSBwYiBwdXNoX2JhY2sKI2RlZmluZSBmZiBmaXJzdAojZGVmaW5lIHNzIHNlY29uZAojZGVmaW5lIGFsbCh4KSAoeCkuYmVnaW4oKSwgKHgpLmVuZCgpCiNkZWZpbmUgcmFsbCh4KSAoeCkucmJlZ2luKCksICh4KS5yZW5kKCkKI2RlZmluZSBzeih4KSAoKGludCkoeCkuc2l6ZSgpKQojZGVmaW5lIGVuZGwgJ1xuJwojZGVmaW5lIHllcyBjb3V0IDw8ICJ5ZXNcbiIKI2RlZmluZSBubyBjb3V0IDw8ICJub1xuIgoKI2RlZmluZSByZXAoaSxhLGIpIGZvcihpbnQgaT1hO2k8YjsrK2kpCiNkZWZpbmUgcGVyKGksYSxiKSBmb3IoaW50IGk9Yi0xO2k+PWE7LS1pKQojZGVmaW5lIGVhY2goeCwgYSkgZm9yIChhdXRvJiB4IDogYSkKCmNvbnN0IGludCBJTkYgPSAxZTE4Owpjb25zdCBpbnQgTU9EID0gMWU5Kzc7CmNvbnN0IGludCBOID0gMmU1ICsgNTsKCmludCBnY2QoaW50IGEsIGludCBiKSB7IHJldHVybiBiID09IDAgPyBhIDogZ2NkKGIsIGEgJSBiKTsgfQppbnQgbGNtKGludCBhLCBpbnQgYikgeyByZXR1cm4gKGEgLyBnY2QoYSwgYikpICogYjsgfQoKaW50IHBvd2VyKGludCBhLCBpbnQgYiwgaW50IG0gPSBNT0QpIHsKICAgIGludCByZXMgPSAxOwogICAgd2hpbGUgKGIgPiAwKSB7CiAgICAgICAgaWYgKGIgJiAxKSByZXMgPSByZXMgKiBhICUgbTsKICAgICAgICBhID0gYSAqIGEgJSBtOwogICAgICAgIGIgPj49IDE7CiAgICB9CiAgICByZXR1cm4gcmVzOwp9CgppbnQgbW9kaW52KGludCBhLCBpbnQgbSA9IE1PRCkgewogICAgcmV0dXJuIHBvd2VyKGEsIG0gLSAyLCBtKTsKfQoKdm9pZCBzb2x2ZSgpIHsKICAgIGludCBuLCBxOwogICAgY2luID4+IG4gPj4gcTsKICAgIHN0cmluZyBzOwogICAgY2luID4+IHM7CgogICAgaW50IGJ2ID0gMDsKICAgIGludCBicCA9IDA7CiAgICAKICAgIGludCBjMiA9IDA7IAogICAgaW50IGNsID0gMDsgCiAgICBpbnQgY2ggPSAwOyAKICAgIGludCB0cyA9IDA7IAogICAgCiAgICBpbnQgY3EgPSAwOwoKICAgIGF1dG8gaXNoID0gW10oY2hhciBjKSB7IHJldHVybiBjID09ICdWJyB8fCBjID09ICdYJzsgfTsKICAgIGF1dG8gaXNsID0gW10oY2hhciBjKSB7IHJldHVybiBjID09ICdJJzsgfTsKICAgIGF1dG8gdmMgPSBbXShjaGFyIGMpIHsKICAgICAgICBpZiAoYyA9PSAnWCcpIHJldHVybiAxMDsKICAgICAgICBpZiAoYyA9PSAnVicpIHJldHVybiA1OwogICAgICAgIGlmIChjID09ICdJJykgcmV0dXJuIDE7CiAgICAgICAgcmV0dXJuIDA7CiAgICB9OwoKICAgIHJlcChpLCAwLCBuKSB7CiAgICAgICAgaWYgKHNbaV0gIT0gJz8nKSB7CiAgICAgICAgICAgIGJ2ICs9IHZjKHNbaV0pOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIGNxKys7CiAgICAgICAgfQogICAgfQogICAgCiAgICByZXAoaSwgMCwgbiAtIDEpIHsKICAgICAgICBpZiAoc1tpXSA9PSAnSScgJiYgKHNbaSsxXSA9PSAnVicgfHwgc1tpKzFdID09ICdYJykpIHsKICAgICAgICAgICAgYnArKzsKICAgICAgICB9CiAgICB9CgogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyApIHsKICAgICAgICBpZiAoc1tpXSA9PSAnPycpIHsKICAgICAgICAgICAgaW50IGogPSBpOwogICAgICAgICAgICB3aGlsZSAoaiA8IG4gJiYgc1tqXSA9PSAnPycpIGorKzsKICAgICAgICAgICAgaW50IGxlbiA9IGogLSBpOwogICAgICAgICAgICAKICAgICAgICAgICAgY2hhciBsID0gKGkgPT0gMCkgPyAnWCcgOiBzW2ktMV07CiAgICAgICAgICAgIGNoYXIgciA9IChqID09IG4pID8gJ0knIDogc1tqXTsKICAgICAgICAgICAgCiAgICAgICAgICAgIGJvb2wgbGwgPSBpc2wobCk7IAogICAgICAgICAgICBib29sIHJoID0gaXNoKHIpOyAKICAgICAgICAgICAgCiAgICAgICAgICAgIGlmIChsbCAmJiByaCkgewogICAgICAgICAgICAgICAgaWYgKGxlbiA9PSAxKSB7CiAgICAgICAgICAgICAgICAgICAgYzIrKzsKICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgY2wrKzsKICAgICAgICAgICAgICAgICAgICBjaCsrOwogICAgICAgICAgICAgICAgICAgIHRzICs9IChsZW4gLSAyKSAvIDI7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0gZWxzZSBpZiAobGwgJiYgIXJoKSB7CiAgICAgICAgICAgICAgICBjaCsrOwogICAgICAgICAgICAgICAgdHMgKz0gKGxlbiAtIDEpIC8gMjsKICAgICAgICAgICAgfSBlbHNlIGlmICghbGwgJiYgcmgpIHsKICAgICAgICAgICAgICAgIGNsKys7CiAgICAgICAgICAgICAgICB0cyArPSAobGVuIC0gMSkgLyAyOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgdHMgKz0gbGVuIC8gMjsKICAgICAgICAgICAgfQogICAgICAgICAgICAKICAgICAgICAgICAgaSA9IGo7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgaSsrOwogICAgICAgIH0KICAgIH0KCiAgICB3aGlsZShxLS0pIHsKICAgICAgICBpbnQgY3gsIGN2LCBjaTsKICAgICAgICBjaW4gPj4gY3ggPj4gY3YgPj4gY2k7CgogICAgICAgIGludCB1aSA9IG1pbihjcSwgY2kpOwogICAgICAgIGludCByZW0gPSBjcSAtIHVpOwogICAgICAgIGludCB1diA9IG1pbihyZW0sIGN2KTsKICAgICAgICBpbnQgdXggPSByZW0gLSB1djsKCiAgICAgICAgaW50IG5sID0gdWk7CiAgICAgICAgaW50IG5oID0gdXYgKyB1eDsKICAgICAgICAKICAgICAgICBpbnQgY3AgPSBicDsKICAgICAgICAKICAgICAgICBpbnQgdDIgPSBjMjsKICAgICAgICAKICAgICAgICBpbnQgc2wgPSBtYXgoMExMLCBubCAtIGNsKTsKICAgICAgICBpbnQgdGwgPSBtaW4odDIsIHNsKTsKICAgICAgICBjcCArPSB0bDsKICAgICAgICBubCAtPSB0bDsKICAgICAgICB0MiAtPSB0bDsKICAgICAgICAKICAgICAgICBpbnQgc2ggPSBtYXgoMExMLCBuaCAtIGNoKTsKICAgICAgICBpbnQgdGggPSBtaW4odDIsIHNoKTsKICAgICAgICBjcCArPSB0aDsKICAgICAgICBuaCAtPSB0aDsKICAgICAgICB0MiAtPSB0aDsKICAgICAgICAKICAgICAgICBpZiAodDIgPiAwKSB7CiAgICAgICAgICAgIGludCBmbCA9IG1pbih0MiwgbmwpOwogICAgICAgICAgICBjcCArPSBmbDsKICAgICAgICAgICAgbmwgLT0gZmw7CiAgICAgICAgICAgIHQyIC09IGZsOwogICAgICAgIH0KICAgICAgICBpZiAodDIgPiAwKSB7CiAgICAgICAgICAgIGludCBmaCA9IG1pbih0MiwgbmgpOwogICAgICAgICAgICBjcCArPSBmaDsKICAgICAgICAgICAgbmggLT0gZmg7CiAgICAgICAgICAgIHQyIC09IGZoOwogICAgICAgIH0KCiAgICAgICAgaW50IHVsID0gbWluKGNsLCBubCk7CiAgICAgICAgY3AgKz0gdWw7CiAgICAgICAgbmwgLT0gdWw7CgogICAgICAgIGludCB1aCA9IG1pbihjaCwgbmgpOwogICAgICAgIGNwICs9IHVoOwogICAgICAgIG5oIC09IHVoOwogICAgICAgIAogICAgICAgIGludCB1cyA9IG1pbih7bmwsIG5oLCB0c30pOwogICAgICAgIGNwICs9IHVzOwogICAgICAgIAogICAgICAgIGludCBhbiA9IGJ2ICsgdXggKiAxMCArIHV2ICogNSArIHVpICogMSAtIDIgKiBjcDsKICAgICAgICBjb3V0IDw8IGFuIDw8IGVuZGw7CiAgICB9Cn0KCmludDMyX3QgbWFpbigpIHsKICAgIGZhc3RfaW87CgogICAgaW50IHQ7CiAgICBjaW4gPj4gdDsKICAgIHdoaWxlICh0LS0pIHsKICAgICAgICBzb2x2ZSgpOwogICAgfQoKICAgIHJldHVybiAwOwp9