The student council has a shared document file. Every day, some members of the student council write the sequence TMT (short for Towa Maji Tenshi) in it.

However, one day, the members somehow entered the sequence into the document at the same time, creating a jumbled mess. Therefore, it is Suguru Doujima's task to figure out whether the document has malfunctioned. Specifically, he is given a string of length $n$ whose characters are all either T or M, and he wants to figure out if it is possible to partition it into some number of disjoint subsequences, all of which are equal to TMT. That is, each character of the string should belong to exactly one of the subsequences.

A string $a$ is a subsequence of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero) characters.


-----Input-----

The first line contains an integer $t$ ($1 \le t \le 5000$)  — the number of test cases.

The first line of each test case contains an integer $n$ ($3 \le n < 10^5$), the number of characters in the string entered in the document. It is guaranteed that $n$ is divisible by $3$.

The second line of each test case contains a string of length $n$ consisting of only the characters T and M.

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.


-----Output-----

For each test case, print a single line containing YES if the described partition exists, and a single line containing NO otherwise.


-----Examples-----

Input
5
3
TMT
3
MTT
6
TMTMTT
6
TMTTTT
6
TTMMTT
Output
YES
NO
YES
NO
YES


-----Note-----

In the first test case, the string itself is already a sequence equal to TMT.

In the third test case, we may partition the string into the subsequences TMTMTT. Both the bolded and the non-bolded subsequences are equal to TMT.

Difficulty: EASY

Solutions:
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	current = 0
	reps = n // 3
	if s.count('T') != 2 * s.count('M') or n % 3 != 0:
		print('NO')
		continue
	for c in s:
		if c == 'T':
			current += 1
		else:
			current -= 1
		if current < 0 or current > reps:
			print('NO')
			break
	else:
		print('YES')


nbTest = int(input())
for k in range(nbTest):
	n = int(input())
	s = input()
	compteur = 0
	sol = 0
	for c in s:
		if c == 'T':
			compteur += 1
			if compteur > n // 3:
				print('NO')
				sol = -1
				break
		else:
			compteur -= 1
			if compteur < 0:
				print('NO')
				sol = -1
				break
	if sol == 0:
		if compteur == n // 3:
			print('YES')
		else:
			print('NO')


def res(s):
	t = 0
	m = 0
	for i in s:
		if i == 'T':
			t += 1
		elif t == 0:
			return False
		else:
			t -= 1
			m += 1
	if t != m:
		return False
	return True

def solve():
	n = int(input())
	first = input()
	if not res(first):
		return 'NO'
	if not res(first[::-1]):
		return 'NO'
	else:
		return 'YES'
for _ in range(int(input())):
	print(solve())


from collections import Counter

def solve(letters):
	counts = Counter(letters)
	if counts['M'] * 2 != counts['T']:
		print('NO')
		return
	n = counts['M']
	(pre, post, m) = ([], [], [])
	for (pos, l) in enumerate(letters):
		if l == 'T':
			if len(pre) < n:
				pre.append(pos)
			else:
				post.append(pos)
		else:
			m.append(pos)
	if len(pre) != len(post) != len(m) != n:
		print('NO')
		return
	for x in range(n):
		if not pre[x] < m[x] < post[x]:
			print('NO')
			return
	print('YES')
for _ in range(int(input())):
	_ = input()
	solve(input())


def check(s):
	c = 0
	for i in s:
		if i == 'T':
			c += 1
		else:
			c -= 1
		if c < 0:
			return False
	c = 0
	s = s[::-1]
	for i in s:
		if i == 'T':
			c += 1
		else:
			c -= 1
		if c < 0:
			return False
	return True
for _ in range(int(input())):
	n = int(input())
	s = input()
	if s.count('T') != 2 * s.count('M'):
		print('NO')
	elif check(s):
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	s = input()
	x = n // 3
	if s.count('T') == 2 * x:
		if s[0] == 'M' or s[n - 1] == 'M':
			print('NO')
		else:
			a = 0
			b = 0
			for i in range(n):
				if s[i] == 'T':
					a += 1
				else:
					if a < 1:
						print('NO')
						break
					a -= 1
					b += 1
			else:
				if a != b:
					print('NO')
				else:
					s = s[::-1]
					a = 0
					b = 0
					for i in range(n):
						if s[i] == 'T':
							a += 1
						elif a < 1:
							print('NO')
							break
						else:
							a -= 1
							b += 1
					else:
						if a == b:
							print('YES')
						else:
							print('NO')
	else:
		print('NO')


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	s = input()[:-1]
	ans = 'YES'
	if s.count('M') != n // 3:
		ans = 'NO'
	else:
		c = 0
		for i in s:
			if i == 'T':
				c += 1
			else:
				c -= 1
			if c < 0:
				ans = 'NO'
				break
		else:
			c = 0
			for i in range(n - 1, -1, -1):
				if s[i] == 'T':
					c += 1
				else:
					c -= 1
				if c < 0:
					ans = 'NO'
					break
	print(ans)


for i in range(int(input())):
	n = int(input())
	s = input()
	l = list(s)
	label = True
	if l.count('T') == 2 * l.count('M'):
		countt = 0
		countm = 0
		for i in l:
			if i == 'T':
				countt += 1
			else:
				countm += 1
			if countt < countm:
				print('NO')
				label = False
				break
		if label:
			l.reverse()
			countt = 0
			countm = 0
			for i in l:
				if i == 'T':
					countt += 1
				else:
					countm += 1
				if countt < countm:
					print('NO')
					label = False
					break
		if label:
			print('YES')
	else:
		print('NO')


import sys
pin = sys.stdin.readline
for T in range(int(pin())):
	N = int(pin())
	S = pin().strip()
	T = M = 0
	F = 1
	for i in range(N):
		if S[i] == 'T':
			T += 1
		elif S[i] == 'M':
			M += 1
		if M > T:
			F = 0
			break
	T = M = 0
	if F:
		for i in range(N - 1, -1, -1):
			if S[i] == 'T':
				T += 1
			elif S[i] == 'M':
				M += 1
			if M > T:
				F = 0
				break
	if 2 * M != T:
		F = 0
	print(['NO', 'YES'][F])


