#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<long long, long long>;
const long long INF = 2e18;
template <class A, class B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}
template <class A>
ostream &operator<<(ostream &os, const vector<A> &v) {
  os << "{";
  for (long long i = 0; i < (long long)v.size(); i++)
    if (i == 0)
      os << v[i];
    else
      os << ", " << v[i];
  os << "}";
  return os;
}
void dprint(string s) { cout << "\n"; }
template <class T, class... U>
void dprint(string s, T t, U... u) {
  long long w = s.find(',');
  cout << "[" << s.substr(0, w) << ": " << t << "] ";
  dprint(s.substr(w + 2, (long long)s.size() - w - 1), u...);
}
const long long N = 1e6 + 5;
void solve() {
  long long n, k;
  cin >> n >> k;
  string s;
  cin >> s;
  long long sc = 0;
  vector<long long> v;
  if (s[0] == 'W') v.push_back(0);
  for (long long i = 0; i < n;) {
    char c = s[i];
    long long cnt = 0;
    while (s[i] == c and i < n) i++, cnt++;
    if (c == 'W')
      sc += 2 * (cnt - 1) + 1;
    else
      v.push_back(cnt);
  }
  if (s[n - 1] == 'W') v.push_back(0);
  if (k == 0) {
    cout << sc << "\n";
    return;
  }
  if ((long long)(v).size() == 1) {
    if (s[0] == s[n - 1]) {
      long long take = min(v[0], k);
      sc += 2 * (take - 1) + 1;
      cout << sc << "\n";
      return;
    }
  }
  long long st = 0, end = 0;
  st = v[0] + v[(long long)(v).size() - 1];
  vector<long long> d;
  for (long long i = 1; i < (long long)(v).size() - 1; i++) d.push_back(v[i]);
  v = d;
  sort((v).begin(), (v).end());
  for (long long x : v) {
    long long take = min(x, k);
    k -= take, x -= take;
    if (x == 0)
      sc += 2 * (take) + 1;
    else {
      sc += 2 * take;
      break;
    }
  }
  long long take = min(st, k);
  st -= take, k -= take;
  sc += 2 * take;
  cout << sc << "\n";
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long T;
  cin >> T;
  for (long long tc = 1; tc <= T; tc++) {
    solve();
  }
  return 0;
}
