#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
void solve() {
int n, q;
if (!(cin >> n >> q)) return;
string s;
cin >> s;
// Precalculation variables
long long base_val = 0;
long long base_pairs = 0;
// Gap counts
long long num_cheap_H = 0; // Type 1: I...I
long long num_cheap_L = 0; // Type 4: H...H
long long num_dual = 0; // Type 2: I...H
long long total_standard_cap = 0; // Internal capacity for all gaps
long long cnt_q = 0; // Total count of '?'
// Helpers
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;
};
// calculate base value and count '?'
for (int i = 0; i < n; ++i) {
if (s[i] != '?') {
base_val += val_char(s[i]);
} else {
cnt_q++;
}
}
// Count existing pairs in the template string (ignoring '?')
for (int i = 0; i < n - 1; ++i) {
if (s[i] == 'I' && (s[i+1] == 'V' || s[i+1] == 'X')) {
base_pairs++;
}
}
// Analyze gaps
for (int i = 0; i < n; ) {
if (s[i] == '?') {
int j = i;
while (j < n && s[j] == '?') j++;
long long len = j - i;
// Determine boundaries
// Virtual 'X' at start, Virtual 'I' at end
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) { // I ... I (Type 1)
num_cheap_H++;
if (len > 0) total_standard_cap += (len - 1) / 2;
} else if (!left_is_L && right_is_H) { // H ... H (Type 4)
num_cheap_L++;
if (len > 0) total_standard_cap += (len - 1) / 2;
} else if (left_is_L && right_is_H) { // I ... H (Type 2)
num_dual++;
if (len > 0) total_standard_cap += (len - 1) / 2;
} else { // H ... I (Type 3)
total_standard_cap += len / 2;
}
i = j;
} else {
i++;
}
}
// Process queries
for (int k = 0; k < q; ++k) {
long long cX, cV, cI;
cin >> cX >> cV >> cI;
// Greedy choice of counts to minimize base sum
long long uI = min(cnt_q, cI);
long long rem = cnt_q - uI;
long long uV = min(rem, cV);
long long uX = rem - uV;
long long N_L = uI;
long long N_H = uV + uX;
long long current_pairs = base_pairs;
long long cur_cheap_H = num_cheap_H;
long long cur_cheap_L = num_cheap_L;
long long rem_dual = num_dual;
// 1. Process Duals (Type 2)
// Prefer using L because L is usually cheaper/more abundant,
// converting Type 2 -> Type 1 (Cheap H opportunity).
long long use_L_dual = min(rem_dual, N_L);
current_pairs += use_L_dual;
N_L -= use_L_dual;
rem_dual -= use_L_dual;
cur_cheap_H += use_L_dual;
// If out of L, use H for remaining duals
long long use_H_dual = min(rem_dual, N_H);
current_pairs += use_H_dual;
N_H -= use_H_dual;
cur_cheap_L += use_H_dual; // Type 2 -> Type 4 (Cheap L opportunity)
// 2. Process Cheap L slots (Type 4)
long long use_L = min(cur_cheap_L, N_L);
current_pairs += use_L;
N_L -= use_L;
// 3. Process Cheap H slots (Type 1)
long long use_H = min(cur_cheap_H, N_H);
current_pairs += use_H;
N_H -= use_H;
// 4. Process Standard slots (Internal pairs, cost 1L + 1H)
long long use_std = min({N_L, N_H, total_standard_cap});
current_pairs += use_std;
long long ans = base_val + uX * 10 + uV * 5 + uI * 1 - 2 * current_pairs;
cout << ans << "\n";
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
if (cin >> t) {
while(t--) {
solve();
}
}
return 0;
}