function maxABSubsequence(T, testCases) {
const results = [];
for (let t = 0; t < T; t++) {
const [N, K] = testCases[t][0];
let S = testCases[t][1].split('');
let countA = 0;
let abCount = 0;
let usedOperations = 0;
for (let i = 0; i < N; i++) {
if (S[i] === 'A') {
countA++; // Increment "A" count
} else if (S[i] === 'B') {
abCount += countA; // "AB" subsequence count increment
} else if (S[i] === '?' && usedOperations < K) {
// Decide based on maximizing AB sequences
if (countA > 0) {
S[i] = 'B';
abCount += countA;
} else {
S[i] = 'A';
countA++;
}
usedOperations++;
}
}
results.push(abCount);
}
return results.join("\n");
}
// Example usage:
const T = 2;
const testCases = [
[[6, 2], "AABC?B"],
[[3, 3], "?B?"]
];
console.log(maxABSubsequence(T, testCases));