def solve():
	n = int(input())
	(t, m) = ([], [])
	for (i, c) in enumerate(input()):
		if c == 'T':
			t.append(i)
		else:
			m.append(i)
	if len(t) != len(m) * 2:
		return False
	for i in range(n // 3):
		if m[i] < t[i] or m[i] > t[i + n // 3]:
			return False
	return True
t = int(input())
while t:
	s = 'YES' if solve() else 'NO'
	print(s)
	t -= 1


t = int(input())
for j in range(t):
	n = int(input())
	s = input()
	t = 0
	m = 0
	flag = 0
	for i in range(n):
		if s[i] == 'T':
			t += 1
		else:
			m += 1
		if m > t:
			flag = 1
			break
	s = s[::-1]
	m1 = 0
	t1 = 0
	for i in range(n):
		if s[i] == 'T':
			t1 += 1
		else:
			m1 += 1
		if m1 > t1:
			flag = 1
			break
	if flag == 0 and 2 * m == t:
		print('YES')
	else:
		print('NO')


n = int(input())
for i in range(n):
	a = int(input())
	s = input()
	k = 0
	flag = True
	for j in s:
		if j == 'T':
			k = k + 1
		else:
			k = k - 1
		if k < 0 or k > a // 3:
			flag = False
			break
	if flag and k == a - 2 * (a // 3):
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	s = input()
	if n % 3 != 0:
		print('NO')
		continue
	count = 0
	flag = False
	for i in range(n):
		if flag:
			break
		if s[i] == 'M':
			count -= 1
		else:
			count += 1
		if count < 0 or count > n // 3:
			flag = True
			break
	if flag or count != n // 3:
		print('NO')
		continue
	else:
		print('YES')


t = int(input())
for i in range(t):
	n = int(input())
	str1 = input()
	tcnt = 0
	mcnt = 0
	for i in range(n):
		if str1[i] == 'T':
			tcnt += 1
		else:
			mcnt += 1
	if n == 3 and mcnt == 1 and (str1 == 'TMT'):
		print('YES')
	elif n > 3 and mcnt == 1 or str1[0] == 'M' or str1[n - 1] == 'M' or (tcnt - mcnt != mcnt) or (mcnt >= tcnt) or (n % mcnt != 0):
		print('NO')
	else:
		tcnt = 0
		mcnt = 0
		f = 0
		for j in range(n):
			if str1[j] == 'T':
				tcnt += 1
			elif str1[j] == 'M':
				mcnt += 1
				if tcnt < mcnt:
					f = 1
					break
		tcnt = 0
		mcnt = 0
		for j in range(n - 1, -1, -1):
			if str1[j] == 'T':
				tcnt += 1
			elif str1[j] == 'M':
				mcnt += 1
				if tcnt < mcnt:
					f = 1
					break
		if f == 0:
			print('YES')
		else:
			print('NO')


import sys, os.path
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
for _ in range(int(input())):
	n = int(input())
	s = input()
	t = 0
	m = 0
	for i in range(n):
		if s[i] == 'T':
			t += 1
		else:
			m += 1
	flag = True
	if m != n // 3 or t != 2 * n // 3:
		flag = False
	o = 0
	for i in range(n):
		if s[i] == 'M':
			if o < 1:
				flag = False
				break
			else:
				o -= 1
		else:
			o += 1
	o = 0
	for i in range(n - 1, -1, -1):
		if s[i] == 'T':
			o += 1
		elif o < 1:
			flag = False
			break
		else:
			o -= 1
	if flag:
		print('YES')
	else:
		print('NO')


list1 = []
for i in range(int(input())):
	n = int(input())
	s = input()
	s1 = s
	n = True
	if s[0] == s[-1] == 'T' and s.count('T') == s.count('M') * 2:
		while 'TM' in s1:
			s1 = s1.replace('TM', '')
			if s1[0] == 'M':
				n = False
				break
		s1 = s[::-1]
		if n:
			while 'TM' in s1:
				s1 = s1.replace('TM', '')
				if s1[0] == 'M':
					n = False
					break
			if n:
				list1.append('YES')
			else:
				list1.append('NO')
		else:
			list1.append('NO')
	else:
		list1.append('NO')
print(*list1, sep='\n')


t = int(input())
for i in range(t):
	n = int(input())
	k = list(input())
	ts = k.count('T')
	ms = k.count('M')
	flag = 1
	if ts == 2 * ms:
		count = 0
		for i in k:
			if i == 'T':
				count = count + 1
			else:
				count = count - 1
			if count < 0:
				flag = 0
				break
		k.reverse()
		if flag:
			count = 0
			for i in k:
				if i == 'T':
					count = count + 1
				else:
					count = count - 1
				if count < 0:
					flag = 0
					break
		if flag:
			print('YES')
		else:
			print('NO')
	else:
		print('NO')


t = int(input())
for test in range(t):
	n = int(input())
	l = list(map(str, input()))
	c = l.count('T')
	c1 = l.count('M')
	if c // 2 != c1:
		print('NO')
	else:
		a = 0
		b = 0
		d = 0
		for i in l:
			if i == 'T' and a < c // 2 and (d == 0):
				a += 1
			elif i == 'M' and a > b:
				b += 1
			elif i == 'T' and b > 0:
				a -= 1
				b -= 1
				d += 1
		if a == 0 and b == 0:
			print('YES')
		else:
			print('NO')


for _ in range(int(input())):
	n = int(input())
	s = input()
	t = 0
	m = 0
	for c in s:
		if c == 'M':
			m += 1
			if m > t:
				print('NO')
				break
		else:
			t += 1
	else:
		if t == 2 * m:
			m = 0
			t = 0
			for c in s[::-1]:
				if c == 'M':
					m += 1
					if m > t:
						print('NO')
						break
				else:
					t += 1
			else:
				print('YES')
		else:
			print('NO')


def solve(n, s):
	(t, m) = ([], [])
	for i in range(n):
		if s[i] == 'T':
			t.append(i)
		else:
			m.append(i)
	if len(t) != 2 * len(m):
		return False
	for i in range(len(m)):
		if m[i] < t[i] or m[i] > t[i + len(m)]:
			return False
	return True
for __ in range(int(input())):
	if solve(int(input()), input()):
		print('YES')
	else:
		print('NO')


import random
from collections import Counter as cc
from copy import deepcopy as dc
try:
	t = int(input())
	while t:
		n = int(input())
		a = input()
		(cnt, T, M, flag) = (0, 0, 0, 0)
		(cnt1, T1, M1, flag1) = (0, 0, 0, 0)
		for i in range(n):
			if a[i] == 'T':
				T += 1
			elif T == 0:
				flag = 1
				break
			else:
				T -= 1
				cnt += 1
		for i in reversed(range(n)):
			if a[i] == 'T':
				T1 += 1
			elif T1 == 0:
				flag1 = 1
				break
			else:
				T1 -= 1
				cnt1 += 1
		if flag1 == 0 and cnt1 == T1 and (flag == 0) and (cnt == T):
			print('YES')
		else:
			print('NO')
		t -= 1
except EOFError as e:
	pass


t = int(input())
for i in range(t):
	n = int(input())
	s = str(input())
	m = 0
	for x in s:
		if x == 'M':
			m += 1
	if m != n / 3:
		print('NO')
		continue
	cur = 0
	f = True
	for x in s:
		if x == 'T':
			cur += 1
		else:
			cur -= 1
			if cur < 0:
				f = False
				break
	cur = 0
	for x in reversed(s):
		if x == 'T':
			cur += 1
		else:
			cur -= 1
			if cur < 0:
				f = False
				break
	if f:
		print('YES')
	else:
		print('NO')


for s in [*open(0)][2::2]:
	n = r = len(s)
	f = 1
	for x in s[:-1]:
		r -= 6 * (x > 'M') - 3
		f &= 0 < r <= n
	print('YNEOS'[f != r::2])


t = int(input())
for i in range(t):
	n = int(input())
	a = input()
	ct = 0
	y = 1
	for j in a:
		if j == 'T':
			ct = ct + 1
		else:
			ct = ct - 1
		if ct > n // 3 or ct < 0:
			y = 0
	if y == 1 and ct == n // 3:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	t = list()
	m = list()
	c = 1
	for i in range(len(s)):
		if s[i] == 'T':
			t.append(i)
		else:
			m.append(i)
	if len(t) != 2 * len(m):
		c = 0
	else:
		for i in range(len(m)):
			if m[i] < t[i] or m[i] > t[i + len(m)]:
				c = 0
				break
	if c == 0:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	s = list(input())
	t1 = 0
	t2 = 0
	m1 = 0
	m2 = 0
	p = 1
	for i in range(n):
		if s[i] == 'T':
			t1 += 1
		else:
			m1 += 1
			if m1 > t1:
				p = 0
				break
		if s[-1 - i] == 'T':
			t2 += 1
		else:
			m2 += 1
			if m2 > t2:
				p = 0
				break
	if p == 0:
		print('NO')
	else:
		z = s.count('M')
		if z * 3 != n:
			print('NO')
		else:
			print('YES')


from sys import stdin
input = stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	s = input().rstrip()
	ts = []
	ms = []
	for (i, c) in enumerate(s):
		if c == 'T':
			ts.append(i)
		else:
			ms.append(i)
	if len(ts) == len(ms) * 2:
		for i in range(len(ms)):
			if ms[i] < ts[i] or ms[i] > ts[i + len(ms)]:
				break
		else:
			print('YES')
			continue
	print('NO')


from collections import defaultdict, OrderedDict, Counter
from sys import stdin, stdout
from bisect import bisect_left, bisect_right
from queue import Queue, PriorityQueue
from heapq import *
from statistics import *
from math import *
import fractions
import copy
from copy import deepcopy
import sys
import io
sys.setrecursionlimit(10000)
import math
import os
import bisect
import collections
mod = int(pow(10, 9)) + 7
import random
from random import *
from time import time

def ncr(n, r, p=mod):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

def normalncr(n, r):
	r = min(r, n - r)
	count = 1
	for i in range(n - r, n + 1):
		count *= i
	for i in range(1, r + 1):
		count //= i
	return count
inf = float('inf')
adj = defaultdict(set)
visited = defaultdict(int)

def addedge(a, b):
	adj[a].add(b)
	adj[b].add(a)

def bfs(v):
	q = Queue()
	q.put(v)
	visited[v] = 1
	while q.qsize() > 0:
		s = q.get_nowait()
		print(s)
		for i in adj[s]:
			if visited[i] == 0:
				q.put(i)
				visited[i] = 1

def dfs(v, visited):
	if visited[v] == 1:
		return
	visited[v] = 1
	print(v)
	for i in adj[v]:
		dfs(i, visited)

def reverse_bisect_right(a, x, lo=0, hi=None):
	if lo < 0:
		raise ValueError('lo must be non-negative')
	if hi is None:
		hi = len(a)
	while lo < hi:
		mid = (lo + hi) // 2
		if x > a[mid]:
			hi = mid
		else:
			lo = mid + 1
	return lo

def reverse_bisect_left(a, x, lo=0, hi=None):
	if lo < 0:
		raise ValueError('lo must be non-negative')
	if hi is None:
		hi = len(a)
	while lo < hi:
		mid = (lo + hi) // 2
		if x >= a[mid]:
			hi = mid
		else:
			lo = mid + 1
	return lo

def get_list():
	return list(map(int, input().split()))

def get_str_list_in_int():
	return [int(i) for i in list(input())]

def get_str_list():
	return list(input())

def get_map():
	return map(int, input().split())

def input_int():
	return int(input())

def matrix(a, b):
	return [[0 for i in range(b)] for j in range(a)]

def swap(a, b):
	return (b, a)

def find_gcd(l):
	a = l[0]
	for i in range(len(l)):
		a = gcd(a, l[i])
	return a

def is_prime(n):
	sqrta = int(sqrt(n))
	for i in range(2, sqrta + 1):
		if n % i == 0:
			return 0
	return 1

def prime_factors(n):
	while n % 2 == 0:
		return [2] + prime_factors(n // 2)
	sqrta = int(sqrt(n))
	for i in range(3, sqrta + 1, 2):
		if n % i == 0:
			return [i] + prime_factors(n // i)
	return [n]

def p(a):
	if type(a) == str:
		print(a + '\n')
	else:
		print(str(a) + '\n')

def ps(a):
	if type(a) == str:
		print(a)
	else:
		print(str(a))

def kth_no_not_div_by_n(n, k):
	return k + (k - 1) // (n - 1)

def forward_array(l):
	n = len(l)
	stack = []
	forward = [0] * n
	for i in range(len(l) - 1, -1, -1):
		while len(stack) and l[stack[-1]] < l[i]:
			stack.pop()
		if len(stack) == 0:
			forward[i] = len(l)
		else:
			forward[i] = stack[-1]
		stack.append(i)
	return forward

def backward_array(l):
	n = len(l)
	stack = []
	backward = [0] * n
	for i in range(len(l)):
		while len(stack) and l[stack[-1]] < l[i]:
			stack.pop()
		if len(stack) == 0:
			backward[i] = -1
		else:
			backward[i] = stack[-1]
		stack.append(i)
	return backward

def char(a):
	return chr(a + 97)

def get_length(a):
	return 1 + int(log10(a))
nc = 'NO'
yc = 'YES'
ns = 'No'
ys = 'Yes'

def fun(s):
	mc = 0
	tc = 0
	for i in s:
		if i == 'M':
			mc += 1
		else:
			tc += 1
		if tc >= mc:
			continue
		else:
			return 0
	if mc == len(s) / 3:
		return 1
	return 0
for i in range(int(input())):
	n = int(input())
	s = list(input())
	if fun(s) and fun(s[::-1]):
		print(yc)
	else:
		print(nc)


for _ in range(int(input())):
	n = int(input())
	s = input()
	count_T = 0
	count_M = 0
	possible = True
	for i in range(n):
		if s[i] == 'T':
			count_T += 1
		else:
			count_M += 1
		if count_M > count_T:
			possible = False
			break
	if not possible or count_T != 2 * count_M:
		print('NO')
	else:
		count_T = 0
		count_M = 0
		for i in range(n - 1, -1, -1):
			if s[i] == 'T':
				count_T += 1
			else:
				count_M += 1
			if count_M > count_T:
				possible = False
				break
		if not possible:
			print('NO')
		else:
			print('YES')


for _ in range(int(input())):
	n = int(input())
	s = input()
	if n == 3:
		if s == 'TMT':
			print('YES')
		else:
			print('NO')
	else:
		T = 0
		for i in s:
			if i == 'T':
				T += 1
			else:
				T -= 1
			if T < 0 or T > n // 3:
				print('NO')
				break
		else:
			if T == n // 3:
				print('YES')
			else:
				print('NO')


for t in range(int(input())):
	n = int(input())
	a = input()
	M = 0
	T = 0
	for i in range(n):
		if a[i] == 'M':
			M += 1
		else:
			T += 1
		if n // 3 + M < T or M > T:
			print('NO')
			break
	else:
		if M * 2 == T:
			print('YES')
		else:
			print('NO')


def valid(s):
	if s.count('T') != 2 * s.count('M'):
		return 'NO'
	count = 0
	for c in s:
		if c == 'T':
			count += 1
		else:
			count -= 1
		if count > len(s) / 3 or count < 0:
			break
	return 'YES' if count == len(s) / 3 else 'NO'

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		s = input()
		print(valid(s))
main()


def solve():
	n = int(input())
	s = input()
	(tc, btc) = (0, 0)
	if 2 * s.count('M') != s.count('T'):
		return 'NO'
	else:
		for i in range(n):
			if s[i] == 'T':
				tc += 1
			else:
				if tc <= 0:
					return 'NO'
				tc -= 1
		s = s[::-1]
		for i in range(n):
			if s[i] == 'T':
				btc += 1
			else:
				if btc <= 0:
					return 'NO'
				btc -= 1
		if btc == tc == n // 3:
			return 'YES'
	return 'YES'
t = int(input())
for i in range(t):
	print(solve())


def fun(s, n):
	(T, M) = (s.count('T'), s.count('M'))
	if not n % 3 == 0 or not M * 3 == n:
		return 'NO'
	(t, m) = (0, 0)
	for i in s:
		if i == 'T':
			t += 1
		else:
			m += 1
		if m > t or M - m > T - t:
			return 'NO'
	return 'YES'
for _ in range(int(input())):
	n = int(input())
	s = input()
	print(fun(s, n))


for _ in range(int(input())):
	(n, s) = (int(input()), input())
	(k, m, t) = (n // 3, [], [])
	for i in range(n):
		if s[i] == 'M':
			m.append(i)
		else:
			t.append(i)
	if len(m) != k:
		print('NO')
		continue
	for i in range(k):
		if m[i] < t[i] or t[i + k] < m[i]:
			print('NO')
			break
	else:
		print('YES')


import sys
input = sys.stdin.readline
import math
import bisect
from copy import deepcopy as dc
from itertools import accumulate
from collections import Counter, defaultdict, deque

def ceil(U, V):
	return (U + V - 1) // V

def modf1(N, MOD):
	return (N - 1) % MOD + 1
inf = int(1e+20)
mod = int(1000000000.0 + 7)
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()[:-1]
	T = 0
	for i in s:
		if i == 'T':
			T += 1
	if 3 * T != 2 * n:
		print('NO')
		continue
	tt = 0
	mm = 0
	f = True
	for i in s:
		if i == 'T':
			tt += 1
		else:
			mm += 1
		if tt < mm:
			f = False
			break
	tt = 0
	mm = 0
	for i in s[::-1]:
		if i == 'T':
			tt += 1
		else:
			mm += 1
		if tt < mm:
			f = False
			break
	if f:
		print('YES')
	else:
		print('NO')


import sys, os, io

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(sys.stdin.readline())

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math, datetime, functools, itertools, operator, bisect, fractions, statistics
from collections import deque, defaultdict, OrderedDict, Counter
from fractions import Fraction
from decimal import Decimal
from sys import stdout
from heapq import heappush, heappop, heapify, _heapify_max, _heappop_max, nsmallest, nlargest
INF = 999999999999999999999999

class SortedList:

	def __init__(self, iterable=[], _load=200):
		values = sorted(iterable)
		self._len = _len = len(values)
		self._load = _load
		self._lists = _lists = [values[i:i + _load] for i in range(0, _len, _load)]
		self._list_lens = [len(_list) for _list in _lists]
		self._mins = [_list[0] for _list in _lists]
		self._fen_tree = []
		self._rebuild = True

	def _fen_build(self):
		self._fen_tree[:] = self._list_lens
		_fen_tree = self._fen_tree
		for i in range(len(_fen_tree)):
			if i | i + 1 < len(_fen_tree):
				_fen_tree[i | i + 1] += _fen_tree[i]
		self._rebuild = False

	def _fen_update(self, index, value):
		if not self._rebuild:
			_fen_tree = self._fen_tree
			while index < len(_fen_tree):
				_fen_tree[index] += value
				index |= index + 1

	def _fen_query(self, end):
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		x = 0
		while end:
			x += _fen_tree[end - 1]
			end &= end - 1
		return x

	def _fen_findkth(self, k):
		_list_lens = self._list_lens
		if k < _list_lens[0]:
			return (0, k)
		if k >= self._len - _list_lens[-1]:
			return (len(_list_lens) - 1, k + _list_lens[-1] - self._len)
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		idx = -1
		for d in reversed(range(len(_fen_tree).bit_length())):
			right_idx = idx + (1 << d)
			if right_idx < len(_fen_tree) and k >= _fen_tree[right_idx]:
				idx = right_idx
				k -= _fen_tree[idx]
		return (idx + 1, k)

	def _delete(self, pos, idx):
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len -= 1
		self._fen_update(pos, -1)
		del _lists[pos][idx]
		_list_lens[pos] -= 1
		if _list_lens[pos]:
			_mins[pos] = _lists[pos][0]
		else:
			del _lists[pos]
			del _list_lens[pos]
			del _mins[pos]
			self._rebuild = True

	def _loc_left(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(lo, pos) = (-1, len(_lists) - 1)
		while lo + 1 < pos:
			mi = lo + pos >> 1
			if value <= _mins[mi]:
				pos = mi
			else:
				lo = mi
		if pos and value <= _lists[pos - 1][-1]:
			pos -= 1
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value <= _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def _loc_right(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(pos, hi) = (0, len(_lists))
		while pos + 1 < hi:
			mi = pos + hi >> 1
			if value < _mins[mi]:
				hi = mi
			else:
				pos = mi
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value < _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def add(self, value):
		_load = self._load
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len += 1
		if _lists:
			(pos, idx) = self._loc_right(value)
			self._fen_update(pos, 1)
			_list = _lists[pos]
			_list.insert(idx, value)
			_list_lens[pos] += 1
			_mins[pos] = _list[0]
			if _load + _load < len(_list):
				_lists.insert(pos + 1, _list[_load:])
				_list_lens.insert(pos + 1, len(_list) - _load)
				_mins.insert(pos + 1, _list[_load])
				_list_lens[pos] = _load
				del _list[_load:]
				self._rebuild = True
		else:
			_lists.append([value])
			_mins.append(value)
			_list_lens.append(1)
			self._rebuild = True

	def discard(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_right(value)
			if idx and _lists[pos][idx - 1] == value:
				self._delete(pos, idx - 1)

	def remove(self, value):
		_len = self._len
		self.discard(value)
		if _len == self._len:
			raise ValueError('{0!r} not in list'.format(value))

	def pop(self, index=-1):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		value = self._lists[pos][idx]
		self._delete(pos, idx)
		return value

	def bisect_left(self, value):
		(pos, idx) = self._loc_left(value)
		return self._fen_query(pos) + idx

	def bisect_right(self, value):
		(pos, idx) = self._loc_right(value)
		return self._fen_query(pos) + idx

	def count(self, value):
		return self.bisect_right(value) - self.bisect_left(value)

	def __len__(self):
		return self._len

	def __getitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		return self._lists[pos][idx]

	def __delitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		self._delete(pos, idx)

	def __contains__(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_left(value)
			return idx < len(_lists[pos]) and _lists[pos][idx] == value
		return False

	def __iter__(self):
		return (value for _list in self._lists for value in _list)

	def __reversed__(self):
		return (value for _list in reversed(self._lists) for value in reversed(_list))

	def __repr__(self):
		return 'SortedList({0})'.format(list(self))

def main():
	mod = 1000000007
	starttime = datetime.datetime.now()
	if os.path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	tc = ri()
	for _ in range(tc):
		n = ri()
		s = rs()
		fp = 0
		mp = 1
		count = 0
		chosen = {}
		mids = []
		while fp < mp < n:
			while mp < n and s[mp] != 'M':
				mp += 1
			if mp >= n:
				break
			else:
				chosen[mp] = 1
				mids.append(mp)
				while fp < mp and s[fp] != 'T':
					fp += 1
				if fp == mp:
					break
				else:
					chosen[fp] = 1
					count += 1
					mp += 1
					fp += 1
		nc = deque([])
		for i in range(n):
			if i not in chosen:
				nc.append(i)
		if len(nc) != len(mids):
			ws('NO')
		else:
			ans = 'YES'
			for i in mids:
				if nc[0] > i:
					nc.popleft()
				else:
					ans = 'NO'
					break
			ws(ans)
	endtime = datetime.datetime.now()
	time = (endtime - starttime).total_seconds() * 1000
	if os.path.exists('input.txt'):
		print('Time:', time, 'ms')

class FastReader(io.IOBase):
	newlines = 0

	def __init__(self, fd, chunk_size=1024 * 8):
		self._fd = fd
		self._chunk_size = chunk_size
		self.buffer = io.BytesIO()

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self, size=-1):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

class FastWriter(io.IOBase):

	def __init__(self, fd):
		self._fd = fd
		self.buffer = io.BytesIO()
		self.write = self.buffer.write

	def flush(self):
		os.write(self._fd, self.buffer.getvalue())
		(self.buffer.truncate(0), self.buffer.seek(0))

class FastStdin(io.IOBase):

	def __init__(self, fd=0):
		self.buffer = FastReader(fd)
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

class FastStdout(io.IOBase):

	def __init__(self, fd=1):
		self.buffer = FastWriter(fd)
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.flush = self.buffer.flush
sys.stdin = FastStdin()
sys.stdout = FastStdout()
main()


def solve(n, s):
	for i in [-1, 1]:
		chars = {'T': 0, 'M': 0}
		for c in s[::i]:
			chars[c] += 1
			if chars['M'] > chars['T']:
				return 'NO'
		if chars['T'] != 2 * chars['M']:
			return 'NO'
	return 'YES'
T = int(input())
for case in range(T):
	N = int(input())
	S = input()
	res = solve(N, S)
	print(f'{res}')


for _ in range(int(input())):
	n = int(input())
	s = input()
	ms = n // 3
	ts = ms * 2
	if s.count('T') != ts:
		print('NO')
		continue
	start = ms
	middle = 0
	end = 0
	bad = False
	for c in s:
		if c == 'T':
			if start > 0:
				start -= 1
				middle += 1
			elif end > 0:
				end -= 1
			else:
				bad = True
				break
		elif middle > 0:
			middle -= 1
			end += 1
		else:
			bad = True
			break
	if bad:
		print('NO')
	else:
		print('YES')


for i in range(int(input())):
	n = int(input())
	s = input()
	c = n // 3
	(m, t) = (0, 0)
	x = 0
	for i in range(len(s)):
		if s[i] == 'T':
			t += 1
		else:
			t -= 1
		if t < 0 or t > c:
			break
	if t != c:
		print('NO')
	else:
		print('YES')


import sys
from math import ceil, floor, sqrt, sin, pi, cos, tan
from collections import defaultdict

def read_generator():
	while True:
		tokens = sys.stdin.readline().rstrip().split(' ')
		for t in tokens:
			yield t
reader = read_generator()

def read():
	return next(reader)

def readint():
	return int(next(reader))

def readfloat():
	return float(next(reader))

def readline():
	return input()

def readlistint(n):
	return [readint() for _ in range(n)]

def readliststr(n):
	return [read() for _ in range(n)]

def readlistfloat(n):
	return [readfloat() for _ in range(n)]

def zfunc(s):
	z = [0] * len(s)
	l = 0
	r = 0
	for i in range(1, len(s)):
		if i <= r:
			z[i] = min(z[i - l], r - i + 1)
		while i + z[i] < len(s) and s[z[i]] == s[i + z[i]]:
			z[i] += 1
		if r < i + z[i] - 1:
			l = i
			r = i + z[i] - 1
	return z

def solve(a):
	if len(a) % 3 != 0:
		return 'NO'
	o = 0
	c = 0
	mc = 0
	m = len(a) // 3
	for v in a:
		if v == 'T':
			if o < m:
				o += 1
			else:
				c += 1
		else:
			mc += 1
		if o < mc or mc < c:
			return 'NO'
	if c != mc != o:
		return 'NO'
	return 'YES'
tests = readint()
for test in range(1, tests + 1):
	n = readint()
	a = read()
	print(solve(a))


ti = int(input())
while ti > 0:
	ti = ti - 1
	n = int(input())
	s = input()
	m = 0
	t = 0
	c = True
	for i in s:
		if i == 'T':
			t += 1
		else:
			m += 1
			if m > t:
				c = False
	if c == False:
		print('NO')
	else:
		d = True
		if m * 2 != t:
			print('NO')
			continue
		else:
			s = s[::-1]
			t = 0
			m = 0
			for i in s:
				if i == 'T':
					t = t + 1
				else:
					m = m + 1
					if m > t:
						d = False
			if d == False:
				print('NO')
			else:
				print('YES')


def readArr():
	return [int(x) for x in input().split()]

def solve():
	N = int(input())
	seq = list(input())
	ti = []
	mi = []
	cntt = 0
	cntm = 0
	for i in range(N):
		if seq[i] == 'T':
			ti.append(i)
			cntt += 1
		else:
			mi.append(i)
			cntm += 1
	if cntt != 2 * cntm:
		print('NO')
		return
	tf = 0
	tl = len(ti) - 1
	ans = True
	for i in range(cntm):
		if mi[i] < ti[i]:
			ans = False
			break
	if not ans:
		print('NO')
		return
	for i in range(cntm - 1, -1, -1):
		if mi[i] > ti[i + cntm]:
			ans = False
			break
	if ans:
		print('YES')
	else:
		print('NO')
T = int(input())
for t in range(T):
	solve()


T = int(input())
for i in range(T):
	N = int(input())
	str = input()
	(t, m) = (0, 0)
	works = 'YES'
	for j in range(N):
		if str[j] == 'T':
			t += 1
		elif str[j] == 'M':
			m += 1
		if m > t:
			works = 'NO'
			break
	(t, m) = (0, 0)
	for j in range(N - 1, -1, -1):
		if str[j] == 'T':
			t += 1
		elif str[j] == 'M':
			m += 1
		if m > t:
			works = 'NO'
			break
	if t != 2 * m:
		works = 'NO'
	print(works)


for _ in range(int(input())):
	N = int(input())
	S = input()
	ctr = 0
	for i in S:
		if i == 'T':
			ctr += 1
		else:
			ctr -= 1
		if ctr > N // 3 or ctr < 0:
			break
	print('YES' if ctr == N // 3 else 'NO')


question = int(input())
for count in range(question):
	n = int(input())
	a = list(input())
	mm = 0
	t = 0
	tt = 0
	ttt = 0
	flag = True
	for s in a:
		if s == 'T':
			if mm < tt + 1:
				ttt += 1
			else:
				tt += 1
		elif ttt > 0:
			mm += 1
			ttt -= 1
		elif tt > 0:
			mm += 1
			tt -= 1
		else:
			flag = False
			break
	if flag:
		if tt == mm and ttt == 0:
			print('YES')
		else:
			print('NO')
	else:
		print('NO')


from sys import stdin, stdout, maxsize
from math import sqrt, log, factorial as ft, gcd, ceil
from collections import defaultdict as D
from bisect import insort
for _ in range(int(input())):
	n = int(input())
	s = stdin.readline().strip()
	if s[0] == 'M' or s[-1] == 'M':
		print('NO')
	else:
		t = s.count('T')
		m = n - t
		if t == 2 * m:
			bal = 0
			ok = True
			for i in s:
				if i == 'T':
					bal += 1
				else:
					bal -= 1
				if bal < 0:
					ok = False
					break
			if ok:
				bal = 0
				for i in s[::-1]:
					if i == 'T':
						bal += 1
					else:
						bal -= 1
					if bal < 0:
						ok = False
						break
			if ok:
				print('YES')
			else:
				print('NO')
		else:
			print('NO')


for _ in range(int(input())):
	n = int(input())
	s = input()
	if s[0] == 'M' or s[-1] == 'M':
		print('NO')
	else:
		t_count = 0
		m_count = 0
		for i in range(n):
			if s[i] == 'T':
				t_count += 1
			else:
				m_count += 1
		if t_count != 2 * m_count:
			print('NO')
		else:
			t_count = 0
			m_count = 0
			flag = 1
			for i in range(n):
				if s[i] == 'T':
					t_count += 1
				elif t_count == 0:
					flag = 0
					break
				else:
					t_count -= 1
			if flag == 1:
				t_count = 0
				for i in range(n - 1, -1, -1):
					if s[i] == 'T':
						t_count += 1
					elif t_count == 0:
						flag = 0
						break
					else:
						t_count -= 1
			if flag == 1:
				print('YES')
			else:
				print('NO')


t = int(input())
l = 0
while l != t:
	l += 1
	n = int(input())
	s = input()
	if n % 3 != 0 or s[0] != 'T' or s.count('M') * 2 != s.count('T'):
		print('NO')
	else:
		a1 = 0
		a2 = 0
		for i in range(n):
			if s[i] == 'T':
				a1 += 1
			else:
				if a1 < a2 + 1 or 2 * (n // 3) - a1 < n // 3 - a2:
					print('NO')
					break
				a2 += 1
		else:
			print('YES')


for _ in range(int(input())):
	n = int(input())
	(t, m) = ([], [])
	s = input()
	for i in range(n):
		if s[i] == 'T':
			t.append(i)
		else:
			m.append(i)
	if len(t) != 2 * len(m):
		print('NO')
	else:
		nm = len(m)
		for i in range(nm):
			if m[i] < t[i] or m[i] > t[i + nm]:
				print('NO')
				break
		else:
			print('YES')


for _ in range(int(input())):
	n = input()
	s = input()
	s1 = s[::-1]
	(t, m, h) = (0, 0, 0)
	if s.count('T') == 2 * s.count('M'):
		f = 0
		for i in range(len(s)):
			if s[i] == 'T':
				t += 1
			elif s[i] == 'M':
				m += 1
			if m > t:
				h = -1
				f = 1
				break
		if f == 0:
			h = 1
	else:
		h = -1
	(t1, m1, h1) = (0, 0, 0)
	if s1.count('T') == 2 * s1.count('M'):
		f1 = 0
		for i in range(len(s)):
			if s1[i] == 'T':
				t1 += 1
			elif s1[i] == 'M':
				m1 += 1
			if m1 > t1:
				h1 = -1
				f1 = 1
				break
		if f1 == 0:
			h1 = 1
	else:
		h1 = -1
	if h1 == 1 and h == 1:
		print('YES')
	else:
		print('NO')


trails = int(input())
answer_list = []
for x in range(trails):
	k = int(input())
	letters = input()
	num_t = 0
	num_m = 0
	answer = True
	debt = 0
	for d in letters:
		if d == 'T':
			num_t += 1
			if debt > 0:
				debt -= 1
		else:
			num_m += 1
			debt += 1
		if num_m > num_t:
			answer = False
	if num_m != k // 3 or letters[-1] == 'M' or debt > 0:
		answer = False
	if answer == True:
		answer_list.append('YES')
	else:
		answer_list.append('NO')
for g in answer_list:
	print(g)


n = int(input())
for i in range(n):
	x = int(input())
	s = input()
	t = 0
	m = 0
	c = 1
	for j in range(x):
		if s[j] == 'T':
			t += 1
		else:
			m += 1
		if m > t:
			c = 0
			break
	s = s[::-1]
	t = 0
	m = 0
	for j in range(x):
		if s[j] == 'T':
			t += 1
		else:
			m += 1
		if m > t:
			c = 0
			break
	if c == 1 and m == x / 3:
		print('YES')
	else:
		print('NO')


import math
t = int(input())
for i in range(0, t):
	n = map(int, input().split())
	a = input()
	count = 0
	min = 1000
	max = 0
	for j in a:
		if j == 'T':
			count += 1
		else:
			count -= 1
		if count < min:
			min = count
		if count > max:
			max = count
	if (min < 0) | (max > len(a) // 3) | (count != len(a) // 3):
		print('NO')
	else:
		print('YES')


import sys
import math
from math import factorial, inf, gcd
from heapq import *
from functools import *
from itertools import *
from collections import *
from typing import *
sys.setrecursionlimit(10 ** 5)

def rarray():
	return [int(i) for i in input().split()]
ti = 1
ti = int(input())
for ii in range(ti):
	n = int(input())
	a = input()
	c = Counter(a)
	v = n // 3
	if c['M'] != v or c['T'] != v * 2:
		print('NO')
		continue
	t = 0
	m = 0
	f = True
	for i in a:
		if i == 'T':
			t += 1
			if t > v + m:
				f = False
				break
		else:
			m += 1
			if m > t:
				f = False
				break
	print('YES' if f else 'NO')


n = int(input())
for i in range(n):
	str_len = int(input())
	tmt_str = input()
	cou = 0
	for k in tmt_str:
		if k == 'T':
			cou = cou + 1
		else:
			cou = cou - 1
		if cou < 0 or cou > str_len / 3:
			break
	if cou == str_len / 3:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	N = int(input())
	A = input()
	T = A.count('T')
	M = A.count('M')
	if T != 2 * M:
		print('NO')
	else:
		t = 0
		m = 0
		Hai = True
		for i in range(N):
			if A[i] == 'T':
				t += 1
			else:
				m += 1
			if m > t:
				Hai = False
				break
		t = 0
		m = 0
		for i in range(N):
			if A[-i - 1] == 'T':
				t += 1
			else:
				m += 1
			if m > t:
				Hai = False
				break
		if Hai:
			print('YES')
		else:
			print('NO')


def process(S):
	Ts = 0
	Ms = 0
	for c in S:
		if c == 'T':
			Ts += 1
		else:
			Ms += 1
			if Ms > Ts:
				return 'NO'
	if Ts != 2 * Ms:
		return 'NO'
	Ts = 0
	Ms = 0
	for c in S[::-1]:
		if c == 'T':
			Ts += 1
		else:
			Ms += 1
			if Ms > Ts:
				return 'NO'
	return 'YES'
T = int(input())
for I in range(T):
	N = int(input())
	S = input()
	print(process(S))


for t in range(int(input())):
	n = int(input())
	s = input()
	tc = s.count('T')
	mc = s.count('M')
	if tc == 2 * mc:
		if s[0] == 'T' and s[-1] == 'T':
			tc = 0
			mc = 0
			f = 1
			for i in range(len(s)):
				if s[i] == 'T':
					tc = tc + 1
				if s[i] == 'M':
					mc = mc + 1
				if mc > tc:
					print('NO')
					f = 0
					break
			if f == 1:
				tc = 0
				mc = 0
				s = s[::-1]
				for i in range(len(s)):
					if s[i] == 'T':
						tc = tc + 1
					if s[i] == 'M':
						mc = mc + 1
					if mc > tc:
						print('NO')
						f = 0
						break
			if f == 1:
				print('YES')
		else:
			print('NO')
	else:
		print('NO')


def cal(s):
	m = 0
	t = 0
	boo = True
	x = n
	j = 0
	for i in s:
		if i == 'T':
			t += 1
		else:
			m += 1
		if t < m:
			boo = False
	return t == 2 * m and boo
for _ in range(int(input())):
	n = int(input())
	s = input()
	ans = 'YES' if cal(s) and cal(s[::-1]) else 'NO'
	print(ans)


import sys
x = input()
for i in range(int(x)):
	y = int(input())
	z = input()
	if y % 3 != 0 or z.count('M') * 3 != y or z.count('T') / 2 * 3 != y:
		print('NO')
	else:
		c_t = 0
		c_m = 0
		c_t_e = int(2 * y / 3)
		c_m_e = int(y / 3)
		p = 1
		for (ind, i) in enumerate(z):
			if i == 'T':
				c_t += 1
				c_t_e = int(2 * y / 3) - c_t
			else:
				c_m += 1
				c_m_e = int(y / 3) - c_m
			if c_m > c_t:
				p = 0
				print('NO')
				break
			if c_m_e > c_t_e:
				p = 0
				print('NO')
				break
		if p == 1:
			print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	T = 0
	M = 0
	no = False
	for c in a:
		if c == 'T':
			T += 1
		else:
			M += 1
			if T < M:
				no = True
				break
	T = 0
	M = 0
	for c in a[::-1]:
		if c == 'T':
			T += 1
		else:
			M += 1
			if T < M:
				no = True
				break
	if not no:
		if T == 2 * M:
			print('YES')
		else:
			print('NO')
	else:
		print('NO')


def prg():
	t = int(input())
	while t > 0:
		t -= 1
		n = int(input())
		x = input()
		nT = 0
		nM = 0
		for c in x:
			if c == 'T':
				nT += 1
			elif c == 'M':
				nM += 1
			if nT < nM:
				nT = -1
				break
		else:
			nT = 0
			nM = 0
			for c in x[::-1]:
				if c == 'T':
					nT += 1
				elif c == 'M':
					nM += 1
				if nT < nM:
					nT = -1
					break
		if nT == 2 * nM:
			print('YES')
		else:
			print('NO')
prg()


def getInput():
	return sys.stdin.readline().strip()

def getInt():
	return int(getInput())

def getInts():
	return map(int, getInput().split())

def getArray():
	return list(getInts())
for _ in range(int(input())):
	n = int(input())
	st = input()
	flag1 = True
	flag2 = True
	check = 0
	for i in st:
		if i == 'T':
			check += 1
		elif i == 'M':
			check -= 1
		if check < 0:
			flag1 = False
			break
	check = 0
	for i in st[::-1]:
		if i == 'T':
			check += 1
		elif i == 'M':
			check -= 1
		if check < 0:
			flag2 = False
			break
	if flag1 == True and flag2 == True and (st.count('T') == st.count('M') * 2):
		print('YES')
	else:
		print('NO')


def check(li, n):
	t = 0
	m = 0
	for i in range(n):
		if li[i] == 'T':
			t += 1
		else:
			m += 1
		if m > t:
			print('NO')
			return 0
t = int(input())
for i in range(t):
	n = int(input())
	string = input()
	li = []
	for chars in string:
		li.append(chars)
	if len(li) >= 3 and li.count('T') == 2 * li.count('M') and (li[0] != 'M') and (li[n - 1] != 'M'):
		li2 = li[::-1]
		if check(li, n) != 0 and check(li2, n) != 0:
			print('YES')
	else:
		print('NO')


import os
import sys
from io import BytesIO, IOBase

def main():
	for _ in range(int(input())):
		n = int(input())
		s = input()
		low = 0
		high = len(s) - 1
		done = set()
		ct = 0
		cm = 0
		notdone = False
		for x in range(len(s)):
			if s[x] == 'T':
				ct += 1
			if s[x] == 'M':
				if ct > 0:
					ct -= 1
					cm += 1
				else:
					notdone = True
					break
		ct = 0
		for x in range(len(s) - 1, -1, -1):
			if s[x] == 'T':
				ct += 1
			if s[x] == 'M':
				if ct > 0:
					ct -= 1
				else:
					notdone = True
					break
		cntT = s.count('T')
		cntM = s.count('M')
		if cntT == 2 * (n // 3) and cntM == n // 3 and (notdone == False):
			print('YES')
		else:
			print('NO')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


list_ans = []
for _ in range(int(input())):
	n = int(input())
	s = input()
	a1 = a2 = b1 = b2 = 0
	for i in range(n):
		if s[i] == 'T':
			a1 += 1
		else:
			b1 += 1
		if s[-i - 1] == 'T':
			a2 += 1
		else:
			b2 += 1
		if a1 < b1 or a2 < b2:
			list_ans.append('NO')
			break
	else:
		if a1 == 2 * b1:
			list_ans.append('YES')
		else:
			list_ans.append('NO')
for item in list_ans:
	print(item)


for _ in range(int(input())):
	n = int(input())
	l = list(input())
	a = 0
	b = 0
	c = 0
	f = 0
	for i in l:
		if i == 'T':
			if c < b:
				c += 1
			else:
				a += 1
		elif i == 'M':
			if a > b:
				b += 1
			elif a + c > b:
				c -= 1
				b += 1
				a += 1
			else:
				f = 1
				break
		else:
			f = 1
			break
	if f:
		print('NO')
	elif a == b == c:
		print('YES')
	else:
		print('NO')


from time import sleep as sle
from math import *
from random import randint as ri

def gcd(a, b):
	if a == b:
		return a
	elif a > b:
		return gcd(a - b, b)
	else:
		return gcd(b, a)

def pr(x):
	print()
	for s in x:
		print(s)

def solve():
	t = int(input())
	for _ in range(t):
		n = int(input())
		s = input()
		if s.count('T') == 2 * s.count('M'):
			ti = []
			mi = []
			for i in range(n):
				if s[i] == 'T':
					ti += [i]
				else:
					mi += [i]
			k = int(n / 3)
			b = [ti[i] < mi[i] < ti[i + k] for i in range(k)]
			if False in b:
				print('NO')
			else:
				print('YES')
		else:
			print('NO')
solve()


for j in range(int(input())):
	n = int(input())
	s = input()
	co = 0
	for k in s:
		if k == 'T':
			co = co + 1
		else:
			co = co - 1
		if co < 0 or co > n / 3:
			break
	if co == n / 3:
		print('YES')
	else:
		print('NO')


def solve():
	n = int(input())
	s = input()
	dp = [0, 0, 0]
	for c in s:
		if c == 'M':
			dp[1] += 1
			if dp[1] > dp[0]:
				if dp[2] == 0:
					print('NO')
					return
				dp[2] -= 1
				dp[0] += 1
		elif dp[1] > dp[2]:
			dp[2] += 1
		else:
			dp[0] += 1
	if dp[0] == dp[1] and dp[1] == dp[2]:
		print('YES')
		return
	print('NO')
	return
t = int(input())
for i in range(t):
	solve()


for _ in range(int(input())):
	n = int(input())
	s = input()
	t = 0
	m = 0
	flag = False
	for i in range(n):
		if s[i] == 'T':
			t += 1
		elif s[i] == 'M':
			m += 1
		if m > t:
			print('NO')
			flag = True
			break
	if flag:
		continue
	t = 0
	m = 0
	for j in range(n):
		i = n - j - 1
		if s[i] == 'T':
			t += 1
		elif s[i] == 'M':
			m += 1
		if m > t:
			print('NO')
			flag = True
			break
	if flag:
		continue
	if t == 2 * m:
		print('YES')
	else:
		print('NO')


n = int(input())
for g in range(n):
	no = int(input())
	s = input()
	t = []
	m = []
	for i in range(len(s)):
		if s[i] == 'T':
			t.append(i)
		else:
			m.append(i)
	if len(t) != 2 * len(m):
		print('NO')
	else:
		for i in range(len(m)):
			if t[i] > m[i] or m[i] > t[len(m) + i]:
				print('NO')
				break
			elif i == len(m) - 1:
				print('YES')


import sys
import collections
import math
import bisect
import heapq
inf = sys.maxsize

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def input():
	return sys.stdin.readline().strip()
mod = 1000000007
for _ in range(int(input())):
	n = int(input())
	s = list(input())
	t = m = flag = 0
	for i in range(n):
		if s[i] == 'T':
			t += 1
		elif s[i] == 'M':
			if t >= m + 1:
				m += 1
			else:
				flag = 1
				break
	mm = tt = 0
	for i in range(n - 1, -1, -1):
		if s[i] == 'T':
			tt += 1
		elif s[i] == 'M':
			if tt >= mm + 1:
				mm += 1
			else:
				flag = 1
				break
	if t == m * 2 and flag == 0:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	c = 0
	ans = 'YES'
	for i in s:
		if i == 'T':
			c += 1
		else:
			c -= 1
		if c < 0 or c > n // 3:
			ans = 'NO'
			break
	if c != n // 3:
		ans = 'NO'
	print(ans)


import sys
input = lambda : sys.stdin.readline()
int_arr = lambda : list(map(int, input().split()))
str_arr = lambda : list(map(str, input().split()))
get_str = lambda : map(str, input().split())
get_int = lambda : map(int, input().split())
get_flo = lambda : map(float, input().split())
mod = 1000000007

def solve(n, s):
	c = 0
	t = 0
	m = 0
	for i in range(n):
		if s[i] == 'T':
			c += 1
			t += 1
		else:
			c -= 1
			m += 1
			if c < 0:
				print('NO')
				return
	c = 0
	t = 0
	m = 0
	for i in range(n - 1, -1, -1):
		if s[i] == 'T':
			c += 1
			t += 1
		else:
			c -= 1
			m += 1
			if c < 0:
				print('NO')
				return
	if m * 2 == t:
		print('YES')
	else:
		print('NO')
for _ in range(int(input())):
	n = int(input())
	s = str(input())[:-1]
	solve(n, s)


def solve():
	n = int(input())
	s = input()
	m = s.count('M')
	t = s.count('T')
	if m * 2 != t:
		return 'NO'
	else:
		t = 0
		for i in range(n):
			if s[i] == 'T':
				t += 1
			elif t >= 1:
				t -= 1
			else:
				return 'NO'
		t = 0
		for i in range(n - 1, -1, -1):
			if s[i] == 'T':
				t += 1
			elif t >= 1:
				t -= 1
			else:
				return 'NO'
		return 'YES'
for _ in range(int(input())):
	print(solve())


def solvecaso():
	n = int(input())
	tmts = input()
	if n % 3 != 0:
		return 'NO'
	tes = 0
	mrest = n / 3
	mes = 0
	for c in tmts:
		if c == 'T':
			tes += 1
		elif c == 'M':
			mes += 1
			mrest -= 1
		if mes > tes or tes - 2 * mes > mrest:
			return 'NO'
	if tes == 2 * n / 3 and mes == n / 3:
		return 'YES'
	return 'NO'
casos = int(input())
for caso in range(1, casos + 1):
	result = solvecaso()
	print(f'{result}')


def call(s, n):
	if n < 3:
		return 'NO'
	if n == 3 and s == 'TMT':
		return 'YES'
	if n == 3 and s != 'TMT':
		return 'NO'
	if s[0] == 'M' or s[n - 1] == 'M':
		return 'NO'
	t = s.count('T')
	m = s.count('M')
	if t != 2 * m:
		return 'NO'
	c = 0
	for i in range(n):
		if c < 0:
			return 'NO'
		if c > m:
			return 'NO'
		if s[i] == 'T':
			c += 1
		else:
			c -= 1
	if c >= 0 and c <= m:
		return 'YES'
	return 'NO'
for _ in range(int(input())):
	n = int(input())
	s = input()
	print(call(s, n))


t = int(input())
for i in range(0, t):
	n = int(input())
	s = str(input())
	m = s.count('T')
	n = s.count('M')
	y = 0
	if 2 * n != m:
		print('NO')
		continue
	else:
		h = []
		for i in range(0, len(s)):
			if s[i] == 'M':
				h.append(i + 1)
		p = 0
		q = 0
		for i in h:
			p = p + 1
			if i - p < p or n - p + 1 > m + p - i:
				y = 1
				break
	if y == 1:
		print('NO')
	else:
		print('YES')


def TMTDocument(s, n):
	t = []
	m = []
	for i in range(0, n):
		if s[i] == 'T':
			t.append(i)
		else:
			m.append(i)
	if 2 * len(m) != len(t):
		print('NO')
		return
	for i in range(0, len(m)):
		if m[i] < t[i] or m[i] > t[i + len(m)]:
			print('NO')
			return
	print('YES')
	return
tc = int(input())
for _ in range(0, tc):
	n = int(input())
	s = input()
	TMTDocument(s, n)


t = int(input())
for i in range(t):
	n = int(input())
	t = 0
	m = 0
	chc = 0
	c2 = 0
	s = input()
	c = 0
	for i in s:
		if i == 'T':
			c += 1
			t += 1
		else:
			c -= 1
			m += 1
		if c < 0:
			chc = 1
			break
	for i in s[::-1]:
		if i == 'T':
			c2 += 1
		else:
			c2 -= 1
		if c2 < 0:
			chc = 1
			break
	if chc == 0 and 2 * m == t:
		print('YES')
	else:
		print('NO')


for ct in range(int(input())):
	n = int(input())
	s = input()
	count = 0
	for c in s:
		if c == 'T':
			count += 1
		else:
			count -= 1
		if count > n // 3 or count < 0:
			break
	print('YES') if count == n // 3 else print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	tm = input()
	mt = tm[::-1]
	t = 0
	m = 0
	tt = 0
	mm = 0
	fail = 0
	for j in range(n):
		if tm[j] == 'T':
			t += 1
		else:
			m += 1
		if mt[j] == 'T':
			tt += 1
		else:
			mm += 1
		if m > t or mm > tt:
			fail = 1
			break
	if t == 2 * m and fail == 0:
		print('YES')
	else:
		print('NO')


from collections import defaultdict, deque

def solve(n, s, m, t):
	for i in range(n):
		if s[i] == 'T':
			t += 1
		elif s[i] == 'M':
			m += 1
			if t < m:
				return False
	m = t = 0
	for i in range(n - 1, -1, -1):
		if s[i] == 'T':
			t += 1
		elif s[i] == 'M':
			m += 1
			if t < m:
				return False
	return True
for _ in range(int(input())):
	n = int(input())
	s = input()
	d = defaultdict(int)
	for i in range(n):
		d[s[i]] += 1
	if d['T'] != 2 * d['M'] or (s[0] != 'T' or s[-1] != 'T'):
		print('NO')
		continue
	left_to_right = []
	m = t = 0
	ans = solve(n, s, m, t)
	if ans:
		print('YES')
	else:
		print('NO')


def solve(S, n):
	left = right = 0
	for ch in S:
		if ch == 'T':
			left += 1
		else:
			if left == 0:
				return False
			left -= 1
	for i in range(n - 1, -1, -1):
		if S[i] == 'T':
			right += 1
		else:
			if right == 0:
				return False
			right -= 1
	return S.count('M') * 3 == n
T = int(input())
for t in range(T):
	N = int(input())
	S = input()
	res = solve(S, len(S))
	print('YES' if res else 'NO')


t = int(input())
while t:
	t -= 1
	n = int(input())
	arr = input()
	arrm = []
	arrt = []
	for i in range(n):
		if arr[i] == 'M':
			arrm.append(i)
		else:
			arrt.append(i)
	if n % 3 != 0 or len(arrm) != n // 3:
		print('NO')
		continue
	k = n // 3
	t1 = arrt[:k]
	t2 = arrt[k:]
	for i in range(k):
		if t1[i] < arrm[i] < t2[i]:
			pass
		else:
			print('NO')
			break
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	s = str(input())[:n]
	c = 0
	res = 0
	for i in s:
		if i == 'T':
			c += 1
		else:
			c -= 1
		res = n // 3
		if c > res or c < 0:
			break
	if c == res:
		print('YES')
	else:
		print('NO')


def xx(x, n):
	if x[0] == 'T' and x[-1] == 'T':
		i = x.count('T')
		j = x.count('M')
		k = []
		l = []
		for ii in range(n):
			if x[ii] == 'M':
				k.append(ii)
			else:
				l.append(ii)
		if 2 * j == i:
			(a, b, c) = (0, 0, i - 1)
			for ii in range(j):
				if l[a] < k[b]:
					a += 1
					b += 1
				else:
					return 'NO'
			b = j - 1
			for ii in range(j):
				if k[b] < l[c]:
					b -= 1
					c -= 1
				else:
					return 'NO'
			return 'YES'
		return 'NO'
	return 'NO'
t = int(input())
for _ in range(t):
	n = int(input())
	x = input()
	print(xx(x, n))


t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	c = 0
	tf = 1
	for j in s:
		if j == 'T':
			c += 1
		else:
			c -= 1
		if c > n // 3 or c < 0:
			tf = 0
	if tf == 1 and c == n // 3:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	s = input()
	t = s.count('T')
	m = s.count('M')
	count = 0
	if t != 2 * m:
		print('NO')
	elif s[0] == 'M' or s[-1] == 'M':
		print('NO')
	else:
		(cntt, cntt1, cntm, cntm1) = (0, 0, 0, 0)
		aa = []
		bb = []
		flag = False
		for i in range(n):
			if s[i] == 'T':
				cntt += 1
			else:
				cntm += 1
			if cntm > cntt:
				flag = True
				print('NO')
				break
			if s[n - i - 1] == 'T':
				cntt1 += 1
			else:
				cntm1 += 1
			if cntm1 > cntt1:
				flag = True
				print('NO')
				break
		if flag == False:
			print('YES')


nn = int(input())
for _ in range(nn):
	n = int(input())
	str = input()
	cnt = 0
	tt = 0
	for i in str:
		if i == 'T':
			cnt += 1
		else:
			cnt -= 1
		if cnt < 0 or cnt > n // 3:
			break
	if cnt == n // 3:
		print('YES')
	else:
		print('NO')


def f(n, s):
	if n % 3 != 0 or s.count('T') != 2 * n // 3 or s.count('M') != n // 3:
		return 'NO'
	else:
		t = []
		m = []
		l = 0
		for i in range(n):
			if s[i] == 'T':
				t.append(i)
			else:
				m.append(i)
		r = len(m)
		for j in range(len(m)):
			if m[j] > t[l] and m[j] < t[r]:
				l = l + 1
				r = r + 1
			else:
				return 'NO'
		return 'YES'
x = int(input())
m = []
for i in range(x):
	z = int(input())
	y = list(input())
	m.append((z, y))
for j in range(x):
	print(f(m[j][0], m[j][1]))


def check(s):
	ct_t = 0
	ct_m = 0
	for c in s:
		if c == 'T':
			ct_t += 1
		if c == 'M':
			if ct_t == 0:
				return False
			ct_m += 1
			ct_t -= 1
	if ct_t != ct_m:
		return False
	return True

def solve():
	n = int(input())
	s = input()
	if not check(s):
		return 'NO'
	if not check(s[::-1]):
		return 'NO'
	return 'YES'
t = int(input())
for case in range(1, t + 1):
	print(solve())


import math
for _ in range(int(input())):
	n = int(input())
	s = input()
	ans = 0
	cnt = 0
	for i in s:
		if i == 'T':
			cnt += 1
		if i == 'M':
			cnt -= 1
		if cnt > n // 3:
			ans = 1
			break
		if cnt < 0:
			ans = 1
			break
	if ans == 1:
		print('NO')
	elif cnt == n // 3:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	s = input()
	(mcount, tcount) = ([], [])
	for i in range(n):
		if s[i] == 'M':
			mcount.append(i)
		else:
			tcount.append(i)
	if len(tcount) != 2 * len(mcount):
		print('NO')
	else:
		for i in range(len(mcount)):
			if mcount[i] < tcount[i]:
				f = 0
				break
			elif mcount[i] > tcount[i + len(mcount)]:
				f = 0
				break
			else:
				f = 1
		if f:
			print('YES')
		else:
			print('NO')


T = int(input())
for i in range(T):
	n = int(input())
	s = input()
	k = n // 3

	def ok(s):
		x = 0
		for j in s:
			if j == 'T':
				x += 1
			else:
				x -= 1
			if x < 0:
				return False
		return True
	print('YES' if s.count('M') == k and ok(s) and ok(s[::-1]) else 'NO')


t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	t = 0
	m = 0
	log = True
	for el in a:
		if el == 'T':
			t += 1
		else:
			m += 1
			if m > t:
				log = False
				break
	t = 0
	m = 0
	for el in a[::-1]:
		if el == 'T':
			t += 1
		else:
			m += 1
			if m > t:
				log = False
				break
	if log:
		if 2 * m != t:
			print('NO')
		else:
			print('YES')
	else:
		print('NO')


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	s = input().rstrip('\n')
	(T, M) = (0, 0)
	flg = True
	rev = True
	for ss in s:
		if ss == 'T':
			T += 1
		else:
			T -= 1
			M += 1
		if T < 0:
			flg = False
			break
	if flg:
		if not T == M:
			rev = False
	else:
		rev = False
	if not rev:
		print('NO')
		continue
	s = reversed(s)
	(T, M) = (0, 0)
	for ss in s:
		if ss == 'T':
			T += 1
		else:
			T -= 1
			M += 1
		if T < 0:
			flg = False
			break
	if flg:
		if T == M:
			print('YES')
		else:
			print('NO')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	S = input()
	flag = True
	c = 0
	c2 = 0
	for i in range(n):
		if S[i] == 'M':
			c += 1
			if c2 < c:
				flag = False
				break
		else:
			c2 += 1
	c = 0
	c2 = 0
	for i in range(n - 1, -1, -1):
		if S[i] == 'M':
			c += 1
			if c2 < c:
				flag = False
				break
		else:
			c2 += 1
	if flag and c * 2 == c2:
		print('YES')
	else:
		print('NO')


def STR():
	s = str(input())
	return s

def INT():
	i = int(input())
	return i

def INT_ARR():
	arr = list(map(int, input().split()))
	return arr

def STR_ARR():
	arr = input().split(' ')
	return arr

def cout(arg):
	print(arg, end=' ')

def solve():
	n = INT()
	s = STR()
	m = 0
	for i in range(n):
		if s[i] == 'M':
			m += 1
	if m * 2 != n - m:
		print('NO')
		return
	m = 0
	for i in range(n):
		if s[i] == 'T':
			m += 1
		else:
			m -= 1
			if m < 0:
				print('NO')
				return
	m = 0
	for i in range(n - 1, -1, -1):
		if s[i] == 'T':
			m += 1
		else:
			m -= 1
			if m < 0:
				print('NO')
				return
	print('YES')
test_case = INT()
for i in range(test_case):
	solve()


def xyz(s):
	if s.count('T') // 2 != s.count('M'):
		return 'NO'
	else:
		(c1, c2, c3) = (0, 0, 0)
		c4 = s.count('T')
		for j in s:
			if j == 'T' and c4 // 2 > c1 and (c3 == 0):
				c1 += 1
			elif j == 'T' and c2 > 0:
				c1 -= 1
				c2 -= 1
				c3 += 1
			elif j == 'M' and c1 > c2:
				c2 += 1
		if c1 == 0 and c2 == 0:
			return 'YES'
		else:
			return 'NO'
for _ in range(int(input())):
	n = int(input())
	s = input()
	print(xyz(s))


def solve():
	n = int(input())
	s = input()
	m = n // 3
	t_id_first = []
	m_id = []
	t_id_second = []
	for i in range(n):
		if s[i] == 'T':
			t_id_first.append(i) if len(t_id_first) < m else t_id_second.append(i)
		else:
			m_id.append(i)
	if len(t_id_first) != m or len(m_id) != m:
		print('NO')
		return
	for i in range(m):
		if t_id_first[i] > m_id[i] or m_id[i] > t_id_second[i]:
			print('NO')
			return
	print('YES')
for _ in range(int(input())):
	solve()


import sys

def answer(n, s):
	numTs = 0
	numMs = 0
	for i in range(n):
		if s[i] == 'T':
			numTs += 1
		else:
			numMs += 1
		if numMs > numTs:
			return 'NO'
	if numTs != 2 * numMs:
		return 'NO'
	numTs = 0
	numMs = 0
	for i in range(n - 1, -1, -1):
		if s[i] == 'T':
			numTs += 1
		else:
			numMs += 1
		if numMs > numTs:
			return 'NO'
	if numTs != 2 * numMs:
		return 'NO'
	return 'YES'

def main():
	t = int(input())
	while t:
		n = int(sys.stdin.readline())
		s = sys.stdin.readline().rstrip()
		print(answer(n, s))
		t -= 1
	return
main()


t = int(input())
while t:
	n = int(input()) // 3
	i = input()
	result = True
	for s in (i, i[::-1]):
		ts = 0
		for c in s:
			if c == 'T':
				ts += 1
			else:
				ts -= 1
				if ts < 0:
					break
		result = result and ts == n
		if not result:
			break
	print('YES' if result else 'NO')
	t -= 1


for _ in range(int(input())):
	n = int(input())
	a = input()
	ans = True
	m = a.count('M')
	t = a.count('T')
	(countt, countm) = (0, 0)
	if t != 2 * m:
		print('NO')
		continue
	if a[-1] == 'M' or a[0] == 'M':
		print('NO')
		continue
	for i in range(len(a)):
		if a[i] == 'T':
			countt += 1
		if a[i] == 'M':
			countm += 1
			if countm > countt:
				ans = False
	(countt, countm) = (0, 0)
	a = a[::-1]
	for i in range(len(a)):
		if a[i] == 'T':
			countt += 1
		if a[i] == 'M':
			countm += 1
			if countm > countt:
				ans = False
	if not ans:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	s = input()
	T = s.count('T')
	M = s.count('M')
	if s[0] == 'M' or s[n - 1] == 'M' or T > M * 2 or (T < M * 2):
		print('NO')
	else:
		count_m = [0 for i in range(n + 1)]
		for i in range(n - 1, -1, -1):
			if s[i] == 'M':
				count_m[i] = count_m[i + 1] + 1
			else:
				count_m[i] = count_m[i + 1]
		pt = 0
		tm = 0
		flag = False
		for i in range(n):
			if s[i] == 'T':
				pt += 1
				if pt > count_m[i] and tm > 0:
					tm -= 1
					pt -= 1
			elif s[i] == 'M':
				if pt == 0:
					flag = True
					break
				else:
					pt -= 1
					tm += 1
		if flag == True or tm > 0 or pt > 0:
			print('NO')
		else:
			print('YES')


import math

def solve():
	n = int(input())
	s = input()
	t = s.count('T')
	m = s.count('M')
	if t != 2 * m:
		print('NO')
	else:
		t1 = 0
		m1 = 0
		f = 1
		for i in s:
			if i == 'T':
				t1 += 1
			elif i == 'M':
				m1 += 1
				if t1 < m1:
					f = 0
					break
		s = s[::-1]
		t1 = 0
		m1 = 0
		for i in s:
			if i == 'T':
				t1 += 1
			elif i == 'M':
				m1 += 1
				if t1 < m1:
					f = 0
					break
		if f:
			print('YES')
		else:
			print('NO')
t = int(input())
while t > 0:
	t -= 1
	solve()


for _ in range(int(input())):
	n = int(input())
	s = input()
	count = 0
	for c in s:
		if c == 'M':
			count += 1
	if len(s) % 3 != 0 or count != len(s) * 1 / 3:
		print('NO')
	else:
		M = 0
		T = 0
		ans = True
		for i in range(len(s)):
			if s[i] == 'M':
				M += 1
			else:
				T += 1
			if M > T:
				ans = False
				break
		M = 0
		T = 0
		for i in range(len(s) - 1, -1, -1):
			if s[i] == 'M':
				M += 1
			else:
				T += 1
			if M > T:
				ans = False
				break
		if ans:
			print('YES')
		else:
			print('NO')


def subsequences2(seq):
	t = 0
	for s in seq:
		if s == 'T':
			t += 1
		else:
			t -= 1
		if t < 0:
			return False
	t = 0
	for s in reversed(seq):
		if s == 'T':
			t += 1
		else:
			t -= 1
		if t < 0:
			return False
	if t != len(seq) // 3:
		return False
	return True
for _ in range(int(input())):
	input()
	if subsequences2(input()):
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	s = input()
	T = s.count('T')
	M = s.count('M')
	if T == 2 * M:
		t = m = 0
		for i in s:
			if i == 'T':
				t += 1
			else:
				m += 1
			if m > t or M - m > T - t:
				print('NO')
				break
		else:
			print('YES')
	else:
		print('NO')


def solve(n, s):
	f = 1
	if s.count('M') != n // 3:
		f = 0
	t = 0
	for c in s:
		if c == 'T':
			t += 1
		else:
			t -= 1
		if t < 0:
			f = 0
	t = 0
	for c in s[::-1]:
		if c == 'T':
			t += 1
		else:
			t -= 1
		if t < 0:
			f = 0
	print('YES' if f else 'NO')
for _ in range(int(input())):
	n = int(input())
	s = input()
	solve(n, s)


import re
import sys
exit = sys.exit
from bisect import bisect_left as bsl, bisect_right as bsr
from collections import Counter, defaultdict as ddict, deque
from functools import lru_cache
cache = lru_cache(None)
from heapq import *
from itertools import *
from math import inf
from pprint import pprint as pp
enum = enumerate
ri = lambda : int(rln())
ris = lambda : list(map(int, rfs()))
rln = sys.stdin.readline
rl = lambda : rln().rstrip('\n')
rfs = lambda : rln().split()
mod = 1000000007
d4 = [(0, -1), (1, 0), (0, 1), (-1, 0)]
d8 = [(-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1)]

def solve(n, s):
	return check(s) and check(s[::-1])

def check(s):
	t = m = 0
	for c in s:
		if c == 'T':
			t += 1
		else:
			m += 1
		if t < m:
			return 0
	return t == 2 * m
t = ri()
for _ in range(t):
	n = ri()
	s = rl()
	if solve(n, s):
		print('YES')
	else:
		print('NO')


def KARENGEYEHSAVAAL(s, n):
	tee = s.count('T')
	em = s.count('M')
	if em == 0 or tee == 0 or tee / em != 2:
		print('NO')
		return ''
	if s[0] == 'M' or s[n - 1] == 'M':
		print('NO')
		return ''
	ct = 0
	for i in s:
		if i == 'T':
			ct += 1
		else:
			ct -= 1
		if ct < 0 or ct > tee / 2:
			print('NO')
			return ''
	print('YES')
	return ''
for _ in range(int(input())):
	n = int(input())
	s = list(input())
	KARENGEYEHSAVAAL(s, n)


from sys import stdin, stdout
from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import *
from random import *
input = stdin.readline
prin = stdout.write
from random import sample
from collections import Counter, deque
from fractions import *
from math import sqrt, ceil, log2, gcd, cos, pi
mod = 10 ** 9 + 7
mod2 = 998244353

def ps(n):
	cp = 0
	lk = 0
	arr = {}
	while n % 2 == 0:
		n = n // 2
		lk += 1
	for ps in range(3, ceil(sqrt(n)) + 1, 2):
		lk = 0
		cc = 0
		while n % ps == 0:
			n = n // ps
			lk += 1
	if n != 1:
		lk += 1
		return False
	return True

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def factorials(n, r):
	slr = n
	dpr = r
	qlr = 1
	qs = 1
	mod = 10 ** 9 + 7
	for ip in range(n - r + 1, n + 1):
		qlr = qlr * ip % mod
	for ij in range(1, r + 1):
		qs = qs * ij % mod
	ans = qlr * modInverse(qs) % mod
	return ans

def modInverse(b):
	qr = 10 ** 9 + 7
	return pow(b, qr - 2, qr)
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func

def power(arr):
	listrep = arr
	subsets = []
	for i in range(2 ** len(listrep)):
		subset = []
		for k in range(len(listrep)):
			if i & 1 << k:
				subset.append(listrep[k])
		subsets.append(subset)
	return subsets

def pda(n):
	list = []
	su = 0
	for i in range(1, int(sqrt(n) + 1)):
		if n % i == 0:
			if n // i == i:
				list.append(i)
				su += i
			else:
				list.append(n // i)
				list.append(i)
				su += i
				su += n // i
	return su

def dis(xa, ya, xb, yb):
	return sqrt((xa - xb) ** 2 + (ya - yb) ** 2)

def ii():
	return int(input())

def ilist():
	return [int(x) for x in input().strip().split()]

def islist():
	return list(map(str, input().split().rstrip()))

def inp():
	return input().strip()

def google(test, ans):
	return 'Case #' + str(test) + ': ' + str(ans)

def overlap(x1, y1, x2, y2):
	if x2 > y1:
		return y1 - x2
	if y1 > y2:
		return y2 - x2
	return y1 - x2

def dist(x1, y1, x2, y2):
	return sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
t = int(input())
for p in range(t):
	n = ii()
	s = inp().lower()
	n = len(s)
	a = s.count('t')
	b = s.count('m')
	deq = []
	tax = []
	rax = []
	cc = 0
	c0 = 0
	c1 = 0
	if a != 2 * b:
		print('NO')
		continue
	for i in range(n):
		if s[i] == 't':
			c0 += 1
		else:
			c1 += 1
		if c1 > c0:
			cc = 1
			break
	c0 = 0
	c1 = 0
	for i in range(n - 1, -1, -1):
		if s[i] == 't':
			c0 += 1
		else:
			c1 += 1
		if c1 > c0:
			cc = 1
			break
	if cc == 1:
		print('NO')
	else:
		print('YES')


import math
for t in range(int(input())):
	n = int(input())
	s = input()
	fl = 0
	mTot = s.count('M')
	tTot = s.count('T')
	if 2 * mTot == tTot:
		l = 0
		r = tTot
		it = 0
		for k in range(n):
			if s[k] == 'T':
				l += 1
				r -= 1
			else:
				it += 1
				if it > l or r < mTot + 1 - it:
					fl = 1
					break
	else:
		fl = 1
	if fl == 0:
		print('YES')
	else:
		print('NO')


def solve(string, n):
	tList = []
	mList = []
	for i in range(n):
		if string[i] == 'T':
			tList.append(i)
		else:
			mList.append(i)
	if len(tList) != 2 * len(mList):
		return False
	lm = len(mList)
	for i in range(lm):
		if tList[i] > mList[i]:
			return False
		if tList[i + lm] < mList[i]:
			return False
	return True
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	if solve(s, n):
		print('YES')
	else:
		print('NO')


def trouvet(indb, indh):
	if chaine[indb] != 'T' and chaine[indh] != 'T':
		return False
	return True
tests = int(input())
for _ in range(tests):
	n = int(input())
	chaine = input()
	(indext, indexm) = ([], [])
	for (i, c) in enumerate(chaine, 1):
		if c == 'T':
			indext.append(i)
		else:
			indexm.append(i)
	possible = True
	if len(indext) != 2 * len(indexm) or 1 not in indext or n not in indext:
		possible = False
	if possible:
		ind = 0
		for m in indexm:
			if indext[ind] < m:
				ind += 1
			else:
				possible = False
				break
	if possible:
		indt = len(indext) - 1
		for m in indexm[::-1]:
			if indext[indt] > m:
				indt -= 1
			else:
				possible = False
				break
	if possible:
		print('YES')
	else:
		print('NO')


from collections import defaultdict, deque
import sys
from math import gcd
input = sys.stdin.readline
t = int(input().rstrip())
maxn = 2000005
Jc = [0 for i in range(maxn)]
mod = 10 ** 9 + 7
for _ in range(t):
	n = int(input().rstrip())
	s = input().rstrip()
	cnt = defaultdict(int)
	for i in s:
		cnt[i] += 1
	if cnt['T'] != n // 3 * 2:
		print('NO')
		continue
	has = True
	cnt = defaultdict(int)
	for i in s:
		cnt[i] += 1
		if i == 'M':
			if cnt['M'] > cnt['T']:
				has = False
				break
	cnt = defaultdict(int)
	for i in range(len(s) - 1, -1, -1):
		cnt[s[i]] += 1
		if s[i] == 'M':
			if cnt['M'] > cnt['T']:
				has = False
				break
	if has:
		print('YES')
	else:
		print('NO')


import sys
t = int(sys.stdin.readline())
for _ in range(t):
	n = int(sys.stdin.readline())
	data = sys.stdin.readline().rstrip()
	Ts = []
	Ms = []
	flag = 0
	for (idx, item) in enumerate(data):
		if item == 'M':
			Ms.append(idx)
		else:
			Ts.append(idx)
	if data[0] == 'M' or data[-1] == 'M' or len(Ms) * 3 != n:
		print('NO')
		continue
	for i in range(len(Ms)):
		if Ts[i] > Ms[i] or Ms[i] > Ts[i + len(Ms)]:
			flag = 1
			break
	print('NO' if flag else 'YES')


from sys import stdin

def solve(lista, leng):
	ms = []
	mc = 0
	ts = []
	for i in range(leng):
		if lista[i] == 'M':
			ms.append(i)
			mc += 1
		else:
			ts.append(i)
	if mc * 3 != leng:
		return False
	for i in range(mc):
		if ms[i] < ts[i] or ms[i] > ts[i + mc]:
			return False
	return True
tests = int(stdin.readline())
while tests:
	leng = int(stdin.readline())
	lista = list(stdin.readline())[:-1]
	print('YES' if solve(lista, leng) else 'NO')
	tests -= 1


def solve(array, n):
	ct = 0
	cm = 0
	for i in array:
		if i == 'T':
			ct += 1
		else:
			cm += 1
		if cm > ct:
			return 'NO'
	ct = 0
	cm = 0
	for i in array[::-1]:
		if i == 'T':
			ct += 1
		else:
			cm += 1
		if cm > ct:
			return 'NO'
	if ct != 2 * cm:
		return 'NO'
	return 'YES'
t = int(input())
for i in range(t):
	n = int(input())
	array = input()
	ans = solve(array, n)
	print(ans)


for k in range(int(input())):
	n = int(input())
	m = input()
	if m[0] != 'T' or m[n - 1] != 'T':
		print('NO')
	else:
		count = 0
		for i in m:
			if i == 'T':
				count += 1
			else:
				count -= 1
			if count < 0 or count > n // 3:
				break
		if count == n // 3:
			print('YES')
		else:
			print('NO')


import sys, os, io
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	s = list(input().strip())
	tc = s.count('T')
	mc = s.count('M')
	if tc != 2 * mc:
		print('NO')
		continue
	cnt = mc
	currT = 0
	currM = 0
	flag = 0
	for i in s:
		if i == 'T':
			currT += 1
		else:
			currM += 1
		rem = cnt - currM
		if currT - 2 * currM > rem:
			flag = 1
			break
		if currM > currT:
			flag = 1
			break
	if flag:
		print('NO')
	else:
		print('YES')


import collections
import itertools
from functools import reduce
mod = 10 ** 9 + 7

def permutationbysum():
	for _ in range(int(input())):
		(num, l, r, achieve) = map(int, input().split())
		k = r - l + 1
		if k * (k + 1) // 2 <= achieve <= k * (num * 2 + 1 - k) // 2:
			outpos = r % num
			inpos = l - 1
			ans = [0] * num
			for i in range(num, 0, -1):
				if achieve - i > 0 or (achieve - i == 0 and inpos == r - 1):
					achieve -= i
					ans[inpos] = i
					inpos += 1
				else:
					ans[outpos] = i
					outpos += 1
					outpos %= num
			print(*ans)
		else:
			print(-1)

def peaks():
	for _ in range(int(input())):
		(num, peak) = map(int, input().split())
		pos = 1
		rpos = num
		ans = []
		now = 0
		if num == 1:
			if not peak:
				print(1)
			else:
				print(-1)
			continue
		if num == 2:
			if peak:
				print(-1)
			else:
				print(1, 2)
			continue
		added = 0
		while rpos + 1 != pos:
			if not peak:
				while pos <= rpos:
					ans.append(pos)
					pos += 1
				break
			if not now:
				ans.append(pos)
				pos += 1
			else:
				ans.append(rpos)
				rpos -= 1
				if num - added != 1:
					peak -= 1
			added += 1
			now = 1 - now
		if peak:
			print(-1)
			continue
		print(*ans)
import sys
input = sys.stdin.readline

def addone():
	for _ in range(int(input())):
		(num, changes) = input().split()
		l = collections.deque(sorted([int(i) for i in num]))
		perm = collections.deque()
		changes = int(changes)
		cd = 0
		ans = len(num)
		while True:
			nextnum = l.pop()
			while perm and perm[-1] == nextnum:
				l.append(perm.pop())
			c = 10 - nextnum - cd
			changes -= c
			cd += c
			if changes >= 0:
				if not c:
					perm.appendleft(1 - cd)
					l.appendleft(-cd)
				else:
					l.appendleft(1 - cd)
					l.appendleft(-cd)
				ans += 1
			else:
				break
		print(ans % 1000000007)
import math

def andsequences():

	def mapping(num):
		nonlocal mnum
		nonlocal c
		num = int(num)
		if num < mnum:
			mnum = num
			c = 1
		elif num == mnum:
			c += 1
		return num
	mod = 1000000007
	for _ in range(int(input())):
		mnum = float('inf')
		c = 0
		num = int(input())
		l = list(map(mapping, input().split()))
		for i in l:
			if mnum & i != mnum:
				print(0)
				break
		else:
			if c == 1:
				print(0)
			else:
				print(math.factorial(num - 2) * (c - 1) * c % mod)

def numberdigit():
	n = 10 ** 5 * 2
	l = [0] * 11
	mod = 10 ** 9 + 7
	l[0] = l[1] = l[2] = l[3] = l[4] = l[5] = l[6] = l[7] = l[8] = 2
	l[9] = 3
	l[10] = 4
	for i in range(11, n):
		l.append((l[i - 10] + l[i - 9]) % mod)
	for _ in range(int(input())):
		(num, c) = input().split()
		c = int(c)
		ans = 0
		for i in num:
			i = int(i)
			if 10 - i > c:
				ans += 1
			else:
				ans += l[c - (10 - i)]
		print(ans % mod)

def mushroom():
	(people, t1, t2, percent) = map(int, input().split())
	l = []
	percent = 1 - percent * 0.01
	for i in range(1, people + 1):
		(s, s1) = map(int, input().split())
		l.append([i, max(s * t1 * percent + s1 * t2, s1 * t1 * percent + s * t2)])
	l.sort(key=lambda x: x[1], reverse=True)
	for i in l:
		i[1] = '{:.2f}'.format(i[1])
		print(*i)

def escape():
	prins = int(input())
	dra = int(input())
	start = int(input())
	pack = int(input())
	c = int(input())
	speeddiff = dra - prins
	if speeddiff <= 0:
		return 0
	pd = start * prins
	ans = 0
	while pd < c:
		hs = pd / speeddiff
		pd += prins * hs
		if pd >= c:
			break
		time = pd / dra
		time += pack
		pd += prins * time
		ans += 1
	return ans

def perm():

	def high(n, k):
		return k * (2 * n - k + 1) // 2

	def low(k):
		return k * (k + 1) // 2
	for _ in range(int(input())):
		(num, lef, rig, s) = map(int, input().split())
		k = rig - lef + 1
		(rig, lef) = (rig - 1, lef - 1)
		if not high(num, k) >= s >= low(k):
			print(-1)
			continue
		l = [0] * num
		lp = lef
		rp = lef - 1
		for i in range(num, 0, -1):
			if high(i, k) >= s and s - i >= low(k - 1) and k:
				l[lp] = i
				lp += 1
				s -= i
				k -= 1
			else:
				l[rp] = i
				rp -= 1
		if k:
			print(-1)
		else:
			print(*l)

def newcom():
	for _ in range(int(input())):
		(days, price) = map(int, input().split())
		dl = input().split()
		worth = input().split()
		worth.append(0)
		ans = float('inf')
		req = 0
		left = 0
		for i in range(days):
			a = int(dl[i])
			w = int(worth[i])
			ans = min(ans, req + max(0, price - left + a - 1) // a)
			ns = max(0, w - left + a - 1) // a
			req += ns + 1
			left += a * ns - w
		print(ans)

def perfectsq():
	for _ in range(int(input())):
		n = input()
		for i in input().split():
			sq = math.sqrt(int(i))
			if sq != int(sq):
				print('YES')
				break
		else:
			print('NO')

def and0big():
	for _ in range(int(input())):
		(l, k) = map(int, input().split())
		print(l ** k % mod)
import math

def mod1p():
	n = int(input())
	ans = dict()
	p = 1
	for i in range(1, n):
		if math.gcd(i, n) == 1:
			ans[str(i)] = True
			p = p * i % n
	if p == 1:
		print(len(ans))
		print(' '.join(ans.keys()))
	else:
		ans.pop(str(p))
		print(len(ans))
		print(' '.join(ans.keys()))

def shorttask():
	num = 10000100
	l = [-1] * (num + 2)
	s = [-1] * (num + 2)
	l[1] = 1
	for i in range(2, int(math.sqrt(num + 1)) + 2):
		if l[i] == -1:
			l[i] = i
			for x in range(i * i, num + 1, i):
				if l[x] == -1:
					l[x] = i
	s[1] = 1
	for i in range(2, num + 1):
		if l[i] == -1:
			l[i] = i
			s[i] = i + 1
		else:
			i1 = i
			s[i] = 1
			while i1 % l[i] == 0:
				i1 //= l[i]
				s[i] = s[i] * l[i] + 1
			s[i] *= s[i1]
	ans = [-1] * (num + 1)
	for i in range(num, 0, -1):
		if s[i] < num:
			ans[s[i]] = i
	for _ in range(int(input())):
		print(ans[int(input())])

def review():
	for _ in range(int(input())):
		n = int(input())
		ans = 0
		for i in input().split():
			i = int(i)
			if i == 1 or i == 3:
				ans += 1
		print(ans)

def GCDleng():
	po10 = [0] * 11
	po10[1] = 1
	for i in range(2, 11):
		po10[i] = po10[i - 1] * 10
	for _ in range(int(input())):
		(n, n1, res) = map(int, input().split())
		print(po10[n], po10[n1] + po10[res])

def anothercarddeck():
	(n, q) = map(int, input().split())
	l = input().split()
	d = {l[i]: i for i in range(n - 1, -1, -1)}
	ans = []
	for i in input().split():
		now = d[i]
		ans.append(now + 1)
		for key in d:
			if d[key] < now:
				d[key] += 1
		d[i] = 0
	print(*ans)

def mincoststring():
	(n, letters) = map(int, input().split())
	l = [chr(i + 97) for i in range(letters)]
	ans = []
	real = letters - 1
	if not n:
		print(*ans, sep='')
		return
	if n == 1:
		print(*ans, sep='', end='')
		print(l[1 % (real + 1)])
		return
	while n:
		for i in range(len(l)):
			for i1 in range(i, len(l)):
				if i1 != real:
					ans.append(l[i1])
					ans.append(l[i])
					n -= 2
				else:
					ans.append(l[i1])
					n -= 1
				if not n:
					print(*ans, sep='')
					return
				if n == 1:
					print(*ans, sep='', end='')
					print(l[(i1 + 1) % (real + 1)])
					return
	print(*ans)

def mincost2():
	(n, letters) = map(int, input().split())
	l = [chr(i + 97) for i in range(letters)]
	comb = []
	if letters == 1 or n == 1:
		print('a' * n)
		return
	for i in range(letters):
		for i1 in range(i, letters):
			comb.append(l[i1] + l[i])
	lc = len(comb)
	while True:
		for i in range(lc):
			if ord(comb[i][0]) - 97 == letters - 1:
				n -= 1
				print(comb[i][0], end='')
			else:
				n -= 2
				print(comb[i], end='')
			if n == 1:
				pos = ord(comb[i][0]) - 97 + 1
				print(l[pos % letters])
				return
			if not n:
				return

def Tittat():
	for _ in range(int(input())):
		(n, k) = map(int, input().split())
		l = list(map(int, input().split()))
		for i in range(n):
			if not k:
				break
			while k and l[i]:
				l[i] -= 1
				l[-1] += 1
				k -= 1
		print(*l)

def xorq2():
	for _ in range(int(input())):
		n = int(input())
		s = 0
		num = -1
		nc = 0
		c = 0
		for i in input().split():
			i = int(i)
			s ^= i
			c += 1
			if num == -1 and s == i:
				num = s
				s = 0
				nc += c
				c = 0
			if num != -1 and (s == num or not s):
				s = 0
				nc += c
				c = 0
		print(['NO', 'YES'][nc == n])

def xorq2re():
	n = int(input())
	s = input().split()
	xor = 0
	for i in range(n):
		s[i] = int(s[i])
		xor ^= s[i]
	if not xor:
		print('YES')
	else:
		new = 0
		ans = 0
		for i in s:
			new ^= i
			if new == xor:
				new = 0
				ans += 1
		print(['NO', 'YES'][ans > 1])
import sys
sys.setrecursionlimit(2300)

def q3partition():

	def partition():
		if tol % 2:
			return 0
		up = tol
		rec = [[False] * (up + 2) for _ in range(n + 1)]
		rec[0][0] = True
		for i in range(1, n + 1):
			v = l[i - 1]
			for j in range(up + 1):
				if rec[i - 1][j]:
					if v + j <= up:
						rec[i][j + v] = True
					rec[i][j] = True
		return rec[n][tol // 2]

	def ints(x):
		nonlocal tol
		nonlocal gcf
		x = int(x)
		tol += x
		gcf = math.gcd(gcf, x) if gcf != float('inf') else x
		return x
	n = int(input())
	tol = 0
	gcf = float('inf')
	l = list(map(ints, input().split()))
	if partition():
		pos = 1
		for i in l:
			if i // gcf % 2:
				print(1)
				print(pos)
				return
			pos += 1
	else:
		print(0)

def permu(n, r):
	return math.factorial(n) // math.factorial(n - r)

def comb(n, r):
	return math.factorial(n) // math.factorial(r) * math.factorial(n - r)

def calc():
	n = int(input())
	ans = permu(n, n)
	for i in range(1, n + 1):
		ans -= comb(n, i)
	print(ans)

def order():
	for _ in range(int(input())):
		n = int(input())
		odd = []
		even = []
		for i in input().split():
			i = int(i)
			if i % 2:
				odd.append(i)
			else:
				even.append(i)
		print(*odd + even)

def TMTdoc():
	n = int(input())
	l = input().rstrip()
	tts = 0
	tms = 0
	for i in l:
		if i == 'T':
			tts += 1
		else:
			tms += 1
	if tms * 2 == tts:
		nowts = 0
		nowms = 0
		for i in l:
			if i == 'T':
				nowts += 1
			else:
				nowms += 1
				if nowts >= nowms and tms - nowms + 1 <= tts - nowts:
					continue
				return 'NO'
	else:
		return 'NO'
	return 'YES'

def main():
	for _ in range(int(input())):
		print(TMTdoc())
main()


import sys

def solve(n, s):
	(ct, cp) = (0, 0)
	for c in s:
		if c == 'T':
			ct += 1
		else:
			ct -= 1
			cp += 1
			if ct < 0:
				print('NO')
				return
	(ct, cp) = (0, 0)
	for c in s[::-1]:
		if c == 'T':
			ct += 1
		else:
			ct -= 1
			cp += 1
			if ct < 0:
				print('NO')
				return
	if ct == cp:
		print('YES')
	else:
		print('NO')
T = int(input())
for t in range(T):
	n = int(input())
	s = input()
	solve(n, s)


for _ in range(int(input())):
	n = int(input())
	s = input()
	m = 0
	t = 0
	rev_m = 0
	rev_t = 0
	for i in range(len(s)):
		if s[i] == 'M':
			m += 1
		else:
			t += 1
		if s[-i - 1] == 'M':
			rev_m += 1
		else:
			rev_t += 1
		if m > t or rev_m > rev_t:
			print('NO')
			break
	else:
		if 2 * m == t:
			print('YES')
		else:
			print('NO')


from sys import stdin
input = stdin.readline

def solve(s, n):
	a = 0
	b = 0
	for i in range(n):
		if s[i] == 'T':
			a += 1
		else:
			b += 1
	if b * 2 != a:
		return 'NO'
	a = 0
	b = 0
	for i in range(n):
		if s[i] == 'T':
			a += 1
		else:
			b += 1
		if b > a:
			return 'NO'
	a = 0
	b = 0
	for i in range(n):
		if s[n - i - 1] == 'T':
			a += 1
		else:
			b += 1
		if b > a:
			return 'NO'
	return 'YES'
t = int(input())
for _ in range(t):
	n = int(input())
	s = input().strip()
	print(solve(s, n))


def check(s):
	t = 0
	m = 0
	for i in s:
		if i == 'T':
			t += 1
		elif i == 'M':
			t -= 1
			m += 1
		if t < 0:
			return False
	if t == m:
		return True
t = int(input())
for i in range(t):
	input()
	s = input().strip()
	print('YES' if check(s) and check(reversed(s)) else 'NO')


for p in range(int(input())):
	n = int(input())
	s = input()
	(a, b, f) = (s.count('M'), s.count('T'), 0)
	if a == n // 3 and b == 2 * (n // 3):
		(x, y) = (0, 0)
		for i in range(n):
			if s[i] == 'M':
				y += 1
			else:
				x += 1
			if s[i] == 'M':
				if x < y:
					f = 1
					break
		(s, x, y) = (s[::-1], 0, 0)
		for i in range(n):
			if s[i] == 'M':
				y += 1
			else:
				x += 1
			if s[i] == 'M':
				if x < y:
					f = 1
					break
		if f:
			print('NO')
		else:
			print('YES')
	else:
		print('NO')


import sys

def solve(s):
	(t_count, m_count, rev_t_count, rev_m_count) = (0, 0, 0, 0)
	n = len(s)
	for idx in range(n):
		if s[idx] == 'T':
			t_count += 1
		else:
			m_count += 1
		if s[n - 1 - idx] == 'T':
			rev_t_count += 1
		else:
			rev_m_count += 1
		if m_count > t_count or rev_m_count > rev_t_count:
			return False
	if t_count != 2 * m_count:
		return False
	return True
t = int(sys.stdin.readline().strip())
for _ in range(t):
	_ = sys.stdin.readline()
	s = sys.stdin.readline().strip()
	if solve(s):
		print('YES')
	else:
		print('NO')


import os
import sys
from io import BytesIO, IOBase

def main():

	def check(s):
		n = len(s)
		ts = 0
		ms = 0
		for i in range(n):
			if s[i] == 'T':
				ts += 1
			else:
				ms += 1
				if ts < ms:
					return False
		return True
	for _ in range(int(input())):
		n = int(input())
		s = input()
		ms = s.count('M')
		ts = n - ms
		if ts != ms * 2:
			print('NO')
			continue
		if not check(s):
			print('NO')
			continue
		if not check(s[::-1]):
			print('NO')
			continue
		print('YES')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


for s in [*open(0)][2::2]:
	f = 0
	for t in (s[:-1], s[-2::-1]):
		i = c = 0
		for x in t:
			i += 1
			c += x < 'T'
			f |= 2 * c > i
	print('YNEOS'[f or c * 3 != i::2])


import sys
import collections
input = sys.stdin.readline

def main():
	t = int(input())
	for _ in range(t):
		N = int(input())
		S = input().strip()
		c = collections.Counter(S)
		if not (c['M'] == N // 3 and c['T'] == N // 3 * 2):
			print('NO')
			continue
		cc = collections.Counter()
		f = True
		for s in S:
			if s == 'T':
				cc['T'] += 1
				if cc['T'] > N // 3:
					f = False
					break
			elif cc['T'] == 0:
				f = False
				break
			else:
				cc['T'] -= 1
				cc['TM'] += 1
		if f:
			print('YES')
		else:
			print('NO')
main()


import math

def get_inp():
	return list(map(int, input().split()))

def solve():
	n = int(input())
	s = input()
	m = 0
	i = 0
	while i < n:
		if m < 0:
			return 'NO'
		if s[i] == 'T':
			m += 1
		elif s[i] == 'M':
			m -= 1
		i += 1
	if m != int(n / 3):
		return 'NO'
	m = 0
	i = n - 1
	while i >= 0:
		if m < 0:
			return 'NO'
		if s[i] == 'T':
			m += 1
		elif s[i] == 'M':
			m -= 1
		i -= 1
	if m != int(n / 3):
		return 'NO'
	return 'YES'
tc = int(input())
for _ in range(tc):
	print(solve())


def photogen(n, a):
	if a[0] == 'M' or a[-1] == 'M':
		return 'NO'
	if not a.count('M') * 2 == a.count('T'):
		return 'NO'
	pr = 1
	mindex = []
	for i in range(1, n):
		if a[i] == 'M':
			mindex.append(i)
			pr -= 1
			if pr < 0:
				return 'NO'
		else:
			pr += 1
	ss = 1
	for i in range(n - 2, -1, -1):
		if a[i] == 'M':
			ss -= 1
			if ss < 0:
				return 'NO'
		else:
			ss += 1
	return 'YES'
for t in range(int(input())):
	n = int(input())
	a = input()
	print(photogen(n, a))


def check(n, s):
	(T_count, M_count) = (0, 0)
	for i in range(n):
		if s[i] == 'M':
			M_count += 1
		else:
			T_count += 1
		if M_count > T_count:
			print('NO')
			return
	(T_count, M_count) = (0, 0)
	s_reverse = s[::-1]
	for i in range(n):
		if s_reverse[i] == 'M':
			M_count += 1
		else:
			T_count += 1
		if M_count > T_count:
			print('NO')
			return
	if 2 * s.count('M') != s.count('T'):
		print('NO')
		return
	print('YES')
for _ in range(int(input())):
	n = int(input())
	s = input()
	check(n, s)


import sys

def solve():
	N = int(input())
	S = str(input())
	t = 0
	m = 0
	for a in S:
		if a == 'T':
			t += 1
		else:
			m += 1
			if t < m:
				print('NO')
				return
	t = 0
	m = 0
	for a in reversed(S):
		if a == 'T':
			t += 1
		else:
			m += 1
			if t < m:
				print('NO')
				return
	if t == m * 2:
		print('YES')
		return
	print('NO')
	return

def main():
	sys.setrecursionlimit(10000)
	T = int(input())
	for i in range(T):
		solve()
main()


from sys import stdin
from collections import defaultdict
from bisect import *
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	s = list(input().strip())
	t = s.count('T')
	m = s.count('M')
	if t != 2 * m:
		print('NO')
	else:
		f = 1
		st = 0
		sm = 0
		for i in range(n):
			if s[i] == 'T':
				st += 1
			else:
				sm += 1
			if st < sm:
				f = 0
		st = 0
		sm = 0
		for i in range(n - 1, -1, -1):
			if s[i] == 'T':
				st += 1
			else:
				sm += 1
			if st < sm:
				f = 0
		if f:
			print('YES')
		else:
			print('NO')


def LSPtable(pattern):
	n = len(pattern)
	l = [0] * n
	j = 0
	for i in range(1, n):
		while j > 0 and pattern[i] != pattern[j]:
			j = l[j - 1]
		if pattern[i] == pattern[j]:
			l[i] = j + 1
			j += 1
		else:
			l[i] = 0
	return l

def KMPsearch(pattern, string):
	lsp = LSPtable(pattern)
	j = 0
	for i in range(len(string)):
		while j > 0 and string[i] != pattern[j]:
			j = lsp[j - 1]
		if string[i] == pattern[j]:
			j += 1
			if j == len(pattern):
				return i - j + 1
	return -1

def _istrue(s, n, y):
	q = []
	c = 0
	flag = False
	for i in range(n):
		if s[i] == 'T':
			if c > 0 and len(q) > 0 and flag:
				q.pop()
				c -= 1
			else:
				q.append(1)
			if len(q) >= y:
				flag = True
		else:
			c += 1
			if len(q) < c:
				return False
	if len(q) > 0:
		return False
	return True

def main():
	n = int(input())
	s = input()
	y = n // 3
	if _istrue(s, n, y):
		print('YES')
	else:
		print('NO')
t = int(input())
for i in range(t):
	main()


lines = int(input())
for i in range(lines):
	n = int(input())
	a = input()
	if n % 3 != 0:
		print('NO')
	else:
		count = 0
		for l in a:
			if l == 'T':
				count += 1
			else:
				count -= 1
			if count < 0 or count > n / 3:
				break
		if count == n / 3:
			print('YES')
		else:
			print('NO')


for i in range(int(input())):
	n = int(input())
	s = input()
	c = {'T': 0, 'M': 0}
	for j in s:
		c[j] += 1
		if c['M'] > c['T'] or c['T'] - c['M'] > n // 3:
			print('NO')
			break
	else:
		if c['M'] * 2 == c['T']:
			print('YES')
		else:
			print('NO')


def solve(s, n):
	if s[0] == 'M' or s[n - 1] == 'M':
		return False
	tCount = s.count('T')
	mCount = n - tCount
	if tCount != 2 * mCount:
		return False
	runningTCount = runningMCount = 0
	for c in s:
		if c == 'T':
			runningTCount += 1
		else:
			runningMCount += 1
		if runningTCount < runningMCount:
			return False
	runningTCount = runningMCount = 0
	for i in range(n - 1, -1, -1):
		if s[i] == 'T':
			runningTCount += 1
		else:
			runningMCount += 1
		if runningTCount < runningMCount:
			return False
	return True
t = int(input())
for tc in range(t):
	n = int(input())
	s = input()
	result = solve(s, n)
	print('YES' if result else 'NO')


for _ in range(int(input())):
	n = int(input())
	a = input()
	ft = 0
	bt = 0
	t = 0
	m = 0
	f = 0
	for i in range(n):
		if a[i] == 'T':
			t += 1
			ft += 1
		else:
			m += 1
			if ft:
				ft -= 1
			else:
				f = 1
				break
	for i in range(n - 1, -1, -1):
		if a[i] == 'T':
			bt += 1
		elif bt:
			bt -= 1
		else:
			f = 1
			break
	if f:
		print('NO')
	elif t == 2 * m:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	s = input()
	l = list(s)
	ans = 'YES'
	ct = 0
	cm = 0
	if l.count('T') != 2 * l.count('M'):
		print('NO')
		continue
	for i in s:
		if i == 'T':
			ct += 1
		if i == 'M':
			cm += 1
		if ct < cm:
			ans = 'NO'
			break
	else:
		ans = 'YES'
	ct = 0
	cm = 0
	ans1 = 'YES'
	for i in s[::-1]:
		if i == 'T':
			ct += 1
		if i == 'M':
			cm += 1
		if ct < cm:
			ans1 = 'NO'
			break
	else:
		ans1 = 'YES'
	if ans == 'YES' and ans1 == 'YES':
		print('YES')
	else:
		print('NO')


def solve(n, a):
	if a[0] != a[-1] or a[0] == 'M':
		print('NO')
		return
	ct = 0
	cm = 0
	for i in range(n):
		if a[i] == 'T':
			ct += 1
		else:
			cm += 1
		if cm > ct or ct - cm > n // 3:
			print('NO')
			return
	if ct != 2 * cm:
		print('NO')
	else:
		print('YES')
t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	solve(n, a)


import sys

def solve():
	n = int(input())
	str = input()
	sum = 0
	for chr in str:
		if chr == 'T':
			sum = sum + 1
		else:
			sum = sum - 1
		if sum < 0 or sum > n / 3:
			return 'NO'
	if sum == n / 3:
		return 'YES'
	return 'NO'
T = int(input())
for i in range(0, T):
	result = solve()
	print(result)


for _ in range(int(input())):
	n = int(input())
	s = list(str(input()))
	em = 0
	te = 0
	for i in s:
		if i == 'T':
			te += 1
		else:
			em += 1
	if te == 2 * em:
		em = 0
		te = 0
		for i in s:
			if i == 'T':
				te += 1
			else:
				em += 1
			if em > te:
				print('NO')
				break
		else:
			s.reverse()
			em = 0
			te = 0
			for i in s:
				if i == 'T':
					te += 1
				else:
					em += 1
				if em > te:
					print('NO')
					break
			else:
				print('YES')
	else:
		print('NO')


def solve():
	n = int(input())
	s = list(input())
	m = []
	t = []
	if s.count('T') != s.count('M') * 2:
		return False
	for i in range(n):
		if s[i] == 'M':
			m.append(i)
		else:
			t.append(i)
	for i in range(len(m)):
		if m[i] < t[i] or m[i] > t[i + len(m)]:
			return False
	return True
tst = int(input())
for i in range(tst):
	print('YES' if solve() else 'NO')


t = int(input())
for tc in range(t):
	n = int(input())
	inp = input()
	tcnt = []
	mcnt = []
	for x in range(n):
		if inp[x] == 'T':
			tcnt.append(x)
		else:
			mcnt.append(x)
	ans = 0
	if len(tcnt) != 2 * len(mcnt):
		ans = 1
	else:
		for x in range(len(mcnt)):
			if mcnt[x] < tcnt[x] or mcnt[x] > tcnt[x + len(mcnt)]:
				ans = 1
	if ans == 0:
		print('YES')
	else:
		print('NO')


import os
import sys
from io import BytesIO, IOBase
import threading
from bisect import bisect_right
from math import gcd, log
from collections import Counter, defaultdict
from pprint import pprint
from itertools import permutations
from bisect import bisect_right

def main():
	n = int(input())
	s = input()
	bol = True
	mc = s.count('M')
	tc = 0
	m = 0
	for i in range(n):
		if s[i] == 'T':
			if mc:
				mc -= 1
				tc += 1
			else:
				m -= 1
		if s[i] == 'M':
			tc -= 1
			m += 1
		if m < 0 or tc < 0:
			print('NO')
			return
	if m != 0 or tc != 0:
		print('NO')
		return
	print('YES')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	main()


t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	m = n // 3
	flag = 0
	k = 0
	(a, b) = (0, 0)
	i = 0
	while i < n and flag == 0:
		if s[i] == 'T':
			a += 1
		else:
			b += 1
			k += 1
		if b > a:
			flag = 1
		i += 1
	if k != m:
		flag = 1
	k = 0
	(a, b) = (0, 0)
	i = n - 1
	while i >= 0 and flag == 0:
		if s[i] == 'T':
			a += 1
		else:
			b += 1
			k += 1
		if b > a:
			flag = 1
		i -= 1
	if flag == 1:
		print('NO')
	else:
		print('YES')


for i in range(int(input())):
	input()
	s = input()
	q = s.count('M')
	c = int(q == len(s) // 3)
	(t, m) = (0, 0)
	for i in s:
		if i == 'T':
			t += 1
		else:
			m += 1
		if m > t:
			c = 0
			break
	(t, m) = (0, 0)
	for i in s[::-1]:
		if i == 'T':
			t += 1
		else:
			m += 1
		if m > t:
			c = 0
			break
	print('YES' if c else 'NO')


def solve(s):
	t1 = s.count('T')
	t2 = s.count('M')
	if t1 != 2 * t2:
		return False
	count1 = 0
	count2 = 0
	for i in range(len(s)):
		if s[i] == 'M':
			count1 += 1
		else:
			count2 += 1
		if count1 > count2:
			return False
	rcount1 = 0
	rcount2 = 0
	for i in range(len(s) - 1, -1, -1):
		if s[i] == 'M':
			rcount1 += 1
		else:
			rcount2 += 1
		if rcount1 > rcount2:
			return False
	return True
t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	if solve(s):
		print('YES')
	else:
		print('NO')


n_inputs = int(input())

def check_if_valid(strochka):
	n_t = 0
	n_m = 0
	for c in strochka:
		if c == 'T':
			n_t += 1
			continue
		n_m += 1
		if n_m > n_t:
			return False
	if n_t != 2 * n_m:
		return False
	return True
for i in range(n_inputs):
	l = int(input())
	stroka = input()
	if l % 3:
		print('NO')
		continue
	if check_if_valid(stroka) and check_if_valid(reversed(stroka)):
		print('YES')
		continue
	print('NO')