#include <iostream>
#include<vector>
#include<algorithm>
#include<string>

using namespace std;

using ll = long long;
using ull = unsigned long long;

void solve(istream &cin = std::cin, ostream &cout = std::cout)
{
	int n, k;
	cin >> n >> k;
	string s;
	cin >> s;
	vector<int> poses;
	int cur = 0, mx = 2 * n - 1,  l=0;
	for (int i = 0; i < n; i++)
	{
		if (s[i] == 'W')
		{
			l=1;
			poses.push_back(i);
			cur++;
			if (i > 0 && s[i - 1] == 'W')
				cur++;
		}
	}
	if(l==1)
	{
		vector<int> holes;
		for (int i = 1; i < poses.size(); i++)
		{
			int cr = poses[i] - poses[i - 1] - 1;
			if (cr > 0)
				holes.push_back(cr);
		}
		sort(holes.begin(), holes.end());
		cur += 2 * k;
		int sum = 0;
		for (int i = 0; i < holes.size(); i++)
		{
			sum += holes[i];
			if (sum <= k)
				cur++;
		}
		cout << min(cur, mx) << endl;
	}
	else{
		k=min(k,n);
		cout<<max(2*k-1,0)<<endl;
	}
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout << fixed;

#ifdef LOCAL
	auto st = clock();

	ifstream fin("../input.txt");

	do
	{
		solve(fin);

		cout << "===" << endl;

		string str;
		while (getline(fin, str) && str != string(max(1, (int) str.size()), '='));
	} while (fin);

	cout << setprecision(6) << "clock: " << double(clock() - st) / CLOCKS_PER_SEC << endl;
#else
	int t;
	cin >> t;
	for (int i = 0; i < t; i++)
		solve();
#endif

	return 0;
}


#pragma comment(linker, "/STACK:9759095000")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,abm,mmx,tune=native")
#include<vector>
#include<iostream>
#include<stack>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<string>
#include<tuple>
#include<bitset>
#include<queue>
#include <unordered_map>
#include<random>
#include<ctime>
#define double long double
//#define int long long
using namespace std;
long long gcd(long long i, long long j) {
    if (j == 0)return i;
    else return gcd(j, i % j);
}
template<typename T> inline T getint() {
    T val = 0;
    char c;

    bool neg = false;
    while ((c = getchar()) && !(c >= '0' && c <= '9')) {
        neg |= c == '-';
    }

    do {
        val = (val * 10) + c - '0';
    } while ((c = getchar()) && (c >= '0' && c <= '9'));

    return val * (neg ? -1 : 1);
}
const long long INF = 1e9;
const int mod = 1000000007;
const double eps = 1e-9, pi = acos(-1);
const int maxN = 1000100, maxT = 2001;
void file_inc() {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
}
int32_t main()
{
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(false);
    cout.precision(15);
   // file_inc();
    int t;
    cin >> t;
    while (t--) {
        int n, k;
        cin >> n >> k;
        string s;
        cin >> s;
        vector<int>id;
        int sum = 0;
        int ln = -1;
        int ix = 0;
        while (ix < n && s[ix] == 'L')++ix;
        vector<int>lns;
        for (int i = ix; i < n; ++i) {
            if (s[i] == 'W') {
                if (i == 0 || s[i - 1] == 'L') {
                    if (ln != -1)lns.push_back(ln);
                    ln = 0;
                    id.push_back(i);
                    ++sum;
                }
                else {
                    sum += 2;
                }
            }
            else ++ln;
        }
        sort(lns.begin(), lns.end());
        if (sum == 0) {
            cout << max(0, 2 * min(n, k) - 1) << "\n";
            continue;
        }
        for (auto x : lns) {
            if (x > k) {
                ln += x;
                break;
            }
            if (x == k) {
                sum += 2 * k + 1;
                k = 0;
                break;
            }
            if (k > x) {
                sum += 2 * x + 1;
                k -= x;
            }
        }
        if (k == 0) {
            cout << sum << "\n";
            continue;
        }
        if (k >= ix) {
            sum += 2 * ix;
            k -= ix;
        }
        else {
            if (k > ln) {
                sum += 2 * ln;
                k -= ln;
            }
            else {
                sum += 2 * k;
                k = 0;
            }
            if (k != 0) {
                sum += 2 * k;
                k = 0;
            }
        }
        if (k != 0) {
            sum += min(k, ln) * 2;
        }
        cout << sum << "\n";
    }
    return 0;
}


#pragma comment(linker, "/STACK:9759095000")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,abm,mmx,tune=native")
#include<vector>
#include<iostream>
#include<stack>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<string>
#include<tuple>
#include<bitset>
#include<queue>
#include <unordered_map>
#include<random>
#include<ctime>
#define double long double
//#define int long long
using namespace std;
long long gcd(long long i, long long j) {
    if (j == 0)return i;
    else return gcd(j, i % j);
}
template<typename T> inline T getint() {
    T val = 0;
    char c;

    bool neg = false;
    while ((c = getchar()) && !(c >= '0' && c <= '9')) {
        neg |= c == '-';
    }

    do {
        val = (val * 10) + c - '0';
    } while ((c = getchar()) && (c >= '0' && c <= '9'));

    return val * (neg ? -1 : 1);
}
const long long INF = 1e9;
const int mod = 1000000007;
const double eps = 1e-9, pi = acos(-1);
const int maxN = 1000100, maxT = 2001;
void file_inc() {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
}
int32_t main()
{
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(false);
    cout.precision(15);
   // file_inc();
    int t;
    cin >> t;
    while (t--) {
        int n, k;
        cin >> n >> k;
        string s;
        cin >> s;
        vector<int>id;
        int sum = 0;
        int ln = -1;
        int ix = 0;
        while (ix < n && s[ix] == 'L')++ix;
        vector<int>lns;
        for (int i = ix; i < n; ++i) {
            if (s[i] == 'W') {
                if (i == 0 || s[i - 1] == 'L') {
                    if (ln != -1)lns.push_back(ln);
                    ln = 0;
                    id.push_back(i);
                    ++sum;
                }
                else {
                    sum += 2;
                }
            }
            else ++ln;
        }
        sort(lns.begin(), lns.end());
        if (sum == 0) {
            cout << max(0, 2 * min(n, k) - 1) << "\n";
            continue;
        }
        for (auto x : lns) {
            if (x > k) {
                ln += x;
                break;
            }
            if (x == k) {
                sum += 2 * k + 1;
                k = 0;
                break;
            }
            if (k > x) {
                sum += 2 * x + 1;
                k -= x;
            }
        }
        if (k == 0) {
            cout << sum << "\n";
            continue;
        }
        if (k >= ix) {
            sum += 2 * ix;
            k -= ix;
        }
        else {
            if (k > ln) {
                sum += 2 * ln;
                k -= ln;
            }
            else {
                sum += 2 * k;
                k = 0;
            }
            if (k != 0) {
                sum += 2 * k - 1;
                k = 0;
            }
        }
        if (k != 0) {
            sum += min(k, ln) * 2;
        }
        cout << sum << "\n";
    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <unordered_set>
#include <iostream>
#include <fstream>
#include <chrono>

using namespace std;

const int DIV = 1000000007;

void solve(int test, istream &in) {
    int n, k; std::string s;
    in >> n >> k >> s;
    int sum = 0;
    vector<int> a, b;
    for (int i = 0; i < n; i++) {
        if (s[i] == 'W') {
            int j = i;
            while (j + 1 < n && s[j + 1] == 'W')
                j++;
            a.push_back(i);
            b.push_back(j);
            sum += j - i + 1;
            i = j;
        }
    }
    if (a.size() == 0) {
        sum = std::max(k * 2 - 1, 0);
        printf("%d\n", sum);
        return;
    }
    k = std::min(k, n - sum);
    vector<int> c(a.size() - 1);
    for (int i = 0; i < c.size(); i++) {
        c[i] = a[i + 1] - b[i] - 1;
    }
    sort(c.begin(), c.end());
    sum += k;
    sum = sum * 2 - a.size();
    for (int i = 0; i < c.size(); i++) {
        if (k >= c[i])
            sum++;
        k -= c[i];
    }
    printf("%d\n", sum);
}

int main(int argc, char* argv[])
{
#ifdef VLAD_LOCAL
    //FILE *f = fopen("in.txt", "r");
    ifstream f("in.txt");
    auto start = std::chrono::steady_clock::now();
#else
    //FILE *f = stdin;
    istream &f = cin;
#endif
    int tests;
    //tests = 1;
    f >> tests;
    for (int test = 0; test < tests; test++) {
        solve(test, f);
    }
#ifdef VLAD_LOCAL
    auto end = std::chrono::steady_clock::now();
    double seconds = (double)std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() / 1000;
    printf("%.3lf\n", seconds);
#endif
    return 0;
}


#include<stdio.h>
#include<algorithm>
using namespace std;
char a[100005];
struct st
{
	int last;
	int len;
}p[100005];
struct cmp
{
	bool operator()(const st&t1,const st&t2)
	{
		return t1.len<t2.len;
	 }
};
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		int n,k;
		scanf("%d%d",&n,&k);
		scanf("%s",a);
		int pos=0;
		int last,r=0;
		for(int i=0;i<n;i++)
		{
			if(a[i]=='W'&&pos==0)
			{
				last=i;
				pos=1;
			}
			else if(a[i]=='W'&&pos)
			{
				int len=i-last-1;
				if(len!=0)
				{
					p[r].last=last;
					p[r++].len=len;
				}
				last=i;
				//printf("%d   ",last);
			}
		}
		sort(p,p+r,cmp());
		//printf("%d  ",p[0].len);
		for(int i=0;i<r;i++)
		{
			int l=p[i].last;
			for(int i=l+1;i<n;i++)
			{
				if(a[i]=='W')
				break;
				if(k==0)
				break;
				a[i]='W';
				k--;
			}
		}
		for(int i=0;i<n;i++)
		{
			if(k==0)
			break;
			if(a[i]=='L'&&(a[i+1]=='W'||a[i-1]=='W'))
			{
				a[i]='W';
				k--;
			}
		}
		for(int i=n-1;i>=0;i--)
		{
			if(k==0)
			break;
			if(a[i]=='L'&&(a[i+1]=='W'||a[i-1]=='W'))
			{
				a[i]='W';
				k--;
			}
		}
		for(int i=0;i<n;i++)
		{
			if(k==0)
			break;
			if(a[i]=='L')
			{
				a[i]='W';
				k--;
			}
		}
		//printf("%s",a);
		long long sum=0;
		int flag=0;
		for(int i=0;i<n;i++)
		{
			if(a[i]=='W'&&flag==0)
			{
				sum++;
				flag=1;
			}
			else if(a[i]=='W'&&flag==1)
			sum+=2;
			else if(a[i]=='L')
			flag=0;
		}
		printf("%d\n",sum);

	}
}


#include<stdio.h>
#include<algorithm>
using namespace std;
char a[100005];
struct st
{
	int last;
	int len;
}p[100005];
struct cmp
{
	bool operator()(const st&t1,const st&t2)
	{
		return t1.len<t2.len;
	 }
};
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		int n,k;
		scanf("%d%d",&n,&k);
		scanf("%s",a);
		int pos=0;
		int last,r=0;
		for(int i=0;i<n;i++)
		{
			if(a[i]=='W'&&pos==0)
			{
				last=i;
				pos=1;
			}
			else if(a[i]=='W'&&pos)
			{
				int len=i-last-1;
				if(len!=0)
				{
					p[r].last=last;
					p[r++].len=len;
				}
				last=i;
				//printf("%d   ",last);
			}
		}
		sort(p,p+r,cmp());
		//printf("%d  ",p[0].len);
		for(int i=0;i<r;i++)
		{
			int l=p[i].last;
			for(int i=l+1;i<n;i++)
			{
				if(a[i]=='W')
				break;
				if(k==0)
				break;
				a[i]='W';
				k--;
			}
		}
		for(int i=0;i<n;i++)
		{
			if(k==0)
			break;
			if(a[i]=='L'&&(a[i+1]=='W'&&a[i-1]=='W'))
			{
				a[i]='W';
				k--;
			}
		}
		for(int i=n-1;i>=0;i--)
		{
			if(k==0)
			break;
			if(a[i]=='L'&&(a[i+1]=='W'&&a[i-1]=='W'))
			{
				a[i]='W';
				k--;
			}
		}
		for(int i=0;i<n;i++)
		{
			if(k==0)
			break;
			if(a[i]=='L')
			{
				a[i]='W';
				k--;
			}
		}
		//printf("%s",a);
		long long sum=0;
		int flag=0;
		for(int i=0;i<n;i++)
		{
			if(a[i]=='W'&&flag==0)
			{
				sum++;
				flag=1;
			}
			else if(a[i]=='W'&&flag==1)
			sum+=2;
			else if(a[i]=='L')
			flag=0;
		}
		printf("%d\n",sum);

	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>

using namespace std;

bool cmp(int a, int b) {
	return a > b;
}

