#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000 * 1000 + 10;
const long long INF = 1LL * 1000 * 1000 * 1000 * 1000 * 1000 * 1000 + 10;
long long arr[MAXN];
vector<long long> l;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int tt;
  cin >> tt;
  while (tt--) {
    long long n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    l.clear();
    bool mark = false;
    long long first = 0, last = 0, pos = 0, ans = 0;
    for (int i = 0; i < n; i++) {
      if (s[i] == 'W') {
        ans++;
        if (s[i - 1] == 'W') ans++;
        if (mark) {
          if (pos != 0) {
            l.push_back(pos);
            pos = 0;
          }
        } else {
          first = pos;
          pos = 0;
          mark = true;
        }
      } else
        pos++;
    }
    last = pos;
    if (!mark) {
      cout << max(0LL, min(n, k) * 2 - 1) << endl;
      continue;
    }
    sort(l.begin(), l.end());
    int ind = 0;
    while (k != 0 and ind != l.size()) {
      if (l[ind] > k) {
        ans += k * 2;
        k = 0;
      } else {
        ans += l[ind] * 2 + 1;
        k -= l[ind];
      }
      ind++;
    }
    if (k > 0) {
      ans += min(k, last) * 2;
      k -= last;
      if (k > 0) ans += min(k, first) * 2;
    }
    cout << ans << endl;
  }
  return 0;
}
