#include<iostream>#include<string>#include<algorithm>#include<cstring>usingnamespace std;int n, k;int hx[26]{};voidsolve(){cin >> n >> k;string s;cin >> s;int ans =0;for(auto& c : s){hx[c -'A']++;if(c =='W'){if(hx['W'-'A']>=3)ans += k;else ans +=1;}else{hx['W'-'A']=0;ans -=1;}}cout << ans <<'\n';memset(hx,0,sizeof hx);}intmain(){ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);int t =1;cin >> t;while(t--)solve();return0;}