int main()
{
	int tc;
	cin >> tc;
	while (tc--) {
		int n, k;
		cin >> n >> k;
		vector<int> games = vector<int>(n + 2);
		vector<int> cheat = vector<int>(n + 2);
		string ip;
		cin >> ip;
		int score = 0;
		for (int i = 0; i < ip.size();i++) {
			char c = ip[i];
			if (c == 'W') {
				games[i + 1] = 1;
				if (games[i] > 0) games[i + 1] = 2;
			}
			if (c == 'L') {
				games[i + 1] = 0;
			}

			score += games[i + 1];
		}

		for (int x = 0; x < k; x++) {
			int cur = -1;
			int maxscore = 0;
			for (int i = 1; i <= ip.size(); i++) {
				if (games[i] == 0) {
					if (games[i - 1] > 0 && games[i + 1] > 0) {
						cur = i;
						maxscore = 3;
						break;
					}
					else if (games[i - 1] > 0 || games[i + 1] > 0) {
						if (maxscore < 2) {
							maxscore = 2;
							cur = i;
						}
						if (maxscore == 2) {
							int idx = i - (k - x);
							if (idx > 0) {
								if (games[idx] > 0) {
									cur = i;
								}
							}
							idx = i + (k - x);
							if (idx <= ip.size()) {
								if (games[idx] > 0) {
									cur = i;
								}
							}
						}
					}
					else  if (games[i - 1] == 0 && games[i + 1] == 0) {
						if (maxscore < 1) {
							maxscore = 1;
							cur = i;
						}
					}
				}
			}

			if (cur == -1) break;
			games[cur] = maxscore;
			score += maxscore;
		}

		cout << score << "\n";

	}

	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>

using namespace std;

bool cmp(int a, int b) {
	return a > b;
}

int main()
{
	int tc;
	cin >> tc;
	while (tc--) {
		int n, k;
		cin >> n >> k;
		vector<int> games = vector<int>(n + 2);
		vector<int> cheat = vector<int>(n + 2);
		string ip;
		cin >> ip;
		int score = 0;
		for (int i = 0; i < ip.size();i++) {
			char c = ip[i];
			if (c == 'W') {
				games[i + 1] = 1;
				if (games[i] > 0) games[i + 1] = 2;
			}
			if (c == 'L') {
				games[i + 1] = 0;
			}

			score += games[i + 1];
		}

		for (int x = 0; x < k; x++) {
			int cur = -1;
			int maxscore = 0;
			for (int i = 1; i <= ip.size(); i++) {
				if (games[i] == 0) {
					if (games[i - 1] > 0 && games[i + 1] > 0) {
						cheat[i] = 3;
						cur = i;
						maxscore = 3;
						break;
					}
					else if (games[i - 1] > 0 || games[i + 1] > 0) {
						cheat[i] = 2;
						if (maxscore < 2) {
							maxscore = 2;
							cur = i;
						}
					}
					else {
						cheat[i] = 1;
						if (maxscore < 1) {
							maxscore = 1;
							cur = i;
						}
					}
				}
			}

			if (cur == -1) break;
			games[cur] = maxscore;
			score += maxscore;
		}

		cout << score << "\n";

	}

	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>

using namespace std;

bool cmp(int a, int b) {
	return a > b;
}

int main()
{
	int tc;
	cin >> tc;
	while (tc--) {
		int n, k;
		cin >> n >> k;
		vector<int> games = vector<int>(n + 2);
		vector<int> lose;
		string ip;
		cin >> ip;
		int score = 0;
		int wc = k;
		int wsc = 0;
		for (int i = 0; i  < ip.size();i++) {
			char c = ip[i];
			if (c == 'W') {
				games[i + 1] = 1;
				if (games[i] == 0) {
					wsc += 1;
				}
				wc += 1;
			}
			if (c == 'L') {
				games[i + 1] = 0;
				if (games[i] == 1) {
					lose.push_back(1);
				}
				if (games[i] == 0) {
					if (lose.size() == 0) {}
					else {
						lose[lose.size() - 1]++;
					}
				}
			}
		}

		if (games[games.size() - 2] == 0 && lose.size() > 0) lose.pop_back();
		if (wsc == 0 && wc > 0) wsc = 1;
		if (wc >= n) {
			cout << n * 2 - 1 << "\n";
			continue;
		}

		sort(lose.begin(), lose.end());

		int lc = 0;
		while (lc < lose.size()) {
			if (k < lose[lc]) break;
			k -= lose[lc];
			wsc--;
			lc++;
		}

		cout << wc * 2 - wsc << "\n";
	}

	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>

using namespace std;

bool cmp(int a, int b) {
	return a > b;
}

int main()
{
	int tc;
	cin >> tc;
	while (tc--) {
		int n, k;
		cin >> n >> k;
		vector<int> games = vector<int>(n + 2);
		vector<int> cheat = vector<int>(n + 2);
		string ip;
		cin >> ip;
		int score = 0;
		for (int i = 0; i < ip.size();i++) {
			char c = ip[i];
			if (c == 'W') {
				games[i + 1] = 1;
				if (games[i] > 0) games[i + 1] = 2;
			}
			if (c == 'L') {
				games[i + 1] = 0;
			}

			score += games[i + 1];
		}

		for (int x = 0; x < k; x++) {
			int cur = -1;
			int maxscore = 0;
			for (int i = 1; i <= ip.size(); i++) {
				if (games[i] == 0) {
					if (games[i - 1] > 0 && games[i + 1] > 0) {
						cur = i;
						maxscore = 3;
						break;
					}
					else if (games[i - 1] > 0 || games[i + 1] > 0) {
						if (maxscore < 2) {
							maxscore = 2;
							cur = i;
						}
						if (maxscore == 2 && (cur == 1 || cur == ip.size())) {
							cur = i;
						}
					}
					else  if (games[i - 1] == 0 && games[i + 1] == 0) {
						if (maxscore < 1) {
							maxscore = 1;
							cur = i;
						}
					}
				}
			}

			if (cur == -1) break;
			games[cur] = maxscore;
			score += maxscore;
		}

		cout << score << "\n";

	}

	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>

using namespace std;

bool cmp(int a, int b) {
	return a > b;
}

int main()
{
	int tc;
	cin >> tc;
	while (tc--) {
		int n, k;
		cin >> n >> k;
		vector<int> games = vector<int>(n + 2);
		vector<int> lose;
		string ip;
		cin >> ip;
		int score = 0;
		int wc = k;
		int wsc = 0;
		for (int i = 0; i  < ip.size();i++) {
			char c = ip[i];
			if (c == 'W') {
				games[i + 1] = 1;
				if (games[i] == 0) {
					wsc += 1;
				}
				wc += 1;
			}
			if (c == 'L') {
				games[i + 1] = 0;
				if (games[i] == 1) {
					lose.push_back(1);
				}
				if (games[i] == 0) {
					if (lose.size() == 0) lose.push_back(0);
					lose[lose.size() - 1]++;
				}
			}
		}

		if (wc >= n) {
			cout << wc * 2 - 1 << "\n";
			continue;
		}

		sort(lose.begin(), lose.end());

		int lc = 0;
		while (k >= lose[lc] && lc < lose.size()) {
			k -= lose[lc];
			wsc--;
			lc++;
		}

		cout << wc * 2 - wsc << "\n";
	}

	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>

using namespace std;

bool cmp(int a, int b) {
	return a > b;
}

int main()
{
	int tc;
	cin >> tc;
	while (tc--) {
		int n, k;
		cin >> n >> k;
		vector<int> games = vector<int>(n + 2);
		vector<int> lose;
		string ip;
		cin >> ip;
		int score = 0;
		int wc = k;
		int wsc = 0;
		for (int i = 0; i  < ip.size();i++) {
			char c = ip[i];
			if (c == 'W') {
				games[i + 1] = 1;
				if (games[i] == 0) {
					wsc += 1;
				}
				wc += 1;
			}
			if (c == 'L') {
				games[i + 1] = 0;
				if (games[i] == 1) {
					lose.push_back(1);
				}
				if (games[i] == 0) {
					if (lose.size() == 0) {}
					else {
						lose[lose.size() - 1]++;
					}
				}
			}
		}

		if (games[games.size() - 2] == 0 && lose.size() > 0) lose.pop_back();

		if (wc >= n) {
			cout << n * 2 - 1 << "\n";
			continue;
		}

		sort(lose.begin(), lose.end());

		int lc = 0;
		while (lc < lose.size()) {
			if (k < lose[lc]) break;
			k -= lose[lc];
			wsc--;
			lc++;
		}

		cout << wc * 2 - wsc << "\n";
	}

	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>

using namespace std;

bool cmp(int a, int b) {
	return a > b;
}

int main()
{
	int tc;
	cin >> tc;
	while (tc--) {
		int n, k;
		cin >> n >> k;
		vector<int> games = vector<int>(n + 2);
		vector<int> cheat = vector<int>(n + 2);
		string ip;
		cin >> ip;
		int score = 0;
		for (int i = 0; i < ip.size();i++) {
			char c = ip[i];
			if (c == 'W') {
				games[i + 1] = 1;
				if (games[i] > 0) games[i + 1] = 2;
			}
			if (c == 'L') {
				games[i + 1] = 0;
			}

			score += games[i + 1];
		}

		for (int x = 0; x < k; x++) {
			int cur = -1;
			int maxscore = 0;
			for (int i = 1; i <= ip.size(); i++) {
				if (games[i] == 0) {
					if (games[i - 1] > 0 && games[i + 1] > 0) {
						cur = i;
						maxscore = 3;
						break;
					}
					else if (games[i - 1] > 0 || games[i + 1] > 0) {
						if (maxscore < 2) {
							maxscore = 2;
							cur = i;
						}
						if (maxscore == 2) {
							if (cur == 1 || cur == ip.size()) {
								cur = i;
							}
							int idx = i - (k - x);
							if (idx > 0) {
								if (games[idx] > 0) {
									cur = i;
								}
							}
							idx = i + (k - x);
							if (idx <= ip.size()) {
								if (games[idx] > 0) {
									cur = i;
								}
							}
						}
					}
					else  if (games[i - 1] == 0 && games[i + 1] == 0) {
						if (maxscore < 1) {
							maxscore = 1;
							cur = i;
						}
					}
				}
			}

			if (cur == -1) break;
			games[cur] = maxscore;
			score += maxscore;
		}

		cout << score << "\n";

	}

	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>

using namespace std;

bool cmp(int a, int b) {
	return a > b;
}

int main()
{
	int tc;
	cin >> tc;
	while (tc--) {
		int n, k;
		cin >> n >> k;
		vector<int> games = vector<int>(n + 2);
		vector<int> lose;
		string ip;
		cin >> ip;
		int score = 0;
		int wc = k;
		int wsc = 0;
		for (int i = 0; i  < ip.size();i++) {
			char c = ip[i];
			if (c == 'W') {
				games[i + 1] = 1;
				if (games[i] == 0) {
					wsc += 1;
				}
				wc += 1;
			}
			if (c == 'L') {
				games[i + 1] = 0;
				if (games[i] == 1) {
					lose.push_back(1);
				}
				if (games[i] == 0) {
					if (lose.size() == 0) lose.push_back(0);
					lose[lose.size() - 1]++;
				}
			}
		}

		if (wc >= n) {
			cout << n * 2 - 1 << "\n";
			continue;
		}

		sort(lose.begin(), lose.end());

		int lc = 0;
		while (k >= lose[lc] && lc < lose.size()) {
			k -= lose[lc];
			wsc--;
			lc++;
		}

		cout << wc * 2 - wsc << "\n";
	}

	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>

using namespace std;

bool cmp(int a, int b) {
	return a > b;
}

int main()
{
	int tc;
	cin >> tc;
	while (tc--) {
		int n, k;
		cin >> n >> k;
		vector<int> games = vector<int>(n + 2);
		vector<int> cheat = vector<int>(n + 2);
		string ip;
		cin >> ip;
		int score = 0;
		for (int i = 0; i < ip.size();i++) {
			char c = ip[i];
			if (c == 'W') {
				games[i + 1] = 1;
				if (games[i] > 0) games[i + 1] = 2;
			}
			if (c == 'L') {
				games[i + 1] = 0;
			}

			score += games[i + 1];
		}

		for (int x = 0; x < k; x++) {
			int cur = -1;
			int maxscore = 0;
			for (int i = 1; i <= ip.size(); i++) {
				if (games[i] == 0) {
					if (games[i - 1] > 0 && games[i + 1] > 0) {
						cheat[i] = 3;
						cur = i;
						maxscore = 3;
						break;
					}
					else if (games[i - 1] > 0 || games[i + 1] > 0) {
						cheat[i] = 2;
						if (maxscore < 2) {
							maxscore = 2;
							cur = i;
						}
						if (maxscore == 2 && (cur == 1 || cur == ip.size())) {
							cur = i;
						}
					}
					else {
						cheat[i] = 1;
						if (maxscore < 1) {
							maxscore = 1;
							cur = i;
						}
					}
				}
			}

			if (cur == -1) break;
			games[cur] = maxscore;
			score += maxscore;
		}

		cout << score << "\n";

	}

	return 0;
}


#include<cstdio>
#include<cmath>
#include<vector>
#include<string>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<functional>
#include<cstring>

using namespace std;

int main()
{
	//freopen("input.txt", "r", stdin);

	int cases;

	scanf("%d", &cases);

	for (int tc = 0; tc < cases; tc++)
	{
		int n, m;

		scanf("%d %d", &n, &m);

		char input[100001];

		scanf("%s", input);

		const string str = input;

		int sol = 0;
		vector<int> loss;
		char prev = 'W';
		int cur = 0;
		int start = 0;
		int end = 0;

		for (int i = 0; i < n; i++)
		{
			if (str[i] == 'W')
			{
				if (prev == 'W')
				{
					sol += 2;
				}

				else
				{
					if (!start)
					{
						start = cur;
					}

					else
					{
						loss.push_back(cur);
					}

					cur = 0;

					sol++;
				}
			}

			else
			{
				cur++;
			}

			prev = str[i];
		}

		end = cur;

		sort(loss.begin(), loss.end());

		int left = m;

		for (int i = 0; i < loss.size() && left; i++)
		{
			if (left >= loss[i])
			{
				sol += loss[i] * 2 + 1;

				left -= loss[i];
			}

			else
			{
				sol += left * 2;
			}
		}

		if (left)
		{
			sol += min(left, start + end) * 2;
		}

		printf("%d\n", sol);
	}

	return 0;
}


#include<cstdio>
#include<cmath>
#include<vector>
#include<string>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<functional>
#include<cstring>

using namespace std;

int main()
{
	//freopen("input.txt", "r", stdin);

	int cases;

	scanf("%d", &cases);

	for (int tc = 0; tc < cases; tc++)
	{
		int n, m;

		scanf("%d %d", &n, &m);

		char input[100001];

		scanf("%s", input);

		const string str = input;

		int sol = 0;
		vector<int> loss;
		char prev = 'L';
		int cur = 0;
		bool first = (str[0] == 'L');
		int start = 0;
		int end = 0;

		for (int i = 0; i < n; i++)
		{
			if (str[i] == 'W')
			{
				if (prev == 'W')
				{
					sol += 2;
				}

				else
				{
					if (first)
					{
						first = false;

						start = cur;
					}

					else if(cur)
					{
						loss.push_back(cur);
					}

					cur = 0;

					sol++;
				}
			}

			else
			{
				cur++;
			}

			prev = str[i];
		}

		if (!m)
		{
			printf("%d\n", sol);

			continue;
		}

		end = cur;

		if (first)
		{
			printf("%d\n", m * 2 - 1);

			continue;
		}

		sort(loss.begin(), loss.end());

		int left = m;

		for (int i = 0; i < loss.size() && left; i++)
		{
			if (left >= loss[i])
			{
				sol += loss[i] * 2 + 1;

				left -= loss[i];
			}

			else
			{
				sol += left * 2;

				left = 0;
			}
		}

		if (left)
		{
			sol += min(left, start + end) * 2;
		}

		printf("%d\n", sol);
	}

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>

#define MAX(a, b) ((a)>(b)?(a):(b))
#define MIN(a, b) ((a)<(b)?(a):(b))

typedef long long ll;

char list[100005] = { 0 };
int ctnLose[100005] = { 0 };

int count_str_same(char *p, char ch)
{
    char *q = p;
    int m = 0;
    while (*q != '\0')
    {
        if (ch == *q)
            m++;
        q++;
    }
    return m;
}

int count_win(char *p)
{
    int countinus_win = 0;
    int ans = 0;
    while (*p)
    {
        if (*p == 'W')
        {
            countinus_win++;
        }
        else
        {
            if (countinus_win != 0)
            {
                ans += countinus_win * 2 - 1;
                countinus_win = 0;
            }
        }
        p++;
    }
    if (countinus_win != 0)
    {
        ans += countinus_win * 2 - 1;
        countinus_win = 0;
    }
    return ans;
}

int cmp(void *a, void *b)
{
    return *((int *)a) - *((int *)b);
}

int main()
{
    int tt = 1;
    scanf("%d", &tt);

    while (tt--)
    {
        int n, k;
        scanf("%d%d", &n, &k);
        scanf("%s", list);
        int ctnLosecnt = 0;

        memset(ctnLose, 0, sizeof(ctnLose));

        char *pos = list;

        int totLose = count_str_same(list, 'L');

        int inListLose = 0;
        if (totLose <= k)
        {
            printf("%d\n", n * 2 - 1);
        }
        else
        {
            if (list[0] == 'L')
            {
                pos = strchr(pos, 'W');
                if (!pos)
                {
                    printf("%d\n", MAX(k * 2 - 1, 0));
                    continue;
                }
            }

            pos = strchr(pos, 'L');

            while (1)
            {
                char *nextWin = strchr(pos, 'W');
                if (!nextWin)
                {
                    break;
                }
                else
                {
                    inListLose += nextWin - pos;
                    ctnLose[ctnLosecnt++] = nextWin - pos;
                    pos = strchr(nextWin, 'L');
                    if (!pos)
                    {
                        break;
                    }
                }
            }

            qsort(ctnLose, ctnLosecnt, sizeof(int), cmp);

            int ans = 0;
            for (int i = 0; i < ctnLosecnt; i++)
            {
                if (ctnLose[i] <= k)
                {
                    ans += ctnLose[i] * 2 + 1;
                    k -= ctnLose[i];
                }
                else
                {
                    // insufficient k
                    break;
                }
            }

            ans += k * 2;
            ans += count_win(list);
            printf("%d\n", ans);
        }

    }
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>

#define MAX(a, b) ((a)>(b)?(a):(b))
#define MIN(a, b) ((a)<(b)?(a):(b))

typedef long long ll;

char list[100005] = { 0 };
int ctnLose[100005] = { 0 };

int count_str_same(char *p, char ch)
{
    char *q = p;
    int m = 0;
    while (*q != '\0')
    {
        if (ch == *q)
            m++;
        q++;
    }
    return m;
}

int count_win(char *p)
{
    int countinus_win = 0;
    int ans = 0;
    while (*p)
    {
        if (*p == 'W')
        {
            countinus_win++;
        }
        else
        {
            if (countinus_win != 0)
            {
                ans += countinus_win * 2 - 1;
                countinus_win = 0;
            }
        }
        p++;
    }
    if (countinus_win != 0)
    {
        ans += countinus_win * 2 - 1;
        countinus_win = 0;
    }
    return ans;
}

int cmp(void const *a, void const *b)
{
    return *((int *)a) - *((int *)b);
}

int main()
{
    int tt = 1;
    scanf("%d", &tt);

    while (tt--)
    {
        int n, k;
        scanf("%d%d", &n, &k);
        scanf("%s", list);
        int ctnLosecnt = 0;

        memset(ctnLose, 0, sizeof(ctnLose));

        char *pos = list;

        int totLose = count_str_same(list, 'L');

        int inListLose = 0;
        if (totLose <= k)
        {
            printf("%d\n", n * 2 - 1);
        }
        else
        {
            if (list[0] == 'L')
            {
                pos = strchr(pos, 'W');
                if (!pos)
                {
                    printf("%d\n", MAX(k * 2 - 1, 0));
                    continue;
                }
            }

            pos = strchr(pos, 'L');

            while (1)
            {
                char *nextWin = strchr(pos, 'W');
                if (!nextWin)
                {
                    break;
                }
                else
                {
                    inListLose += nextWin - pos;
                    ctnLose[ctnLosecnt++] = nextWin - pos;
                    pos = strchr(nextWin, 'L');
                    if (!pos)
                    {
                        break;
                    }
                }
            }

            qsort(ctnLose, ctnLosecnt, sizeof(int), cmp);

            int ans = 0;
            for (int i = 0; i < ctnLosecnt; i++)
            {
                if (ctnLose[i] <= k)
                {
                    ans += ctnLose[i] * 2 + 1;
                    k -= ctnLose[i];
                }
                else
                {
                    // insufficient k
                    break;
                }
            }

            ans += k * 2;
            ans += count_win(list);
            printf("%d\n", ans);
        }

    }
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>

#define MAX(a, b) ((a)>(b)?(a):(b))
#define MIN(a, b) ((a)<(b)?(a):(b))

typedef long long ll;

char list[100005] = { 0 };
int ctnLose[100005] = { 0 };

int count_str_same(char *p, char ch)
{
    char *q = p;
    int m = 0;
    while (*q != '\0')
    {
        if (ch == *q)
            m++;
        q++;
    }
    return m;
}

int count_win(char *p)
{
    int countinus_win = 0;
    int ans = 0;
    while (*p)
    {
        if (*p == 'W')
        {
            countinus_win++;
        }
        else
        {
            if (countinus_win != 0)
            {
                ans += countinus_win * 2 - 1;
                countinus_win = 0;
            }
        }
        p++;
    }
    if (countinus_win != 0)
    {
        ans += countinus_win * 2 - 1;
        countinus_win = 0;
    }
    return ans;
}

int cmp(void const *a, void const *b)
{
    return *((int *)a) - *((int *)b);
}

int main()
{
    int tt = 1;
    scanf("%d", &tt);

    while (tt--)
    {
        int n, k;
        scanf("%d%d", &n, &k);
        scanf("%s", list);
        int ctnLosecnt = 0;

        memset(ctnLose, 0, sizeof(ctnLose));

        char *pos = list;

        int totLose = count_str_same(list, 'L');

        int inListLose = 0;
        if (totLose <= k)
        {
            printf("%d\n", n * 2 - 1);
        }
        else
        {
            if (list[0] == 'L')
            {
                pos = strchr(pos, 'W');
                if (!pos)
                {
                    printf("%d\n", MAX(k * 2 - 1, 0));
                    continue;
                }
            }

            pos = strchr(pos, 'L');
            if (!pos)
            {
                printf("%d\n", k * 2 + count_win(list));
                continue;
            }

            while (1)
            {
                char *nextWin = strchr(pos, 'W');
                if (!nextWin)
                {
                    break;
                }
                else
                {
                    inListLose += nextWin - pos;
                    ctnLose[ctnLosecnt++] = nextWin - pos;
                    pos = strchr(nextWin, 'L');
                    if (!pos)
                    {
                        break;
                    }
                }
            }

            qsort(ctnLose, ctnLosecnt, sizeof(int), cmp);

            int ans = 0;
            for (int i = 0; i < ctnLosecnt; i++)
            {
                if (ctnLose[i] <= k)
                {
                    ans += ctnLose[i] * 2 + 1;
                    k -= ctnLose[i];
                }
                else
                {
                    // insufficient k
                    break;
                }
            }

            ans += k * 2;
            ans += count_win(list);
            printf("%d\n", ans);
        }

    }
    return 0;
}


#include<iostream>
#include<stdio.h>
#include <sstream>
#include <cstdio>
#include<fstream>
#include<algorithm>
#include<vector>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <numeric>
#include <vector>
#include<unordered_map>
#include <stdio.h>
#include <string.h>
#include <math.h>

using namespace std;
#define _USE_MATH_DEFINES

# define M_PI           3.14159265358979323846  /* pi */
#define ll long long
#define ld long double
#define vbe(v) ((v).begin()), ((v).end())
#define sz(v)     ((int)((v).size()))
#define clr(v, d)   memset(v, d, sizeof(v))
#define rep(i, v)   for(int i=0;i<sz(v);++i)
#define lp(i, n)    for(int i=0;i<(int)(n);++i)
#define lpi(i, j, n)  for(int i=(j);i<(int)(n);++i)
#define lpd(i, j, n)  for(int i=(j);i>=(int)(n);--i)
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
ll GCD(ll a, ll b) { return (a) ? GCD(b % a, a) : b; }
ll LCM(ll a, ll b) { return a * b / GCD(a, b); }
ll fastpow(ll b, ll p) { if (!p) return 1; ll ret = fastpow(b, p >> 1); ret *= ret; if (p & 1) ret *= b; return ret; }
string alpha = "abcdefghijklmnopqrstuvwxyz";
int divisor(int number)
{
    int i;
    for (i = 2; i <= sqrt(number); i++)
    {
        if (number % i == 0)
        {
            return number / i;
        }
    }
    return 1;
}
int myXOR(int x, int y)
{
    int res = 0; // Initialize result

    // Assuming 32-bit Integer
    for (int i = 31; i >= 0; i--)
    {
        // Find current bits in x and y
        bool b1 = x & (1 << i);
        bool b2 = y & (1 << i);

        // If both are 1 then 0 else xor is same as OR
        bool xoredBit = (b1 & b2) ? 0 : (b1 | b2);

        // Update result
        res <<= 1;
        res |= xoredBit;
    }
    return res;
}
//std::getline(std::cin, a); //read string with spaces
void printDivisors(int n, vector<int>& v)
{
    // Note that this loop runs till square root
    for (int i = 1; i <= sqrt(n); i++)
    {
        if (n % i == 0)
        {
            // If divisors are equal, print only one
            if (n / i == i && i > 1)
                v.push_back(i);

            else // Otherwise print both
            {
                if (i > 1)
                    v.push_back(i);
                if (n / i > 1)
                    v.push_back(n / i);
            }
        }
    }
}
int bin(vector<int>vec, int val)
{
    int l = 0, r = vec.size() - 1, mid = r / 2;
    while (l <= r)
    {
        mid = (l + r) / 2;

        if (vec[mid]<val && vec[mid + 1]>val)
        {
            if (vec[mid + 1] == val)
                return mid + 1;
            return mid;

        }
        else if (vec[mid] > val)
        {
            r = mid - 1;
        }
        else if (vec[mid] < val)
        {
            l = mid + 1;
        }
        else if (vec[mid] == val)
            return mid;

    }
    return -1;
}
void clear(vector<bool>v)
{
    for (int i = 0; i < v.size(); i++)
        v[i] = 0;
}

bool comp(const string& s1, const string& s2)
{
    // Suppose s1 = 900, s2 = 9, then it compares
    // 9900 with 9009.
    return s2 + s1 < s1 + s2;
}
vector<string> split(const string& s, char delim) {
    vector<string> result;
    stringstream ss(s);
    string item;

    while (getline(ss, item, delim)) {
        result.push_back(item);
    }

    return result;
}
int countWords(string str)
{
    // breaking input into word using string stream
    stringstream s(str); // Used for breaking words
    string word; // to store individual words

    int count = 0;
    while (s >> word)
        count++;
    return count;
}
ll power(ll x, ll y, ll p, int& c)
{
    int res = 1;     // Initialize result
    if (x >= p)
        c++;
    x = x % p; // Update x if it is more than or
                // equal to p

    if (x == 0) return 0; // In case x is divisible by p;

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
        {
            if (res * x >= p)
                c++;
            res = (res * x) % p;
        }

        // y must be even now
        y = y >> 1; // y = y/2
        if (x * x >= p)
            c++;
        x = (x * x) % p;
    }
    return res;
}
bool isPalindrome(string str)
{
    // Start from leftmost and rightmost corners of str
    int l = 0;
    int h = str.length() - 1;

    // Keep comparing characters while they are same
    while (h > l)
    {
        if (str[l++] != str[h--])
        {

            return 0;
        }
    }
    return 1;
}
bool checkMuns(vector<int>vec)
{
    lp(i, vec.size())
    {
        if (vec[i] < 0)
            return 0;
    }
    return 1;
}
void fast()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}
ll power(ll x, ll y, ll p)
{
    ll res = 1;     // Initialize result

    x = x % p; // Update x if it is more than or
                // equal to p

    if (x == 0) return 0; // In case x is divisible by p;

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res * x) % p;

        // y must be even now
        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }
    return res;
}
ll ceiLl(ll x, ll y)
{
    return (x + y - 1) / y;
}
vector<ll> divisors(ll n)
{
    vector<ll>ans;
    ll i = 1;
    for (; i * i < n; i++)
    {
        if (n % i == 0)
        {
            ans.push_back(i);
            ans.push_back(n / i);
        }
    }
    if (i * i == n)
        ans.push_back(i);
    return ans;
}
vector<ll> findDivisors(ll n)//form 1 to n
{

    // Array to store the count
    // of divisors
    vector<ll> numFactors(n + 1);

    // For every number from 1 to n
    for (int i = 1; i <= n; i++) {

        // Increase divisors count for
        // every number divisible by i
        for (int j = 1; j * i <= n; j++)
            numFactors[i * j]++;
    }

    return numFactors;
}
vector<ll> factorization(ll n) //	 max n is 1e12
{	 //O(sqrt(n))

    vector<ll> primes;

    for (ll i = 2; i * i <= n; ++i)	// Improve start by i = 3.
    {
        if (n % i == 0)
        {

            primes.push_back(i);
            while (n % i == 0)
            {
                n /= i;
            }
        } //Get every prime inside n.n  i^ j	is a new number
    }
    if (n > 1)
        primes.push_back(n);

    return primes;
}
bool isSubSeq(string a, string b)
{
    int idx = 0;
    for (int i = 0; i < a.length(); i++)
    {
        if (a[i] == b[idx])
            idx++;
    }
    if (idx == b.length())
        return 1;
    else
        return 0;
}
bool isSubStr(string a, string b)
{

    for (int i = 0; i < a.length(); i++)
    {
        if (a.substr(i, b.length()) == b)
            return 1;
    }
        return 0;
}
bool sortbysec(const pair<int, int>& a,
    const pair<int, int>& b)
{
    return (a.second < b.second);
}
int solve(string str)
{
    vector<int>L;
    lp(i, str.length())
    {
        if (str[i] == 'W')
        {
            int j = i + 1;
            int l = 1;
            while (j != str.length() && str[j] == 'W')
            {
                l++;
                j++;
            }
            L.push_back(l);
            i = j - 1;
        }
    }
    int sum = 0;
    lp(i, L.size())
        sum += L[i] * 2-1;
    return sum;
}
int main()
{
    fast();
    int t;
    cin >> t;
    while(t--)
    {
        int n, k;
        cin >> n >> k;
        string str;
        cin >> str;
        bool allL = 1;
        lp(i, str.length())
        {
            if (str[i] == 'W')
            {
                allL = 0;
                break;
            }

        }
        vector<int>L;
        ll score = solve(str);
        if (k == 0)
        {
            cout << score;
        }
        else if (n == 1)
        {
            if (str == "L")
                cout << 1;
            else
                cout << 1;
        }
        else if (allL)
        {
            cout << min(n, k) * 2 - 1;
        }
        else
        {
            int firstw = 0, lastw = 0;
            while (firstw!=n&&str[firstw] == 'L')
                firstw++;
            lpi(i, firstw, str.length())
            {
                if (str[i] == 'L')
                {
                    int j = i + 1;
                    int l = 1;
                    while (j != n && str[j] == 'L')
                    {
                        l++;
                        j++;
                    }
                    L.push_back(l);
                    i = j - 1;
                }
            }
            if (str[n - 1] == 'L')
            {
                if (L.size())
                {
                    lastw = L[L.size() - 1];
                    L.pop_back();
                }

            }

            sort(vbe(L));
            lp(i, L.size())
            {
                if(!k)
                    break;
                if (k >= L[i])
                {
                    k -= L[i];
                    score += L[i] * 2 + 1;

                }
                else {
                    score += (k) * 2;
                    k = 0;
                }

            }
            if (k)
            {
                if (k >= lastw)
                {
                    k -= lastw;
                    score += lastw * 2;
                }
                else
                {
                    score += k * 2;
                    k = 0;
                }
                if (k)
                {
                    if (k >= firstw)
                    {
                        k -= firstw;
                        score += firstw * 2;
                    }
                    else
                    {
                        score += k * 2;
                        k = 0;
                    }
                }

            }
            cout << score;
        }
        cout << "\n";
    }

    return 0;
}

/*
        std::cout << std::fixed;
    std::cout << std::setprecision(12);
*/


#include<iostream>
#include<stdio.h>
#include <sstream>
#include <cstdio>
#include<fstream>
#include<algorithm>
#include<vector>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <numeric>
#include <vector>
#include<unordered_map>
#include <stdio.h>
#include <string.h>
#include <math.h>

using namespace std;
#define _USE_MATH_DEFINES

# define M_PI           3.14159265358979323846  /* pi */
#define ll long long
#define ld long double
#define vbe(v) ((v).begin()), ((v).end())
#define sz(v)     ((int)((v).size()))
#define clr(v, d)   memset(v, d, sizeof(v))
#define rep(i, v)   for(int i=0;i<sz(v);++i)
#define lp(i, n)    for(int i=0;i<(int)(n);++i)
#define lpi(i, j, n)  for(int i=(j);i<(int)(n);++i)
#define lpd(i, j, n)  for(int i=(j);i>=(int)(n);--i)
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
ll GCD(ll a, ll b) { return (a) ? GCD(b % a, a) : b; }
ll LCM(ll a, ll b) { return a * b / GCD(a, b); }
ll fastpow(ll b, ll p) { if (!p) return 1; ll ret = fastpow(b, p >> 1); ret *= ret; if (p & 1) ret *= b; return ret; }
string alpha = "abcdefghijklmnopqrstuvwxyz";
int divisor(int number)
{
    int i;
    for (i = 2; i <= sqrt(number); i++)
    {
        if (number % i == 0)
        {
            return number / i;
        }
    }
    return 1;
}
int myXOR(int x, int y)
{
    int res = 0; // Initialize result

    // Assuming 32-bit Integer
    for (int i = 31; i >= 0; i--)
    {
        // Find current bits in x and y
        bool b1 = x & (1 << i);
        bool b2 = y & (1 << i);

        // If both are 1 then 0 else xor is same as OR
        bool xoredBit = (b1 & b2) ? 0 : (b1 | b2);

        // Update result
        res <<= 1;
        res |= xoredBit;
    }
    return res;
}
//std::getline(std::cin, a); //read string with spaces
void printDivisors(int n, vector<int>& v)
{
    // Note that this loop runs till square root
    for (int i = 1; i <= sqrt(n); i++)
    {
        if (n % i == 0)
        {
            // If divisors are equal, print only one
            if (n / i == i && i > 1)
                v.push_back(i);

            else // Otherwise print both
            {
                if (i > 1)
                    v.push_back(i);
                if (n / i > 1)
                    v.push_back(n / i);
            }
        }
    }
}
int bin(vector<int>vec, int val)
{
    int l = 0, r = vec.size() - 1, mid = r / 2;
    while (l <= r)
    {
        mid = (l + r) / 2;

        if (vec[mid]<val && vec[mid + 1]>val)
        {
            if (vec[mid + 1] == val)
                return mid + 1;
            return mid;

        }
        else if (vec[mid] > val)
        {
            r = mid - 1;
        }
        else if (vec[mid] < val)
        {
            l = mid + 1;
        }
        else if (vec[mid] == val)
            return mid;

    }
    return -1;
}
void clear(vector<bool>v)
{
    for (int i = 0; i < v.size(); i++)
        v[i] = 0;
}

bool comp(const string& s1, const string& s2)
{
    // Suppose s1 = 900, s2 = 9, then it compares
    // 9900 with 9009.
    return s2 + s1 < s1 + s2;
}
vector<string> split(const string& s, char delim) {
    vector<string> result;
    stringstream ss(s);
    string item;

    while (getline(ss, item, delim)) {
        result.push_back(item);
    }

    return result;
}
int countWords(string str)
{
    // breaking input into word using string stream
    stringstream s(str); // Used for breaking words
    string word; // to store individual words

    int count = 0;
    while (s >> word)
        count++;
    return count;
}
ll power(ll x, ll y, ll p, int& c)
{
    int res = 1;     // Initialize result
    if (x >= p)
        c++;
    x = x % p; // Update x if it is more than or
                // equal to p

    if (x == 0) return 0; // In case x is divisible by p;

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
        {
            if (res * x >= p)
                c++;
            res = (res * x) % p;
        }

        // y must be even now
        y = y >> 1; // y = y/2
        if (x * x >= p)
            c++;
        x = (x * x) % p;
    }
    return res;
}
bool isPalindrome(string str)
{
    // Start from leftmost and rightmost corners of str
    int l = 0;
    int h = str.length() - 1;

    // Keep comparing characters while they are same
    while (h > l)
    {
        if (str[l++] != str[h--])
        {

            return 0;
        }
    }
    return 1;
}
bool checkMuns(vector<int>vec)
{
    lp(i, vec.size())
    {
        if (vec[i] < 0)
            return 0;
    }
    return 1;
}
void fast()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}
ll power(ll x, ll y, ll p)
{
    ll res = 1;     // Initialize result

    x = x % p; // Update x if it is more than or
                // equal to p

    if (x == 0) return 0; // In case x is divisible by p;

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res * x) % p;

        // y must be even now
        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }
    return res;
}
ll ceiLl(ll x, ll y)
{
    return (x + y - 1) / y;
}
vector<ll> divisors(ll n)
{
    vector<ll>ans;
    ll i = 1;
    for (; i * i < n; i++)
    {
        if (n % i == 0)
        {
            ans.push_back(i);
            ans.push_back(n / i);
        }
    }
    if (i * i == n)
        ans.push_back(i);
    return ans;
}
vector<ll> findDivisors(ll n)//form 1 to n
{

    // Array to store the count
    // of divisors
    vector<ll> numFactors(n + 1);

    // For every number from 1 to n
    for (int i = 1; i <= n; i++) {

        // Increase divisors count for
        // every number divisible by i
        for (int j = 1; j * i <= n; j++)
            numFactors[i * j]++;
    }

    return numFactors;
}
vector<ll> factorization(ll n) //	 max n is 1e12
{	 //O(sqrt(n))

    vector<ll> primes;

    for (ll i = 2; i * i <= n; ++i)	// Improve start by i = 3.
    {
        if (n % i == 0)
        {

            primes.push_back(i);
            while (n % i == 0)
            {
                n /= i;
            }
        } //Get every prime inside n.n  i^ j	is a new number
    }
    if (n > 1)
        primes.push_back(n);

    return primes;
}
bool isSubSeq(string a, string b)
{
    int idx = 0;
    for (int i = 0; i < a.length(); i++)
    {
        if (a[i] == b[idx])
            idx++;
    }
    if (idx == b.length())
        return 1;
    else
        return 0;
}
bool isSubStr(string a, string b)
{

    for (int i = 0; i < a.length(); i++)
    {
        if (a.substr(i, b.length()) == b)
            return 1;
    }
        return 0;
}
bool sortbysec(const pair<int, int>& a,
    const pair<int, int>& b)
{
    return (a.second < b.second);
}
int solve(string str)
{
    vector<int>L;
    lp(i, str.length())
    {
        if (str[i] == 'W')
        {
            int j = i + 1;
            int l = 1;
            while (j != str.length() && str[j] == 'W')
            {
                l++;
                j++;
            }
            L.push_back(l);
            i = j - 1;
        }
    }
    int sum = 0;
    lp(i, L.size())
        sum += L[i] * 2-1;
    return sum;
}
int main()
{
    fast();
    int t;
    cin >> t;
    while(t--)
    {
        int n, k;
        cin >> n >> k;
        string str;
        cin >> str;
        vector<int>L;
        ll score = solve(str);
        if (k == 0)
        {
            cout << score;
        }
        else if (n == 1)
        {
            if (str == "L")
                cout << 1;
            else
                cout << 1;
        }
        else
        {
            int firstw = 0, lastw = 0;
            while (firstw!=n&&str[firstw] == 'L')
                firstw++;
            lpi(i, firstw, str.length())
            {
                if (str[i] == 'L')
                {
                    int j = i + 1;
                    int l = 1;
                    while (j != n && str[j] == 'L')
                    {
                        l++;
                        j++;
                    }
                    L.push_back(l);
                    i = j - 1;
                }
            }
            if (str[n - 1] == 'L')
            {
                if (L.size())
                {
                    lastw = L[L.size() - 1];
                    L.pop_back();
                }

            }

            sort(vbe(L));
            lp(i, L.size())
            {
                if(!k)
                    break;
                if (k >= L[i])
                {
                    k -= L[i];
                    score += L[i] * 2 + 1;

                }
                else {
                    score += (k) * 2;
                    k = 0;
                }

            }
            if (k)
            {
                score += min(k, lastw + firstw) * 2;
            }
            cout << score;
        }
        cout << "\n";
    }

    return 0;
}

/*
        std::cout << std::fixed;
    std::cout << std::setprecision(12);
*/


#include <iostream>
#include <vector>
#include <unordered_map>
#include <stack>
#include <set>
using namespace std;

int main() {
	int testcases;
	cin >> testcases;
	for (int testnum = 0; testnum < testcases; testnum++) {
		multiset<int> lossruns;
		bool firstwin = false;//True if you start by winning
		int n, k;
		cin >> n >> k;
		string s;
		cin >> s;
		s = '0' + s;
		int currentscore = 0;
		int loss = 0;
		for (int i = 1; i < s.size(); i++) {
			char c1 = s[i];
			char c2 = s[i - 1];
			if (c1 == 'L') {
				loss++;
			}
			if (c1 == 'W' && c2 != c1) {
				currentscore++;
				if (loss != 0) {
					lossruns.insert(loss);
					loss = 0;
				}
			}
			if (c1 == 'W' && c2 == c1) {
				currentscore += 2;
			}
		}
		//Find begin and end losing runs if possible
		int beginrun = 0;
		int endrun = 0;
		int test = 1;
		while (s[test] == 'L') {
			beginrun++;
			test++;
		}
		test = s.size() - 1;
		while (s[test] == 'L') {
			endrun++;
			test--;
		}

		//The score from a run of n is 2n-1
		//If you join two winning runs of m and n with a losing run of p, your score is
		//2n+2m+2p-1, for a total gain of 2p+1
		//If you simply tack p onto the end of a run you gain 2p

		if (k == 0) {
		} else if (beginrun == s.size() - 1) {
			if (k > beginrun) {
				currentscore += 2 * beginrun - 1;
			}
			else {
				currentscore += 2 * k - 1;
			}
		}
		else {

			multiset<int>::iterator it;
			it = lossruns.find(beginrun);
			if (it != lossruns.end()) lossruns.erase(it);
			it = lossruns.begin();
			for (; it != lossruns.end(); it++) {
				if (k < *it) {
					currentscore += 2 * k;
					k = 0;
					break;
				}
				k -= *it;
				currentscore += 2 * (*it) + 1;
			}
			if (k > beginrun) {
				currentscore += 2 * beginrun;
				k -= beginrun;
			}
			else {
				currentscore += 2 * k;
				k = 0;
			}
			if (k > endrun) {
				currentscore += 2 * endrun;
				k -= endrun;
			}
			else {
				currentscore += 2 * k;
				k = 0;
			}
		}
		cout << currentscore << endl;

	}
}


#include <iostream>
#include <vector>
#include <unordered_map>
#include <stack>
#include <set>
#include <string>
using namespace std;

int main() {
	int testcases;
	cin >> testcases;
	for (int testnum = 0; testnum < testcases; testnum++) {
		multiset<int> lossruns;
		bool firstwin = false;//True if you start by winning
		int n, k;
		cin >> n >> k;
		string s;
		getline(cin, s);
		getline(cin, s);
		s = '0' + s;
		int currentscore = 0;
		int loss = 0;
		for (int i = 1; i < s.size(); i++) {
			char c1 = s[i];
			char c2 = s[i - 1];
			if (c1 == 'L') {
				loss++;
			}
			if (c1 == 'W' && c2 != c1) {
				currentscore++;
				if (loss != 0) {
					lossruns.insert(loss);
					loss = 0;
				}
			}
			if (c1 == 'W' && c2 == c1) {
				currentscore += 2;
			}
		}
		//Find begin and end losing runs if possible
		int beginrun = 0;
		int endrun = 0;
		int test = 1;
		while (s[test] == 'L') {
			beginrun++;
			test++;
		}
		test = s.size() - 1;
		while (s[test] == 'L') {
			endrun++;
			test--;
		}

		//The score from a run of n is 2n-1
		//If you join two winning runs of m and n with a losing run of p, your score is
		//2n+2m+2p-1, for a total gain of 2p+1
		//If you simply tack p onto the end of a run you gain 2p

		if (k == 0) {
		} else if (beginrun == s.size() - 1) {
			if (k > beginrun) {
				currentscore += 2 * beginrun - 1;
			}
			else {
				currentscore += 2 * k - 1;
			}
		}
		else {

			multiset<int>::iterator it;
			it = lossruns.find(beginrun);
			if (it != lossruns.end()) lossruns.erase(it);
			it = lossruns.begin();
			for (; it != lossruns.end(); it++) {
				if (k < *it) {
					currentscore += 2 * k;
					k = 0;
					break;
				}
				k -= *it;
				currentscore += 2 * (*it) + 1;
			}
			if (k > beginrun) {
				currentscore += 2 * beginrun;
				k -= beginrun;
			}
			else {
				currentscore += 2 * k;
				k = 0;
			}
			if (k > endrun) {
				currentscore += 2 * endrun;
				k -= endrun;
			}
			else {
				currentscore += 2 * k;
				k = 0;
			}
		}
		cout << currentscore << endl;

	}
}


#include<iostream>
#include<algorithm>
#include<stack>
#include<vector>
#include<queue>
#include<map>
#include<unordered_map>
#include<unordered_set>
#include<string>
using namespace std;

int t=1;
int n,k;
string result;

void checkVal(string r, vector<int>& ret) {
	vector<int> v[3];
	for (int i = 0; i < r.size(); i++) {
		if (r[i] != 'W') {
			int score = 0;
			if (i - 1 >= 0 && r[i - 1] == 'W')
				++score;
			if (i + 1 >= 0 && r[i + 1] == 'W')
				++score;
			v[score].push_back(i);
		}
	}
	for (int i = 2; i >= 0; i--) {
		if (v[i].size() > 0) {
			ret = v[i];
			return;
		}
	}
}

int calScore(string r) {
	int sum = 0;
	bool bFirst = true;
	for (int i = 0; i < r.size(); i++)
	{
		if (r[i] == 'W')
		{
			if (bFirst)
			{
				sum += 1;
				bFirst = false;
			}
			else
			{
				sum += 2;
			}
		}
		else
		{
			bFirst = true;
		}
	}

	return sum;
}

int helper(string r, int chg) {
	vector<int> ret;
	checkVal(r, ret);

	if (chg == 1)
	{
		int n = ret[0];
		r[n] = 'W';
		int ret = calScore(r);
		r[n] = 'L';
		return ret;
	}

	int maxScore = 0;
	for (int i = 0; i < ret.size(); i++)
	{
		int n = ret[i];
		r[n] = 'W';
		int ret = helper(r, chg - 1);
		r[n] = 'L';
		maxScore = maxScore > ret ? maxScore : ret;
	}

	return maxScore;
}

int main()
{
	cin>>t;
	while(t)
	{
		t--;

		// n = 40;
		// k = 7;
		// result = "LLWLWLWWWLWLLWLWWWLWLLWLLWLLLLWLLWWWLWWL";
		cin >> n >> k;
		cin >> result;

		int finalScore;
		if (k <= 0) {
			finalScore = calScore(result);
		} else {
			int lostCount = 0;
			for (int i = 0; i < result.size(); i++)
			{
				if (result[i] != 'W')
					lostCount++;
			}
			if (lostCount <= k) {
				finalScore = result.size() * 2 - 1;
			} else {
				finalScore = helper(result, k);
			}
		}

		cout << finalScore << endl;
	}
	// cout << "hello, world !" << endl;
	// system("pause");
	return 0;
}


#include<iostream>
#include<algorithm>
#include<stack>
#include<vector>
#include<queue>
#include<map>
#include<unordered_map>
#include<unordered_set>

using namespace std;

int t=1;
int n,k;
string result;

void checkVal(string r, vector<int>& ret) {
	vector<int> v[3];
	for (int i = 0; i < r.size(); i++) {
		if (r[i] != 'W') {
			int score = 0;
			if (i - 1 >= 0 && r[i - 1] == 'W')
				++score;
			if (i + 1 >= 0 && r[i + 1] == 'W')
				++score;
			v[score].push_back(i);
		}
	}
	for (int i = 2; i >= 0; i--) {
		if (v[i].size() > 0) {
			ret = v[i];
			return;
		}
	}
}

int calScore(string r) {
	int sum = 0;
	bool bFirst = true;
	for (int i = 0; i < r.size(); i++)
	{
		if (r[i] == 'W')
		{
			if (bFirst)
			{
				sum += 1;
				bFirst = false;
			}
			else
			{
				sum += 2;
			}
		}
		else
		{
			bFirst = true;
		}
	}

	return sum;
}

int helper(string r, int chg) {
	vector<int> ret;
	checkVal(r, ret);

	if (chg == 1)
	{
		int n = ret[0];
		r[n] = 'W';
		int ret = calScore(r);
		r[n] = 'L';
		return ret;
	}

	int maxScore = 0;
	for (int i = 0; i < ret.size(); i++)
	{
		int n = ret[i];
		r[n] = 'W';
		int ret = helper(r, chg - 1);
		r[n] = 'L';
		maxScore = maxScore > ret ? maxScore : ret;
	}

	return maxScore;
}

int main()
{
	cin>>t;
	while(t)
	{
		t--;

		// n = 40;
		// k = 7;
		// result = "LLWLWLWWWLWLLWLWWWLWLLWLLWLLLLWLLWWWLWWL";
		cin >> n >> k;
		cin >> result;

		int finalScore;
		if (k <= 0) {
			finalScore = calScore(result);
		} else {
			int lostCount = 0;
			for (int i = 0; i < result.size(); i++)
			{
				if (result[i] != 'W')
					lostCount++;
			}
			if (lostCount <= k) {
				finalScore = result.size() * 2 - 1;
			} else {
				finalScore = helper(result, k);
			}
		}

		cout << finalScore << endl;
	}
	// cout << "hello, world !" << endl;
	// system("pause");
	return 0;
}


#include<iostream>
#include<algorithm>
#include<stack>
#include<vector>
#include<queue>
#include<map>
#include<unordered_map>
#include<unordered_set>
#include<string>
using namespace std;

int t=1;
int n,k;
string result;

int calScore(string r) {
	int sum = 0;
	bool bFirst = true;
	for (int i = 0; i < r.size(); i++)
	{
		if (r[i] == 'W')
		{
			if (bFirst)
			{
				sum += 1;
				bFirst = false;
			}
			else
			{
				sum += 2;
			}
		}
		else
		{
			bFirst = true;
		}
	}

	return sum;
}

int helper1(string r, int chg) {
	int maxScore = 0;
	int minDiv = 1;
	string findMode = "WLW";
	int lastPos = 0;
	while(chg > 0) {
		if (minDiv <= chg ) {
			int nPos = r.find(findMode, lastPos);
			if (nPos >= 0) {
				r.replace(nPos + 1, minDiv, minDiv, 'W');
				lastPos = nPos + minDiv + 1;
				chg -= minDiv;
			} else {
				minDiv++;
				lastPos = 0;
				if (minDiv > r.size()) {
					minDiv = chg + 1;
				} else {
					findMode = "W";
					findMode.append(minDiv, 'L');
					findMode.push_back('W');
				}
			}
		} else {
			int nPos;

			nPos = r.find("WL");
			if (nPos >= 0) {
				int len = r.size() - nPos - 1;
				len = len > chg ? chg : len;
				r.replace(nPos + 1, len, len, 'W');
				chg -= len;
			} else {
				nPos = r.find("LW");
				if (nPos >= 0) {
					int len = nPos + 1;
					len = len > chg ? chg : len;
					r.replace(nPos - chg + 1, len, len, 'W');
					chg -= len;
				} else {
					r.replace(0, chg, chg, 'W');
					chg = 0;
				}
			}
		}
	}

	maxScore = calScore(r);
	return maxScore;
}

int main()
{
	cin>>t;
	while(t)
	{
		t--;

		// n = 40;
		// k = 7;
		// result = "LLWLWLWWWLWLLWLWWWLWLLWLLWLLLLWLLWWWLWWL";

		// k = 1;
		// result = "WLLWLW";
		cin >> n >> k;
		cin >> result;

		int finalScore;
		if (k <= 0) {
			finalScore = calScore(result);
		} else {
			int lostCount = 0;
			for (int i = 0; i < result.size(); i++)
			{
				if (result[i] != 'W')
					lostCount++;
			}
			if (lostCount <= k) {
				finalScore = result.size() * 2 - 1;
			} else {
				finalScore = helper1(result, k);
			}
		}

		cout << finalScore << endl;
	}
	// cout << "hello, world !" << endl;
	// system("pause");
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <iterator>
#include <queue>

using namespace std;
#define ll long long
#define ull unsigned long long
#define rep(n) for(ll i = 0; i < n; i++)

int main()
{
	int t;
	cin >> t;
	rep(t)
	{
		int n, k, sum = 0;
		cin >> n >> k;
		vector<char> a;
		vector<int> b;
		string s;
		cin >> s;
		int begin = 0, end = n;
		while (begin < n && s[begin] != 'W') begin++;
		while (end > 0 && s[end - 1] != 'W') end--;
		if (begin >= end)
		{
			if (k == 0)
			{
				cout << 0 << endl;
				continue;
			}
			cout << min(n,k) * 2 - 1 << endl;
			continue;
		}
		for (int j = begin; j < end; j++) a.push_back(s[j]);
		if (s[0] == 'W') sum++;
		for (int j = 1; j < n; j++)
		{
			if (s[j] == 'W')
			{
				if (s[j - 1] == 'W') sum += 2;
				else sum++;
			}
		}
		int seq = 0;
		for (int j = 0; j < a.size(); j++)
		{
			if (a[j] == 'L')
			{
				seq++;
			}
			else
			{
				if (seq > 0) b.push_back(seq);
				seq = 0;
			}
		}
		sort(b.begin(), b.end());
		for (int j = 0; j < b.size(); j++)
		{
			if (k >= b[j])
			{
				k -= b[j];
				sum += b[j] * 2 + 1;
			}
			else
			{
				sum += k * 2;
				k = 0;
				break;
			}
		}
		if (k > 0) sum += min(k,begin + n-end) * 2;
		cout << sum << endl;
	}
}


#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include "stdio.h"
using namespace std;
const int N = 1 * 1000 * 1001;
int n, m, k;
int b[N], ind[N];
char s[N];
const long long MOD = 1000 * 1000 * 1000 + 7;
void solve_case()
{

	memset(b, 0, sizeof(b));
	memset(ind, 0, sizeof(ind));
	m = 0;
	cin >> n >> k;
	scanf("%s", &s);
	int startW = -1;
	for (int i = 0; i < n; ++i) {
		if (s[i] == 'W') {
			startW = i;
			break;
		}
	}
	int endW = -1;
	for (int i = n-1; i >= 0; --i) {
		if (s[i] == 'W') {
			endW = i;
			break;
		}
	}

	for (int i = startW + 1; i <= endW; ++i) {
		while (s[i] == 'W' && i <= endW) i++;
		int startL = i;
		if (i >= endW) break;
		while (s[i] == 'L' && i <= endW) i++;
		int endL = i-1;
		int ln = endL - startL + 1;
		if (!b[ln])
			ind[m++] = ln;
		b[ln]++;

	}
	sort(ind, ind + m);
	int sum = 0;
	int rest = k;
	for (int j = 0; j < m; j++) {
		int i = ind[j];
		int restGroups = rest / i;
		int hasGroups = b[i];

		if (restGroups >= hasGroups) {
			sum += hasGroups * (i*2 + 1);
			rest -= hasGroups * i;
			b[i] = 0;
		}
		else {
			sum += restGroups * (i * 2 + 1);
			sum += (rest % i) * (2);
			rest = 0;
		}
	}

	if (endW == -1 && rest > 0) {
		sum += min(rest, n) * 2 - 1;
	}
	else {

		if (rest > 0) {
			int take = min(n - endW - 1, rest);
			sum += take * 2;
			rest -= take;
		}
		if (rest > 0) {
			int take = min(startW, rest);
			sum += take * 2;
		}
	}
	for (int i = 0; i < n; ++i)
		if (s[i] == 'W') {
			sum++;
			if (i && s[i - 1] == 'W')
				sum++;
		}
	cout << sum;
	cout << endl;
}

void prepare()
{
}

int main()
{
#ifdef __OLIMP__
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	int T = 1;
	scanf("%d", &T);
	prepare();
	for (int tc = 1; tc <= T; tc++)
		solve_case();

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include "stdio.h"
using namespace std;
const int N = 1 * 1000 * 1001;
int n, m, k;
int b[N], pow1[N];
const long long MOD = 1000 * 1000 * 1000 + 7;
void solve_case()
{
	string s;

	memset(b, 0, sizeof(b));

	cin >> n >> k;
	cin >> s;
	int startW = -1;
	for (int i = 0; i < n; ++i) {
		if (s[i] == 'W') {
			startW = i;
			break;
		}
	}
	int endW = -1;
	for (int i = n-1; i >= 0; --i) {
		if (s[i] == 'W') {
			endW = i;
			break;
		}
	}

	for (int i = startW + 1; i <= endW; ++i) {
		while (s[i] == 'W') i++;
		int startL = i;
		if (i >= endW) break;
		while (s[i] == 'L') i++;
		int endL = i-1;
		int ln = endL - startL + 1;
		b[ln]++;
	}

	int sum = 0;
	int rest = k;
	for (int i = 1; i < 100 * 1000 + 2; i++) {
		int restGroups = rest / i;
		int hasGroups = b[i];

		if (restGroups >= hasGroups) {
			sum += hasGroups * (i*2 + 1);
			rest -= hasGroups * i;
			b[i] = 0;
		}
		else {
			sum += restGroups * (i * 2 + 1);
			sum += (rest % i) * (2);
			rest = 0;
		}
	}

	if (endW == -1 && rest > 0) {
		sum += min(rest, n) * 2 - 1;
	}
	else {

		if (rest > 0) {
			int take = min(n - endW - 1, rest);
			sum += take * 2;
			rest -= take;
		}
		if (rest > 0) {
			int take = min(startW, rest);
			sum += take * 2;
		}
	}
	for (int i = 0; i < n; ++i)
		if (s[i] == 'W') {
			sum++;
			if (i && s[i - 1] == 'W')
				sum++;
		}
	cout << sum;
	cout << endl;
}

void prepare()
{
}

int main()
{
#ifdef __OLIMP__
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	int T = 1;
	scanf("%d", &T);
	prepare();
	for (int tc = 1; tc <= T; tc++)
		solve_case();

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include "stdio.h"
using namespace std;
const int N = 1 * 1000 * 1001;
int n, m, k;
int b[N], has[N];
const long long MOD = 1000 * 1000 * 1000 + 7;
void solve_case()
{
	string s;

	memset(b, 0, sizeof(b));
	vector<int> ind;
	cin >> n >> k;
	cin >> s;
	int startW = -1;
	for (int i = 0; i < n; ++i) {
		if (s[i] == 'W') {
			startW = i;
			break;
		}
	}
	int endW = -1;
	for (int i = n-1; i >= 0; --i) {
		if (s[i] == 'W') {
			endW = i;
			break;
		}
	}

	for (int i = startW + 1; i <= endW; ++i) {
		while (s[i] == 'W') i++;
		int startL = i;
		if (i >= endW) break;
		while (s[i] == 'L') i++;
		int endL = i-1;
		int ln = endL - startL + 1;
		if (!b[ln])
			ind.push_back(ln);
		b[ln]++;

	}
	sort(ind.begin(), ind.end());
	int sum = 0;
	int rest = k;
	for (int j = 0; j < ind.size(); j++) {
		int i = ind[j];
		int restGroups = rest / i;
		int hasGroups = b[i];

		if (restGroups >= hasGroups) {
			sum += hasGroups * (i*2 + 1);
			rest -= hasGroups * i;
			b[i] = 0;
		}
		else {
			sum += restGroups * (i * 2 + 1);
			sum += (rest % i) * (2);
			rest = 0;
		}
	}

	if (endW == -1 && rest > 0) {
		sum += min(rest, n) * 2 - 1;
	}
	else {

		if (rest > 0) {
			int take = min(n - endW - 1, rest);
			sum += take * 2;
			rest -= take;
		}
		if (rest > 0) {
			int take = min(startW, rest);
			sum += take * 2;
		}
	}
	for (int i = 0; i < n; ++i)
		if (s[i] == 'W') {
			sum++;
			if (i && s[i - 1] == 'W')
				sum++;
		}
	cout << sum;
	cout << endl;
}

void prepare()
{
}

int main()
{
#ifdef __OLIMP__
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	int T = 1;
	scanf("%d", &T);
	prepare();
	for (int tc = 1; tc <= T; tc++)
		solve_case();

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include "stdio.h"
using namespace std;
const int N = 1 * 1000 * 1001;
int n, m, k;
int b[N], has[N];
char s[N];
const long long MOD = 1000 * 1000 * 1000 + 7;
void solve_case()
{

	memset(b, 0, sizeof(b));
	vector<int> ind;
	cin >> n >> k;
	scanf("%s", &s);
	int startW = -1;
	for (int i = 0; i < n; ++i) {
		if (s[i] == 'W') {
			startW = i;
			break;
		}
	}
	int endW = -1;
	for (int i = n-1; i >= 0; --i) {
		if (s[i] == 'W') {
			endW = i;
			break;
		}
	}

	for (int i = startW + 1; i <= endW; ++i) {
		while (s[i] == 'W' && i <= endW) i++;
		int startL = i;
		if (i >= endW) break;
		while (s[i] == 'L' && i <= endW) i++;
		int endL = i-1;
		int ln = endL - startL + 1;
		if (!b[ln])
			ind.push_back(ln);
		b[ln]++;

	}
	sort(ind.begin(), ind.end());
	int sum = 0;
	int rest = k;
	for (int j = 0; j < ind.size(); j++) {
		int i = ind[j];
		int restGroups = rest / i;
		int hasGroups = b[i];

		if (restGroups >= hasGroups) {
			sum += hasGroups * (i*2 + 1);
			rest -= hasGroups * i;
			b[i] = 0;
		}
		else {
			sum += restGroups * (i * 2 + 1);
			sum += (rest % i) * (2);
			rest = 0;
		}
	}

	if (endW == -1 && rest > 0) {
		sum += min(rest, n) * 2 - 1;
	}
	else {

		if (rest > 0) {
			int take = min(n - endW - 1, rest);
			sum += take * 2;
			rest -= take;
		}
		if (rest > 0) {
			int take = min(startW, rest);
			sum += take * 2;
		}
	}
	for (int i = 0; i < n; ++i)
		if (s[i] == 'W') {
			sum++;
			if (i && s[i - 1] == 'W')
				sum++;
		}
	cout << sum;
	cout << endl;
}

void prepare()
{
}

int main()
{
#ifdef __OLIMP__
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	int T = 1;
	scanf("%d", &T);
	prepare();
	for (int tc = 1; tc <= T; tc++)
		solve_case();

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include "stdio.h"
using namespace std;
const int N = 1 * 1000 * 1001;
int n, m, k;
int b[N], has[N];
char s[N];
const long long MOD = 1000 * 1000 * 1000 + 7;
void solve_case()
{

	memset(b, 0, sizeof(b));
	vector<int> ind;
	cin >> n >> k;
	scanf("%s", &s);
	int startW = -1;
	for (int i = 0; i < n; ++i) {
		if (s[i] == 'W') {
			startW = i;
			break;
		}
	}
	int endW = -1;
	for (int i = n-1; i >= 0; --i) {
		if (s[i] == 'W') {
			endW = i;
			break;
		}
	}

	for (int i = startW + 1; i <= endW; ++i) {
		while (s[i] == 'W') i++;
		int startL = i;
		if (i >= endW) break;
		while (s[i] == 'L') i++;
		int endL = i-1;
		int ln = endL - startL + 1;
		if (!b[ln])
			ind.push_back(ln);
		b[ln]++;

	}
	sort(ind.begin(), ind.end());
	int sum = 0;
	int rest = k;
	for (int j = 0; j < ind.size(); j++) {
		int i = ind[j];
		int restGroups = rest / i;
		int hasGroups = b[i];

		if (restGroups >= hasGroups) {
			sum += hasGroups * (i*2 + 1);
			rest -= hasGroups * i;
			b[i] = 0;
		}
		else {
			sum += restGroups * (i * 2 + 1);
			sum += (rest % i) * (2);
			rest = 0;
		}
	}

	if (endW == -1 && rest > 0) {
		sum += min(rest, n) * 2 - 1;
	}
	else {

		if (rest > 0) {
			int take = min(n - endW - 1, rest);
			sum += take * 2;
			rest -= take;
		}
		if (rest > 0) {
			int take = min(startW, rest);
			sum += take * 2;
		}
	}
	for (int i = 0; i < n; ++i)
		if (s[i] == 'W') {
			sum++;
			if (i && s[i - 1] == 'W')
				sum++;
		}
	cout << sum;
	cout << endl;
}

void prepare()
{
}

int main()
{
#ifdef __OLIMP__
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	int T = 1;
	scanf("%d", &T);
	prepare();
	for (int tc = 1; tc <= T; tc++)
		solve_case();

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include "stdio.h"
using namespace std;
const int N = 1 * 100000 + 2;
int n, m, k;
int b[N], ind[N];
char s[N];
const long long MOD = 1000 * 1000 * 1000 + 7;
void solve_case()
{

	memset(b, 0, sizeof(b));
	memset(ind, 0, sizeof(ind));
	m = 0;
	cin >> n >> k;
	scanf("%s", &s);
	int startW = -1;
	for (int i = 0; i < n; ++i) {
		if (s[i] == 'W') {
			startW = i;
			break;
		}
	}
	int endW = -1;
	for (int i = n-1; i >= 0; --i) {
		if (s[i] == 'W') {
			endW = i;
			break;
		}
	}

	for (int i = startW + 1; i <= endW; ++i) {
		while (s[i] == 'W' && i <= endW) i++;
		int startL = i;
		if (i >= endW) break;
		while (s[i] == 'L' && i <= endW) i++;
		int endL = i-1;
		int ln = endL - startL + 1;
		if (!b[ln])
			ind[m++] = ln;
		b[ln]++;

	}
	sort(ind, ind + m);
	int sum = 0;
	int rest = k;
	for (int j = 0; j < m; j++) {
		int i = ind[j];
		int restGroups = rest / i;
		int hasGroups = b[i];

		if (restGroups >= hasGroups) {
			sum += hasGroups * (i*2 + 1);
			rest -= hasGroups * i;
			b[i] = 0;
		}
		else {
			sum += restGroups * (i * 2 + 1);
			sum += (rest % i) * (2);
			rest = 0;
		}
	}

	if (endW == -1 && rest > 0) {
		sum += min(rest, n) * 2 - 1;
	}
	else {

		if (rest > 0) {
			int take = min(n - endW - 1, rest);
			sum += take * 2;
			rest -= take;
		}
		if (rest > 0) {
			int take = min(startW, rest);
			sum += take * 2;
		}
	}
	for (int i = 0; i < n; ++i)
		if (s[i] == 'W') {
			sum++;
			if (i && s[i - 1] == 'W')
				sum++;
		}
	cout << sum;
	cout << endl;
}

void prepare()
{
}

int main()
{
#ifdef __OLIMP__
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	int T = 1;
	scanf("%d", &T);
	prepare();
	for (int tc = 1; tc <= T; tc++)
		solve_case();

	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#include <stack>
#include <unordered_set>

using namespace std;

int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n, k;
		string s;
		cin >> n >> k >> s;
		int p = 0;
		char prv = 'L';
		for (char c : s)
		{
			if (c == 'W')
			{
				p += 1 + (prv == 'W');
			}
			prv = c;
		}
		vector<int> v;
		int ls = 0;
		for (int i = 0; i < s.size(); i++)
		{
			if (s[i] == 'L')
			{
				int st = i;
				while (s[i] == 'L' && i < s.size())
					i++;
				v.push_back(i - st);
				ls += i - st;
			}
		}
		if (k >= ls)
		{
			cout << s.size() * 2 - 1 << endl;
			continue;
		}
		int fs = s[0] == 'L', bs = s[s.size() - 1] == 'L';
		if (!(fs == 1 && v.size() == 1) && !(v.size() == 0))
			sort(v.begin() + fs, v.end() - bs);
		for (int i = fs; k > 0 && i < v.size() - bs; i++)
		{
			if (k >= v[i])
			{
				p += v[i] * 2 + 1;
				k -= v[i];
			}
			else
			{
				p += k * 2;
				k = 0;
				break;
			}
		}
		p += k * 2;
		cout << p << endl;
	}
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#include <stack>
#include <unordered_set>

using namespace std;

int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n, k;
		string s;
		cin >> n >> k >> s;
		int p = 0;
		char prv = 'L';
		for (char c : s)
		{
			if (c == 'W')
			{
				p += 1 + (prv == 'W');
			}
			prv = c;
		}
		vector<int> v;
		int ls = 0;
		for (int i = 0; i < s.size(); i++)
		{
			if (s[i] == 'L')
			{
				int st = i;
				while (s[i] == 'L' && i < s.size())
					i++;
				v.push_back(i - st);
				ls += i - st;
			}
		}
		if (p == 0)
		{
			cout << min(k, ls) * 2 - 1 << endl;
		}
		if (k >= ls)
		{
			cout << s.size() * 2 - 1 << endl;
			continue;
		}
		int fs = s[0] == 'L', bs = s[s.size() - 1] == 'L';
		if (!(fs == 1 && v.size() == 1) && !(v.size() == 0))
			sort(v.begin() + fs, v.end() - bs);
		for (int i = fs; k > 0 && i < v.size() - bs; i++)
		{
			if (k >= v[i])
			{
				p += v[i] * 2 + 1;
				k -= v[i];
			}
			else
			{
				p += k * 2;
				k = 0;
				break;
			}
		}
		p += k * 2;
		cout << p << endl;
	}
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#include <stack>
#include <unordered_set>

using namespace std;

int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n, k;
		string s;
		cin >> n >> k >> s;
		int p = 0;
		char prv = 'L';
		for (char c : s)
		{
			if (c == 'W')
			{
				p += 1 + (prv == 'W');
			}
			prv = c;
		}
		vector<int> v;
		int ls = 0;
		for (int i = 0; i < s.size(); i++)
		{
			if (s[i] == 'L')
			{
				int st = i;
				while (s[i] == 'L' && i < s.size())
					i++;
				v.push_back(i - st);
				ls += i - st;
			}
		}
		if (p == 0)
		{
			cout << max(min(k, ls) * 2 - 1, 0) << endl;
			continue;
		}
		if (k >= ls)
		{
			cout << s.size() * 2 - 1 << endl;
			continue;
		}
		int fs = s[0] == 'L', bs = s[s.size() - 1] == 'L';
		if (!(fs == 1 && v.size() == 1) && !(v.size() == 0))
			sort(v.begin() + fs, v.end() - bs);
		for (int i = fs; k > 0 && i < v.size() - bs; i++)
		{
			if (k >= v[i])
			{
				p += v[i] * 2 + 1;
				k -= v[i];
			}
			else
			{
				p += k * 2;
				k = 0;
				break;
			}
		}
		p += k * 2;
		cout << p << endl;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
	int t;
	cin >> t;
	for (int i = 0; i < t; i++) {
		int n, k, s = 0;
		cin >> n >> k;
		vector <int> a(n);
		int step = 0;
		vector <int> steps;
		int ans = 0;
		for (int j = 0; j < n; j++){
			char l;
			cin >> l;
			a[j] = 0;
			if (l == 'W') {
				if (j == 0) steps.push_back(step);
				ans++;
				if (j != 0 && a[j - 1] == 1) ans++;
				a[j] = 1;
				s += 1;
				if (step != 0)steps.push_back(step);
				step = 0;
			}
			else step++;
		}
		if (k + s >= n) {
			cout << 2 * n - 1 << "\n";
			continue;
		}
		if (s == 0) {
			cout << 2 * k - 1 << "\n";
			continue;
		}
		reverse(steps.begin(), steps.end());
		steps.pop_back();
		sort(steps.begin(), steps.end());
		int p = 0;
		if (steps.size() == 0) {
			ans += 2 * k;
			k = 0;
		}
		while (k > 0) {
			if (steps[p] <= k) {
				ans++;
				ans += 2 * steps[p];
				k -= steps[p];
				p++;
				if (p == steps.size()) {
					ans += 2 * k;
					break;
				}
				continue;

			}
			ans += k * 2;
			break;
		}
		cout << max(ans, 0) << "\n";
	}
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
	int t;
	cin >> t;
	for (int i = 0; i < t; i++) {
		int n, k, s = 0;
		cin >> n >> k;
		vector <int> a(n);
		int step = 0;
		vector <int> steps;
		int ans = 0;
		for (int j = 0; j < n; j++){
			char l;
			cin >> l;
			a[j] = 0;
			if (l == 'W') {
				ans++;
				if (j != 0 && a[j - 1] == 1) ans++;
				a[j] = 1;
				s += 1;
				steps.push_back(step);
				step = 0;
			}
			else step++;
		}
		if (k + s >= n) {
			cout << 2 * n - 1 << "\n";
			continue;
		}
		if (s == 0) {
			cout << 2 * k - 1 << "\n";
			continue;
		}
		reverse(steps.begin(), steps.end());
		steps.pop_back();
		sort(steps.begin(), steps.end());
		int p = 0;
		if (steps.size() == 0) {
			ans += 2 * k;
			k = 0;
		}
		while (k > 0) {
			ans++;
			if (steps[p] <= k) {
				ans += 2 * steps[p];
				k -= steps[p];
				p++;
				if (p == steps.size()) {
					ans += 2 * k;
					break;
				}
				continue;

			}
			ans += k * 2;
			break;
		}
		cout << ans << "\n";
	}
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
	int t;
	cin >> t;
	for (int i = 0; i < t; i++) {
		int n, k, s = 0;
		cin >> n >> k;
		vector <int> a(n);
		int step = 0;
		vector <int> steps;
		int ans = 0;
		for (int j = 0; j < n; j++){
			char l;
			cin >> l;
			a[j] = 0;
			if (l == 'W') {
				if (j == 0) steps.push_back(step);
				ans++;
				if (j != 0 && a[j - 1] == 1) ans++;
				a[j] = 1;
				s += 1;
				if (step != 0)steps.push_back(step);
				step = 0;
			}
			else step++;
		}
		if (k + s >= n) {
			cout << max(2 * n - 1, 0) << "\n";
			continue;
		}
		if (s == 0) {
			cout << max(2 * k - 1, 0) << "\n";
			continue;
		}
		reverse(steps.begin(), steps.end());
		steps.pop_back();
		sort(steps.begin(), steps.end());
		int p = 0;
		if (steps.size() == 0) {
			ans += 2 * k;
			k = 0;
		}
		while (k > 0) {
			if (steps[p] <= k) {
				ans++;
				ans += 2 * steps[p];
				k -= steps[p];
				p++;
				if (p == steps.size()) {
					ans += 2 * k;
					break;
				}
				continue;

			}
			ans += k * 2;
			break;
		}
		cout << max(ans, 0) << "\n";
	}
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
	int t;
	cin >> t;
	for (int i = 0; i < t; i++) {
		int n, k, s = 0;
		cin >> n >> k;
		vector <int> a(n);
		int step = 0;
		vector <int> steps;
		int ans = 0;
		for (int j = 0; j < n; j++){
			char l;
			cin >> l;
			a[j] = 0;
			if (l == 'W') {
				ans++;
				if (j != 0 && a[j - 1] == 1) ans++;
				a[j] = 1;
				s += 1;
				if (step != 0)steps.push_back(step);
				step = 0;
			}
			else step++;
		}
		if (k + s >= n) {
			cout << 2 * n - 1 << "\n";
			continue;
		}
		if (s == 0) {
			cout << 2 * k - 1 << "\n";
			continue;
		}
		reverse(steps.begin(), steps.end());
		steps.pop_back();
		sort(steps.begin(), steps.end());
		int p = 0;
		if (steps.size() == 0) {
			ans += 2 * k;
			k = 0;
		}
		while (k > 0) {
			ans++;
			if (steps[p] <= k) {
				ans += 2 * steps[p];
				k -= steps[p];
				p++;
				if (p == steps.size()) {
					ans += 2 * k;
					break;
				}
				continue;

			}
			ans += k * 2;
			break;
		}
		cout << max(ans, 0) << "\n";
	}
}


#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif //_MSC_VER

#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <numeric>
#include <cassert>
#include <stdlib.h>
#include <ctime>
#include <functional>
#include <stack>
#include <bitset>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <cstdlib>
#include <cmath>
#include <iterator>
#include <array>
#include <random>
#include <list>
#include <forward_list>
#include <cstdint>
#include <iostream>
#include <iomanip>

using ll = long long;
using pii = std::pair<int, int>;
using vi = std::vector<int>;
using vii = std::vector<pii>;
using std::cout;
using std::cin;
using std::string;
using std::vector;
using std::sort;
//using std::reverse;
//using std::less;
//using std::greater;

#define fi first
#define se second
#define sz(c)  ((int)(c).size())
#define all(c) (c).begin(), (c).end()
#define forn(i,n)  for (int i = 0, nnnn = (n); i < nnnn; ++i)
#define pb  push_back
#define doUnique(c)   (c).erase( ::std::unique( all(c) ), (c).end() )

const int inf = 0x3f3f3f3f;
const int MOD = 1000000007;//998244853;//
int powm(int x, int n) {
	int r = 1;
	while (n) {
		if (n & 1) r = (r * 1LL * x) % MOD;
		x = (x * 1LL * x) % MOD;
		n /= 2;
	}
	return r;
}
int inv(int x) { return powm(x, MOD - 2); }

void solve()
{
	int n, k;
	cin >> n >> k;

	string s;
	cin >> s;

	std::vector<pii> st;
	for (int i = 0; i < n; ++i)
	{
		if (s[i] == 'L') {
			int j = i + 1;
			while (j < n && s[j] == 'L')++j;
			int count = j - i;
			int start = i;
			if (i == 0 || j == n)
				start += n;

			st.push_back(pii(count, start));
			i = j;
		 }
	}

	std::sort(all(st));
	for (auto p : st)
	{
		int start = p.second % n;
		int count = p.first;
		if (start == 0) {
			start += count;
			while (count > 0 && k > 0)
			{
				--k; --count;
				s[--start] = 'W';
			}
		}
		else
		{
			while (count > 0 && k > 0) {
				--count;
				--k;
				s[start++] = 'W';
			}
		}
	}

	int ans = 0;
	forn(i, n) {
		if (s[i] == 'W') {
			if (i > 0 && s[i - 1] == 'W')
				ans += 2;
			else
				ans += 1;
		}
	}

	cout << ans << '\n';

}

int main(int argc, char* argv[])
{

#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif //ONLINE_JUDGE
	::std::ios::sync_with_stdio(false);
	::std::cin.tie(0);
	::std::cout.tie(0);

	int tc = 1;
	std::cin >> tc;
	for (int i= 1; i <= tc; ++i)
	{
		solve();
	}
	return 0;
}


#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif //_MSC_VER

#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <numeric>
#include <cassert>
#include <stdlib.h>
#include <ctime>
#include <functional>
#include <stack>
#include <bitset>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <cstdlib>
#include <cmath>
#include <iterator>
#include <array>
#include <random>
#include <list>
#include <forward_list>
#include <cstdint>
#include <iostream>
#include <iomanip>

using ll = long long;
using pii = std::pair<int, int>;
using vi = std::vector<int>;
using vii = std::vector<pii>;
using std::cout;
using std::cin;
using std::string;
using std::vector;
using std::sort;
//using std::reverse;
//using std::less;
//using std::greater;

#define fi first
#define se second
#define sz(c)  ((int)(c).size())
#define all(c) (c).begin(), (c).end()
#define forn(i,n)  for (int i = 0, nnnn = (n); i < nnnn; ++i)
#define pb  push_back
#define doUnique(c)   (c).erase( ::std::unique( all(c) ), (c).end() )

const int inf = 0x3f3f3f3f;
const int MOD = 1000000007;//998244853;//
int powm(int x, int n) {
	int r = 1;
	while (n) {
		if (n & 1) r = (r * 1LL * x) % MOD;
		x = (x * 1LL * x) % MOD;
		n /= 2;
	}
	return r;
}
int inv(int x) { return powm(x, MOD - 2); }

void solve()
{
	int n, k;
	cin >> n >> k;

	string s;
	cin >> s;

	std::vector<pii> st;
	for (int i = 0; i < n; ++i)
	{
		if (s[i] == 'L') {
			int j = i + 1;
			while (j < n && s[j] == 'L')++j;
			int count = j - i;
			int start = i;
			if (i == 0 || j == n)
				start += n;

			st.push_back(pii(count, start));
			i = j;
		 }
	}

	std::sort(all(st));
	for (auto p : st)
	{
		int start = p.second % n;
		int count = p.first;
		while (count > 0 && k > 0) {
			--count;
			--k;
			s[start++] = 'W';
		}
	}

	int ans = 0;
	forn(i, n) {
		if (s[i] == 'W') {
			if (i > 0 && s[i - 1] == 'W')
				ans += 2;
			else
				ans += 1;
		}
	}

	cout << ans << '\n';

}

int main(int argc, char* argv[])
{

#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif //ONLINE_JUDGE
	::std::ios::sync_with_stdio(false);
	::std::cin.tie(0);
	::std::cout.tie(0);

	int tc = 1;
	std::cin >> tc;
	while (tc--)solve();
	return 0;
}


#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif //_MSC_VER

#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <numeric>
#include <cassert>
#include <stdlib.h>
#include <ctime>
#include <functional>
#include <stack>
#include <bitset>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <cstdlib>
#include <cmath>
#include <iterator>
#include <array>
#include <random>
#include <list>
#include <forward_list>
#include <cstdint>
#include <iostream>
#include <iomanip>

using ll = long long;
using pii = std::pair<int, int>;
using vi = std::vector<int>;
using vii = std::vector<pii>;
using std::cout;
using std::cin;
using std::string;
using std::vector;
using std::sort;
//using std::reverse;
//using std::less;
//using std::greater;

#define fi first
#define se second
#define sz(c)  ((int)(c).size())
#define all(c) (c).begin(), (c).end()
#define forn(i,n)  for (int i = 0, nnnn = (n); i < nnnn; ++i)
#define pb  push_back
#define doUnique(c)   (c).erase( ::std::unique( all(c) ), (c).end() )

const int inf = 0x3f3f3f3f;
const int MOD = 1000000007;//998244853;//
int powm(int x, int n) {
	int r = 1;
	while (n) {
		if (n & 1) r = (r * 1LL * x) % MOD;
		x = (x * 1LL * x) % MOD;
		n /= 2;
	}
	return r;
}
int inv(int x) { return powm(x, MOD - 2); }

void solve()
{
	int n, k;
	cin >> n >> k;

	string s;
	cin >> s;

	std::vector<pii> st;
	for (int i = 0; i < n; ++i)
	{
		if (s[i] == 'L') {
			int j = i + 1;
			while (j < n && s[j] == 'L')++j;
			int count = j - i;
			int start = i;
			if (i > 0 && j < n)
				st.push_back(pii(count, start));

			i = j - 1;
		 }
	}

	std::sort(all(st));
	for (auto p : st)
	{
		int start = p.second;
		int count = p.first;

		if (k < count) break;

		k -= count;
		while (count--)
			s[start++] = 'W';
	}

	for (int i = 0; i < n - 1; ++i) {
		if (s[i] == 'W' && s[i + 1] == 'L') {
			++i;
			while (i < n && s[i] == 'L' && k > 0) {
				s[i++] = 'W'; --k;
			}
			--i;
		}
	}

	for (int i = n - 1; i > 0; --i) {
		if (s[i] == 'W' && s[i - 1] == 'L') {
			--i;
			while (i >= 0 && s[i] == 'L' && k > 0) {
				s[i--] = 'W';
				--k;
			}
			++i;
		}
	}

	for (int i = 0; i < n; ++i) {
		if (s[i] == 'L' && k > 0) {
			s[i] = 'W', --k;
		}
	}

	int ans = 0;
	forn(i, n) {
		if (s[i] == 'W') {
			if (i > 0 && s[i - 1] == 'W')
				ans += 2;
			else
				ans += 1;
		}
	}

	cout << ans << '\n';

}

int main(int argc, char* argv[])
{

#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif //ONLINE_JUDGE
	::std::ios::sync_with_stdio(false);
	::std::cin.tie(0);
	::std::cout.tie(0);

	int tc = 1;
	std::cin >> tc;
	for (int i= 1; i <= tc; ++i)
	{
		solve();
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
	int t, n, k;
	string s;
	cin >> t;
	for (int i = 0; i < t; ++i) {
		cin >> n >> k;
		cin >> s;
		vector <int> v;
		int w = 0;
		if (s[0] == 'L') {
			v.push_back(1);
		}
		else { ++w; }
		for (int j = 1; j < n; ++j) {
			if (s[j] == 'L' && s[j-1] == 'W') {
				v.push_back(1);
			}
			else if (s[j] == 'L') {
				++v.back();
			}
			else {++w;}
		}
		if (s[0] == 'L') {
			v.erase(v.begin());
		}
		if (v.size() > 0 && s[n - 1] == 'L') {
			v.erase(--v.end());
		}
		sort(v.begin(), v.end());
		int sum = 0;
		int a = 0;
		while (a < int(v.size()) && sum + v[a] <= k) {
			sum += v[a];
			++a;
		}
		int c = 2 * (w + k);
		if (w + k > n) { c = 2 * n; }
		if (w + k == 0) { cout << 0 << "\n"; }
		else { cout << c + a - int(v.size()) - 1 << "\n"; };
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

int main() {
	int t, n, k;
	string s;
	cin >> t;
	for (int i = 0; i < t; ++i) {
		cin >> n >> k;
		cin >> s;
		vector <int> v;
		int w = 0;
		if (s[0] == 'L') {
			v.push_back(1);
		}
		else { ++w; }
		for (int j = 1; j < n; ++j) {
			if (s[j] == 'L' && s[j-1] == 'W') {
				v.push_back(1);
			}
			else if (s[j] == 'L') {
				++v.back();
			}
			else {++w;}
		}
		if (s[0] == 'L') {
			v.erase(v.begin());
		}
		if (v.size() > 0 && s[n - 1] == 'L') {
			v.erase(--v.end());
		}
		sort(v.begin(), v.end());
		int sum = 0;
		int a = 0;
		while (a < int(v.size()) && sum + v[a] <= k) {
			sum += v[a];
			++a;
		}
		int c = 2 * (w + k);
		if (w + k > n) { c = 2 * n; }
		if (w + k == 0) { cout << 0 << "\n"; }
		else { cout << c + a - int(v.size()) - 1 << "\n"; };
	}
}


#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		string s;
		deque<int>A;
		vector<int>V;
		int n, k, ans = 0, j = 0, pt = 0;
		cin >> n >> k >> s;
		for (int i = 0; i < n; i++)
			j += s[i] == 'W';
		if (!j)
			cout << max(0, min(n, k) * 2 - 1) << endl;
		else
		{
			for (int i = 0; i < n; i++)
				if (s[i] == 'W')
				{
					if (pt < i)
						A.push_back(i - pt);
					pt = i + 1;
				}
			if (s[0] == 'L')
				A.pop_front();
			for (int x : A)
				V.push_back(x);
			sort(V.begin(), V.end());
			int cnt = V.size(),
				val = min(n, j + k);
			for (int i = 0; i < V.size(); i++)
				if (V[i] <= k)
				{
					k -= V[i];
					cnt--;
				}
			cout << val * 2 - 1 - cnt << endl;
		}
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <stack>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <cmath>
#include <tuple>
#include <random>
#include <bitset>
#include <iomanip>
#include <unordered_map>

#define forn(i, n) for(int i=0; i<n; i++)
#define ll long long
#define ld long double
#define mp make_pair

//#pragma GCC optimize("Ofast,inline,unroll-loops,no-stack-protector")

using namespace std;

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#else
//	freopen("king.in", "r", stdin);
	//freopen("king.out", "w", stdout);
#endif // _DEBUG

	int t;
	cin >> t;
	forn(t_id, t) {
		int n, k;
		cin >> n >> k;
		string s;
		cin >> s;
		vector<int>loses;
		int cnt = 0;
		int wins = 0;
		bool f = 0;
		forn(i, n) {
			if (s[i] == 'W') {
				if (cnt > 0) {
					loses.push_back(cnt);
					cnt = 0;
				}
				wins++;
				f = 1;
			}
			else if(f)cnt++;
		}
		if (wins == 0) {
			cout << max(k * 2 - 1, 0) << '\n';
			continue;
		}
		k = min(k, n - wins);
		int w_cnt = loses.size() + 1;
		if (wins == 0)w_cnt = 0;
		int ans = wins * 2 - w_cnt + k * 2;
		sort(loses.begin(), loses.end());
		for (int i = 0; i < loses.size() && loses[i] <= k; i++, ans++) {
			k -= loses[i];
		}
		cout << ans << '\n';
	}
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
string a;
int b[100005], m, sum;
int n, k;
int p1, p2;

void init() {
	p1 = 0, p2 = 0;
	for(int i = 1; i <= n; ++i)
		if(a[i] == 'W') {
			p1 = i;
			break;
		}
	for(int i = n; i >= 1; --i)
		if(a[i] == 'W') {
			p2 = i;
			break;
		}
	int len = 0;
	for(int i = p1; i <= p2; ++i) {
		if(a[i] == 'L') ++len;
		else if(len) b[++m] = len, len = 0;
	}
	len = 0;
	for(int i = 1; i <= n; ++i) {
		if(a[i] == 'W') {
			if(len) sum += 2;
			else sum += 1, len = 1;
		}
		else len = 0;
	}
	--p1, ++p2;
//	cout<<sum<<'\n';
//	cout<<p1<<' '<<p2<<' '<<m<<'\n';
}

int main() {
	//freopen("1.out", "w", stdout);
	int t;
	cin>>t;
	while(t) {
		--t;
		cin>>n>>k;
		for(int i = 1; i <= n; ++i) cin>>a[i];

		m = 0;
		sum = 0;
		init();

		sort(b + 1, b + m + 1);
		for(int i = 1; i <= m; ++i) {
			if(k - b[i] >= 0) {
				sum += b[i] * 2 + 1;
				k -= b[i];
			}
			else  sum += k * 2, k = 0;
		}
		//cout<<sum<<'\n';
		if(k) {
			if(p1 == -1) if(k) --sum, ++p1;
			if(k - p1 >= 0) {
				sum += p1 * 2;
				k -= p1;
			}
			else sum += k * 2, k = 0;
		//	cout<<sum<<'\n';
			int l = n - p2 + 1;
			if(k - l >= 0)
				sum += l * 2;
			else sum += k * 2;
		}
		printf("%d\n", sum);
	}
}