#include <bits/stdc++.h>
using namespace std;
const int MAX_game = 100000;
int a[100000 + 10];
int mid[100000 + 10];
char s[100000 + 10];
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    memset(a, 0, sizeof(a));
    memset(mid, 0, sizeof(mid));
    int n, k;
    scanf("%d%d", &n, &k);
    scanf("%s", s);
    int p = 1, tot = 1;
    for (int i = 0; i < n; i++) {
      if (i == n - 1) {
        a[p] = tot;
        continue;
      }
      if (s[i] == s[i + 1])
        tot++;
      else {
        a[p] = tot;
        tot = 1;
        p++;
      }
    }
    if (p == 1 && s[0] == 'L') {
      int cnt = min(k, n);
      cout << max(cnt * 2 - 1, 0) << endl;
      continue;
    }
    int ans = 0, t = 0, sum = 0;
    if (s[0] == 'W') {
      for (int i = 1; i <= p; i++) {
        if (i % 2)
          ans += a[i] * 2 - 1;
        else {
          mid[++t] = a[i];
          sum += a[i];
        }
      }
    } else {
      for (int i = 1; i <= p; i++) {
        if (i % 2 == 0)
          ans += a[i] * 2 - 1;
        else
          sum += a[i];
        if (i >= 3 && i % 2) mid[++t] = a[i];
      }
    }
    if (s[n - 1] == 'L' && p != 1) t--;
    if (k >= sum) {
      cout << 2 * n - 1 << endl;
      continue;
    }
    sort(mid + 1, mid + (t + 1));
    for (int i = 1; i <= t && k >= mid[i]; i++) {
      ans += 2 * mid[i] + 1;
      k -= mid[i];
      sum -= mid[i];
    }
    if (k > 0 && sum > k) ans += k * 2;
    cout << ans << endl;
  }
  return 0;
}
