#include <bits/stdc++.h>
#define ll long long 
using namespace std;

void solve()
{
    int n;
    cin>>n;
    string s;
    cin>>s;
    if(n%3!=0) cout<<"NO"<<endl;
    else
    {
    ll z=0,y=0;
    ll flg=0;
    for(int i=0;i<n;i++)
    {
       if(s[i]=='T')
       {
           z++;
         
       }
       else if (s[i]=='M') 
       {
           y++;
           if(z==0) {flg=1; break;}
           z--;
       }
    }
    ll z1=0,y1=0;
     for(int i=n-1;i>=0;i--)
    {
       if(s[i]=='T')
       {
           z1++;
         
       }
       else if (s[i]=='M') 
       {
           y1++;
           if(z1==0) {flg=1; break;}
           z1--;
       }
    }
    if(flg || y!=z || y1!=z1 ) cout<<"NO"<<endl;
    else cout<<"YES"<<endl;
    
    }
    
   
}
int main()
{
   ll t;
   cin>>t;
   //t=1;
   while(t--)
   {
       solve();
   }
   
   return 0;
}
