#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve(){
int ans = 0, cntp = 0, n, x = 0, c = 0;
cin >> n;
string s;
cin >> s;
s = " " + s;
for(int i = 1; i <= n; i++){
if(s[i] == 'P') cntp++;
else ans += cntp, x += i % 2, c++;
}
int t = abs((c + 1) / 2 - x);
cout << (ans - t) / 2ll + t << '\n';
}
signed main(){
ios :: sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
cin >> t;
while(t--) solve();
return 0;
}