#include <bits/stdc++.h>
using namespace std;
// Speed
#define fast_io ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
// Typedefs
#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"
// Loops
#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)
// Consts
const int INF = 1e18;
const int MOD = 1e9+7;
const int N = 2e5 + 5;
// Math
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);
}
// Logic
void solve() {
int n, q;
cin >> n >> q;
string s;
cin >> s;
int base_val = 0;
int base_pairs = 0;
int cnt_2b = 0; // I ? H
int cnt_dedicated_L = 0; // ... H
int cnt_dedicated_H = 0; // I ...
int total_standard_cap = 0;
int cnt_q = 0;
auto is_H = [](char c) { return c == 'V' || c == 'X'; };
auto is_L = [](char c) { return c == 'I'; };
auto val_char = [](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] != '?') {
base_val += val_char(s[i]);
} else {
cnt_q++;
}
}
rep(i, 0, n - 1) {
if (s[i] == 'I' && (s[i+1] == 'V' || s[i+1] == 'X')) {
base_pairs++;
}
}
for (int i = 0; i < n; ) {
if (s[i] == '?') {
int j = i;
while (j < n && s[j] == '?') j++;
int len = j - i;
char left = (i == 0) ? 'X' : s[i-1];
char right = (j == n) ? 'I' : s[j];
bool left_is_L = is_L(left);
bool right_is_H = is_H(right);
if (left_is_L && right_is_H) {
if (len == 1) {
cnt_2b++;
} else {
cnt_dedicated_L++;
cnt_dedicated_H++;
total_standard_cap += (len - 2) / 2;
}
} else if (left_is_L && !right_is_H) {
cnt_dedicated_H++;
total_standard_cap += (len - 1) / 2;
} else if (!left_is_L && right_is_H) {
cnt_dedicated_L++;
total_standard_cap += (len - 1) / 2;
} else {
total_standard_cap += len / 2;
}
i = j;
} else {
i++;
}
}
while(q--) {
int cX, cV, cI;
cin >> cX >> cV >> cI;
int uI = min(cnt_q, cI);
int rem = cnt_q - uI;
int uV = min(rem, cV);
int uX = rem - uV;
int N_L = uI;
int N_H = uV + uX;
int current_pairs = base_pairs;
int temp_2b = cnt_2b;
int surplus_L = max(0LL, N_L - cnt_dedicated_L);
int take_L = min(temp_2b, surplus_L);
current_pairs += take_L;
N_L -= take_L;
temp_2b -= take_L;
int surplus_H = max(0LL, N_H - cnt_dedicated_H);
int take_H = min(temp_2b, surplus_H);
current_pairs += take_H;
N_H -= take_H;
temp_2b -= take_H;
if (temp_2b > 0) {
int fill_L = min(temp_2b, N_L);
current_pairs += fill_L;
N_L -= fill_L;
temp_2b -= fill_L;
}
if (temp_2b > 0) {
int fill_H = min(temp_2b, N_H);
current_pairs += fill_H;
N_H -= fill_H;
temp_2b -= fill_H;
}
int use_L_ded = min(cnt_dedicated_L, N_L);
current_pairs += use_L_ded;
N_L -= use_L_ded;
int use_H_ded = min(cnt_dedicated_H, N_H);
current_pairs += use_H_ded;
N_H -= use_H_ded;
int use_std = min({N_L, N_H, total_standard_cap});
current_pairs += use_std;
int ans = base_val + uX * 10 + uV * 5 + uI * 1 - 2 * current_pairs;
cout << ans << endl;
}
}
// Main
int32_t main() {
fast_io;
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgovLyBTcGVlZAojZGVmaW5lIGZhc3RfaW8gaW9zOjpzeW5jX3dpdGhfc3RkaW8oMCk7IGNpbi50aWUoMCk7IGNvdXQudG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JuIHBvd2VyKGEsIG0gLSAyLCBtKTsKfQoKLy8gTG9naWMKdm9pZCBzb2x2ZSgpIHsKICAgIGludCBuLCBxOwogICAgY2luID4+IG4gPj4gcTsKICAgIHN0cmluZyBzOwogICAgY2luID4+IHM7CgogICAgaW50IGJhc2VfdmFsID0gMDsKICAgIGludCBiYXNlX3BhaXJzID0gMDsKICAgIAogICAgaW50IGNudF8yYiA9IDA7ICAgICAgICAgICAvLyBJID8gSAogICAgaW50IGNudF9kZWRpY2F0ZWRfTCA9IDA7ICAvLyAuLi4gSAogICAgaW50IGNudF9kZWRpY2F0ZWRfSCA9IDA7ICAvLyBJIC4uLgogICAgaW50IHRvdGFsX3N0YW5kYXJkX2NhcCA9IDA7CiAgICAKICAgIGludCBjbnRfcSA9IDA7CgogICAgYXV0byBpc19IID0gW10oY2hhciBjKSB7IHJldHVybiBjID09ICdWJyB8fCBjID09ICdYJzsgfTsKICAgIGF1dG8gaXNfTCA9IFtdKGNoYXIgYykgeyByZXR1cm4gYyA9PSAnSSc7IH07CiAgICBhdXRvIHZhbF9jaGFyID0gW10oY2hhciBjKSB7CiAgICAgICAgaWYgKGMgPT0gJ1gnKSByZXR1cm4gMTA7CiAgICAgICAgaWYgKGMgPT0gJ1YnKSByZXR1cm4gNTsKICAgICAgICBpZiAoYyA9PSAnSScpIHJldHVybiAxOwogICAgICAgIHJldHVybiAwOwogICAgfTsKCiAgICByZXAoaSwgMCwgbikgewogICAgICAgIGlmIChzW2ldICE9ICc/JykgewogICAgICAgICAgICBiYXNlX3ZhbCArPSB2YWxfY2hhcihzW2ldKTsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBjbnRfcSsrOwogICAgICAgIH0KICAgIH0KICAgIAogICAgcmVwKGksIDAsIG4gLSAxKSB7CiAgICAgICAgaWYgKHNbaV0gPT0gJ0knICYmIChzW2krMV0gPT0gJ1YnIHx8IHNbaSsxXSA9PSAnWCcpKSB7CiAgICAgICAgICAgIGJhc2VfcGFpcnMrKzsKICAgICAgICB9CiAgICB9CgogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyApIHsKICAgICAgICBpZiAoc1tpXSA9PSAnPycpIHsKICAgICAgICAgICAgaW50IGogPSBpOwogICAgICAgICAgICB3aGlsZSAoaiA8IG4gJiYgc1tqXSA9PSAnPycpIGorKzsKICAgICAgICAgICAgaW50IGxlbiA9IGogLSBpOwogICAgICAgICAgICAKICAgICAgICAgICAgY2hhciBsZWZ0ID0gKGkgPT0gMCkgPyAnWCcgOiBzW2ktMV07CiAgICAgICAgICAgIGNoYXIgcmlnaHQgPSAoaiA9PSBuKSA/ICdJJyA6IHNbal07CiAgICAgICAgICAgIAogICAgICAgICAgICBib29sIGxlZnRfaXNfTCA9IGlzX0wobGVmdCk7IAogICAgICAgICAgICBib29sIHJpZ2h0X2lzX0ggPSBpc19IKHJpZ2h0KTsgCiAgICAgICAgICAgIAogICAgICAgICAgICBpZiAobGVmdF9pc19MICYmIHJpZ2h0X2lzX0gpIHsKICAgICAgICAgICAgICAgIGlmIChsZW4gPT0gMSkgewogICAgICAgICAgICAgICAgICAgIGNudF8yYisrOwogICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICBjbnRfZGVkaWNhdGVkX0wrKzsKICAgICAgICAgICAgICAgICAgICBjbnRfZGVkaWNhdGVkX0grKzsKICAgICAgICAgICAgICAgICAgICB0b3RhbF9zdGFuZGFyZF9jYXAgKz0gKGxlbiAtIDIpIC8gMjsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfSBlbHNlIGlmIChsZWZ0X2lzX0wgJiYgIXJpZ2h0X2lzX0gpIHsKICAgICAgICAgICAgICAgIGNudF9kZWRpY2F0ZWRfSCsrOwogICAgICAgICAgICAgICAgdG90YWxfc3RhbmRhcmRfY2FwICs9IChsZW4gLSAxKSAvIDI7CiAgICAgICAgICAgIH0gZWxzZSBpZiAoIWxlZnRfaXNfTCAmJiByaWdodF9pc19IKSB7CiAgICAgICAgICAgICAgICBjbnRfZGVkaWNhdGVkX0wrKzsKICAgICAgICAgICAgICAgIHRvdGFsX3N0YW5kYXJkX2NhcCArPSAobGVuIC0gMSkgLyAyOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgdG90YWxfc3RhbmRhcmRfY2FwICs9IGxlbiAvIDI7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgCiAgICAgICAgICAgIGkgPSBqOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIGkrKzsKICAgICAgICB9CiAgICB9CgogICAgd2hpbGUocS0tKSB7CiAgICAgICAgaW50IGNYLCBjViwgY0k7CiAgICAgICAgY2luID4+IGNYID4+IGNWID4+IGNJOwoKICAgICAgICBpbnQgdUkgPSBtaW4oY250X3EsIGNJKTsKICAgICAgICBpbnQgcmVtID0gY250X3EgLSB1STsKICAgICAgICBpbnQgdVYgPSBtaW4ocmVtLCBjVik7CiAgICAgICAgaW50IHVYID0gcmVtIC0gdVY7CgogICAgICAgIGludCBOX0wgPSB1STsKICAgICAgICBpbnQgTl9IID0gdVYgKyB1WDsKICAgICAgICAKICAgICAgICBpbnQgY3VycmVudF9wYWlycyA9IGJhc2VfcGFpcnM7CiAgICAgICAgCiAgICAgICAgaW50IHRlbXBfMmIgPSBjbnRfMmI7CiAgICAgICAgCiAgICAgICAgaW50IHN1cnBsdXNfTCA9IG1heCgwTEwsIE5fTCAtIGNudF9kZWRpY2F0ZWRfTCk7CiAgICAgICAgaW50IHRha2VfTCA9IG1pbih0ZW1wXzJiLCBzdXJwbHVzX0wpOwogICAgICAgIGN1cnJlbnRfcGFpcnMgKz0gdGFrZV9MOwogICAgICAgIE5fTCAtPSB0YWtlX0w7CiAgICAgICAgdGVtcF8yYiAtPSB0YWtlX0w7CiAgICAgICAgCiAgICAgICAgaW50IHN1cnBsdXNfSCA9IG1heCgwTEwsIE5fSCAtIGNudF9kZWRpY2F0ZWRfSCk7CiAgICAgICAgaW50IHRha2VfSCA9IG1pbih0ZW1wXzJiLCBzdXJwbHVzX0gpOwogICAgICAgIGN1cnJlbnRfcGFpcnMgKz0gdGFrZV9IOwogICAgICAgIE5fSCAtPSB0YWtlX0g7CiAgICAgICAgdGVtcF8yYiAtPSB0YWtlX0g7CiAgICAgICAgCiAgICAgICAgaWYgKHRlbXBfMmIgPiAwKSB7CiAgICAgICAgICAgIGludCBmaWxsX0wgPSBtaW4odGVtcF8yYiwgTl9MKTsKICAgICAgICAgICAgY3VycmVudF9wYWlycyArPSBmaWxsX0w7CiAgICAgICAgICAgIE5fTCAtPSBmaWxsX0w7CiAgICAgICAgICAgIHRlbXBfMmIgLT0gZmlsbF9MOwogICAgICAgIH0KICAgICAgICBpZiAodGVtcF8yYiA+IDApIHsKICAgICAgICAgICAgaW50IGZpbGxfSCA9IG1pbih0ZW1wXzJiLCBOX0gpOwogICAgICAgICAgICBjdXJyZW50X3BhaXJzICs9IGZpbGxfSDsKICAgICAgICAgICAgTl9IIC09IGZpbGxfSDsKICAgICAgICAgICAgdGVtcF8yYiAtPSBmaWxsX0g7CiAgICAgICAgfQoKICAgICAgICBpbnQgdXNlX0xfZGVkID0gbWluKGNudF9kZWRpY2F0ZWRfTCwgTl9MKTsKICAgICAgICBjdXJyZW50X3BhaXJzICs9IHVzZV9MX2RlZDsKICAgICAgICBOX0wgLT0gdXNlX0xfZGVkOwoKICAgICAgICBpbnQgdXNlX0hfZGVkID0gbWluKGNudF9kZWRpY2F0ZWRfSCwgTl9IKTsKICAgICAgICBjdXJyZW50X3BhaXJzICs9IHVzZV9IX2RlZDsKICAgICAgICBOX0ggLT0gdXNlX0hfZGVkOwogICAgICAgIAogICAgICAgIGludCB1c2Vfc3RkID0gbWluKHtOX0wsIE5fSCwgdG90YWxfc3RhbmRhcmRfY2FwfSk7CiAgICAgICAgY3VycmVudF9wYWlycyArPSB1c2Vfc3RkOwogICAgICAgIAogICAgICAgIGludCBhbnMgPSBiYXNlX3ZhbCArIHVYICogMTAgKyB1ViAqIDUgKyB1SSAqIDEgLSAyICogY3VycmVudF9wYWlyczsKICAgICAgICBjb3V0IDw8IGFucyA8PCBlbmRsOwogICAgfQp9CgovLyBNYWluCmludDMyX3QgbWFpbigpIHsKICAgIGZhc3RfaW87CgogICAgaW50IHQ7CiAgICBjaW4gPj4gdDsKICAgIHdoaWxlICh0LS0pIHsKICAgICAgICBzb2x2ZSgpOwogICAgfQoKICAgIHJldHVybiAwOwp9