Happy Ladybugs is a board game having the following properties:

The board is represented by a string, $\boldsymbol{b}$, of length $n$. The $i^{\mbox{th}}$ character of the string, $b[i]$, denotes the $i^{\mbox{th}}$ cell of the board.

If $b[i]$ is an underscore (i.e., _), it means the $i^{\mbox{th}}$ cell of the board is empty.
If $b[i]$ is an uppercase English alphabetic letter (ascii[A-Z]), it means the $i^{\mbox{th}}$ cell contains a ladybug of color $b[i]$.
String $\boldsymbol{b}$ will not contain any other characters.
A ladybug is happy only when its left or right adjacent cell (i.e., $b[i\pm1]$) is occupied by another ladybug having the same color.
In a single move, you can move a ladybug from its current position to any empty cell. 

Given the values of $n$ and $\boldsymbol{b}$ for $\mathrm{~g~}$ games of Happy Ladybugs, determine if it's possible to make all the ladybugs happy. For each game, return YES if all the ladybugs can be made happy through some number of moves.  Otherwise, return NO. 

Example 

$b=[YYR\text{_}B\text{_}BR]$    

You can move the rightmost $\mbox{B}$ and $\mbox{R}$ to make $b=[YYRRBB\text{__}]$ and all the ladybugs are happy. Return YES.   

Function Description  

Complete the happyLadybugs function in the editor below.   

happyLadybugs has the following parameters:

string b: the initial positions and colors of the ladybugs   

Returns   

string: either YES or NO   

Input Format

The first line contains an integer $\mathrm{~g~}$, the number of games.  

The next $\mathrm{~g~}$ pairs of lines are in the following format:  

The first line contains an integer $n$, the number of cells on the board.  
The second line contains a string $\boldsymbol{b}$ that describes the $n$ cells of the board.  

Constraints

$1\leq g,n\leq100$  
$b[i]\in\{\_,ascii[A-Z]\}$

Sample Input 0
4
7
RBY_YBR
6
X_Y__X
2
__
6
B_RRBR

Sample Output 0
YES
NO
YES
YES

Explanation 0

The four games of Happy Ladybugs are explained below:

Initial board: 

After the first move: 

After the second move: 

After the third move: 

Now all the ladybugs are happy, so we print YES on a new line.
There is no way to make the ladybug having color Y happy, so we print NO on a new line.
There are no unhappy ladybugs, so we print YES on a new line.
Move the rightmost $\mbox{B}$ and $\mbox{R}$ to form $b=[BBRR\text{__}]$.

Sample Input 1
5
5
AABBC
7
AABBC_C
1
_
10
DD__FQ_QQF
6
AABCBC

Sample Output 1
NO
YES
YES
YES
NO

Source: hackerrank | Difficulty: EASY
Tags: Implementation


Solutions:
import sys
import collections

def all_happy(ladybugs):
	for (i, bug) in enumerate(ladybugs):
		try:
			left = ladybugs[i - 1]
		except IndexError:
			left = None
		try:
			right = ladybugs[i + 1]
		except IndexError:
			right = None
		if bug not in (left, right):
			return False
	return True
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	counter = collections.Counter(b)
	empty_cells = counter['_']
	del counter['_']
	if 1 in counter.values():
		print('NO')
	elif empty_cells == 0 and (not all_happy(b)):
		print('NO')
	else:
		print('YES')


from collections import Counter
for _ in range(int(input())):
	(n, b) = (int(input()), input().strip())
	c = Counter(b)
	if any((c[x] == 1 for x in c if x != '_')):
		print('NO')
	elif '_' not in c and any((b[i - 1] != b[i] and b[i] != b[i + 1] for i in range(1, n - 1))):
		print('NO')
	else:
		print('YES')


color_hash = {}
for _ in range(int(input().strip())):
	color_hash.clear()
	N = int(input().strip())
	(have_empty, in_place) = (False, True)
	Cells = input().strip()
	for (idx, ch) in enumerate(Cells):
		if ch == '_':
			have_empty = True
		else:
			color_hash[ch] = color_hash.get(ch, 0) + 1
		if (idx - 1 >= 0 and Cells[idx] != Cells[idx - 1]) and (idx + 1 < len(Cells) and Cells[idx] != Cells[idx + 1]):
			in_place = False
	for color_n in color_hash.values():
		if color_n <= 1:
			break
	else:
		print('YES' if have_empty or in_place else 'NO')
		continue
	print('NO')


def ordindex(c):
	return ord(c) - ord('A')
T = int(input())
for t in range(T):
	input()
	C = [0 for i in range(26)]
	underscore = False
	alreadyhappy = True
	S = input()
	for i in range(len(S)):
		if S[i] == '_':
			underscore = True
		else:
			C[ordindex(S[i])] += 1
			if (i == 0 or S[i - 1] != S[i]) and (i == len(S) - 1 or S[i + 1] != S[i]):
				alreadyhappy = False
	if alreadyhappy:
		print('YES')
	elif underscore:
		for i in C:
			if i == 1:
				print('NO')
				break
		else:
			print('YES')
	else:
		print('NO')


def happy(s):
	if len(s) == 1:
		return False
	if s[0] != s[1]:
		return False
	if s[-1] != s[-2]:
		return False
	for i in range(1, len(s) - 1):
		if s[i] != s[i - 1] and s[i] != s[i + 1]:
			return False
	return True
for _ in range(int(input())):
	n = int(input())
	C = input()
	Alpha = [0] * 26
	for c in C:
		if c != '_':
			Alpha[ord(c) - ord('A')] += 1
	single = False
	for count in Alpha:
		if count == 1:
			single = True
			break
	if single:
		print('NO')
	elif '_' in C:
		print('YES')
	elif happy(C):
		print('YES')
	else:
		print('NO')


import sys
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	dc = {x: b.count(x) for x in set(b)}
	cnt_ = 0
	if '_' in dc.keys():
		cnt_ = dc['_']
		del dc['_']
	if 1 in dc.values():
		print('NO')
	elif cnt_:
		print('YES')
	else:
		res = 'YES'
		for i in range(1, n - 1):
			if b[i - 1] != b[i] and b[i + 1] != b[i]:
				res = 'NO'
		print(res)


def single_bug_exists(line):
	d = {}
	for ch in line:
		if ch == '_':
			continue
		elif ch in d:
			d[ch] += 1
		else:
			d[ch] = 1
	for (k, v) in d.items():
		if v == 1:
			return True
	return False

def has_empty_cell(line):
	for ch in line:
		if ch == '_':
			return True
	return False

def all_bugs_happy(line):
	for i in range(1, len(line) - 1):
		if line[i] != line[i - 1] and line[i] != line[i + 1]:
			return False
	return True
tt = int(input().strip())
for t in range(tt):
	n = input()
	line = input().strip()
	if single_bug_exists(line):
		print('NO')
	elif has_empty_cell(line):
		print('YES')
	elif all_bugs_happy(line):
		print('YES')
	else:
		print('NO')


import sys
import re
from collections import Counter

def check(b):
	if re.match('^(?:([A-Z])\\1+)+$', b):
		return True
	if not '_' in b:
		return False
	bc = Counter(b)
	for (k, v) in bc.items():
		if k != '_':
			if v == 1:
				return False
	return True
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	if check(b):
		print('YES')
	else:
		print('NO')


import sys
from collections import Counter
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	counts = Counter(b)
	flag = False
	f = False
	cntS = 0
	for (k, v) in counts.items():
		if k != '_' and v == 1:
			print('NO')
			flag = True
			break
		elif k == '_':
			cntS += 1
	if cntS == n and flag == False:
		print('YES')
		flag = True
	elif flag == False:
		if cntS == 0:
			i = 0
			while i < n - 1:
				if b[i] == b[i + 1]:
					while i < n - 1 and b[i] == b[i + 1]:
						i += 1
				else:
					print('NO')
					f = True
					break
				i += 1
	if flag == False and f == False:
		print('YES')


import sys
from itertools import groupby
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = list(input().strip())
	result = []
	uniq_b = set(b)
	if '_' not in uniq_b:
		for (x, y) in groupby(b):
			if len(list(y)) >= 2:
				result.append(True)
			else:
				result.append(False)
	else:
		for elm in uniq_b:
			if elm is not '_' and b.count(elm) > 1:
				result.append(True)
			elif elm is not '_' and b.count(elm) == 1:
				result.append(False)
	if False in result:
		print('NO')
	else:
		print('YES')


import sys
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	if n == 1:
		if b[0] == '_':
			print('YES')
		else:
			print('NO')
		continue
	if '_' in b:
		flag = True
		d = dict(((letter, b.count(letter)) for letter in set(b)))
		if d['_'] == n:
			print('YES')
			continue
		d.pop('_')
		for i in d:
			if d[i] < 2:
				flag = False
				break
		if flag:
			print('YES')
		else:
			print('NO')
	else:
		flag = True
		for i in range(n):
			if i > 0 and i != n - 1 and (b[i] != b[i + 1] and b[i] != b[i - 1]):
				flag = False
				break
			elif i == 0 and b[i] != b[i + 1]:
				flag = False
				break
			elif i == n - 1 and b[i] != b[i - 1]:
				flag = False
				break
		if flag:
			print('YES')
		else:
			print('NO')


import sys

def solve(s):
	if n == 1:
		if s == '_':
			return 'YES'
		return 'NO'
	a = [0 for _ in range(26)]
	happy = 'YES'
	for (i, c) in enumerate(s):
		if c != '_':
			a[chars.index(c)] += 1
			if i == 0 and c != s[1]:
				happy = 'NO'
			if i == n - 1 and c != s[n - 2]:
				happy = 'NO'
			if i > 0 and i < n - 1 and (c != s[i - 1]) and (c != s[i + 1]):
				happy = 'NO'
	if happy == 'YES':
		return happy
	if sum(a) == n:
		return happy
	for i in a:
		if i == 1:
			return happy
	return 'YES'
g = int(input().strip())
chars = list(map(chr, range(ord('A'), ord('Z') + 1)))
for _ in range(g):
	n = int(input().strip())
	b = input().strip()
	result = solve(b)
	print(result)


from collections import Counter
from itertools import groupby
for _ in range(int(input())):
	input()
	b = input()
	if '_' in b:
		B = Counter(b)
		del B['_']
		print('NO' if 1 in B.values() else 'YES')
	else:
		for g in groupby(b):
			if len(list(g[1])) == 1:
				print('NO')
				break
		else:
			print('YES')


import sys
Q = int(input().strip())
for a0 in range(Q):
	l = [0] * 26
	n = int(input().strip())
	b = input().strip()
	underscore = 0
	c = 0
	d = 0
	for i in range(n):
		if b[i] == '_':
			underscore = 1
		else:
			l[ord(b[i]) - 65] += 1
	if underscore == 1:
		for i in l:
			if i == 1:
				c = 1
				print('NO')
				break
		if c == 0:
			print('YES')
	else:
		for i in l:
			if i == 1:
				c = 1
				print('NO')
				break
		if c == 0:
			for i in range(1, n - 1):
				if b[i] != b[i + 1] and b[i] != b[i - 1]:
					print('NO')
					d = 1
					break
			if d == 0 and b[0] == b[1] and (b[n - 1] == b[n - 2]):
				print('YES')
			elif d == 0:
				print('NO')


import string
import sys

def happiness(b, freq):
	try:
		happy = (b[0] == b[1] or b[0] == '_') and (b[-1] == b[-2] or b[-1] == '_')
	except:
		if b[0] == '_':
			return True
		else:
			return False
	freq[b[0]] += 1
	freq[b[-1]] += 1
	for i in range(1, len(b) - 1):
		freq[b[i]] += 1
		if b[i] != b[i - 1] and b[i] != b[i + 1] and (b[i] != '_'):
			happy = False
	return happy
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	freq = {}
	YES = False
	for i in string.ascii_uppercase:
		freq[i] = 0
	freq['_'] = 0
	if happiness(b, freq):
		YES = True
	elif b.count('_') != 0:
		YES = True
		del freq['_']
		for f in freq.items():
			if f[1] == 1:
				YES = False
				break
	if YES:
		print('YES')
	else:
		print('NO')


import sys
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = list(input().strip())
	dic = {}
	flag = True
	for i in range(len(b)):
		c = b[i]
		if i > 0 and i < len(b) - 1:
			if b[i - 1] != b[i] and b[i] != b[i + 1]:
				flag = False
		if c not in dic:
			dic[c] = 1
		else:
			dic[c] += 1
	if '_' in dic:
		blank = dic['_']
		del dic['_']
	else:
		blank = 0
	if 1 in dic.values():
		print('NO')
	elif flag:
		print('YES')
	elif len(dic) <= 1:
		print('YES')
	elif blank == 0:
		print('NO')
	else:
		print('YES')


from string import ascii_uppercase
Q = int(input().strip())
output = []
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	curr = True
	d = dict.fromkeys(ascii_uppercase, 0)
	d['_'] = 0
	for i in b:
		d[i] += 1
	if d['_'] == 0:
		j = 1
		while j < len(b) - 1:
			if b[j - 1] == b[j] or b[j] == b[j + 1]:
				j += 1
			else:
				curr = False
				break
		d['_'] = 2
	if d['_'] == 1:
		d['_'] = 2
	for i in d.values():
		if i == 1:
			curr = False
	if curr == False:
		output.append('NO')
	else:
		output.append('YES')
for i in output:
	print(i)


t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	l = [0] * (ord('z') - ord('a') + 1)
	count = 0
	for i in range(n):
		if s[i].isalpha():
			l[ord(s[i]) - ord('A')] += 1
		else:
			count += 1
	flag = 1
	for i in l:
		if i == 1:
			flag = 0
			print('NO')
			break
	if flag == 1 and count > 0:
		print('YES')
	elif flag == 1 and count == 0:
		temp = 1
		if s[0] != s[1] or s[-1] != s[-2]:
			temp = 0
		if temp != 0:
			for i in range(1, n - 1):
				if s[i] == s[i + 1] or s[i] == s[i - 1]:
					continue
				else:
					temp = 0
					break
		if temp == 0:
			print('NO')
		else:
			print('YES')


import sys
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	u = b.count('_')
	f = 0
	if len(b) == u:
		f = 1
	if f == 0:
		for i in range(len(b)):
			if b[i] != '_' and b.count(b[i]) < 2:
				f = 0
				break
			else:
				f = 1
	if f == 1 and b.count('_') >= 1:
		print('YES')
	elif f == 1 and b.count('_') == 0:
		t = 0
		for i in range(1, len(b) - 1):
			if b[i - 1] == b[i] or b[i + 1] == b[i]:
				t = 1
			else:
				t = 0
				break
		if t == 1:
			print('YES')
		else:
			print('NO')
	else:
		print('NO')


games = int(input())
for g in range(games):
	n = int(input())
	b = input()
	bugs = {}
	underscore = False
	for bi in b:
		if bi == '_':
			underscore = True
		elif bi in bugs:
			bugs[bi] += 1
		else:
			bugs[bi] = 1
	if underscore:
		for bi in bugs:
			if bugs[bi] == 1:
				print('NO')
				break
		else:
			print('YES')
	elif n == 1:
		print('NO')
	elif b[0] != b[1]:
		print('NO')
	else:
		for i in range(1, n - 1):
			if b[i - 1] != b[i] and b[i + 1] != b[i]:
				print('NO')
				break
		else:
			if b[-1] != b[-2]:
				print('NO')
			else:
				print('YES')


import sys
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	hash = [0] * 26
	dict = {}
	count = 0
	pehlaFlag = False
	for i in range(0, n):
		if b[i] != '_':
			count += 1
			hash[ord(b[i]) - 65] += 1
	if count == 0:
		print('YES')
	elif count < n:
		for i in range(0, 26):
			if hash[i] == 1:
				pehlaFlag = True
				break
		if pehlaFlag:
			print('NO')
		else:
			print('YES')
	elif count == n:
		contiCount = 1
		flag = False
		b = b + ';'
		j = 1
		while j < n + 1:
			if b[j] == b[j - 1]:
				contiCount = contiCount + 1
			else:
				if contiCount < 2:
					flag = True
					break
				contiCount = 1
			j = j + 1
		if flag == False:
			print('YES')
		else:
			print('NO')


import sys
import collections

def allHappy(b):
	bugOnly = b.replace('_', '')
	ladybugs = dict(collections.Counter(bugOnly))
	for bug in ladybugs:
		if ladybugs[bug] == 1:
			return False
	if len(b) == len(bugOnly):
		if len(b) <= 1:
			return False
		if b[0] != b[1]:
			return False
		if b[len(b) - 1] != b[len(b) - 2]:
			return False
		for i in range(1, len(b) - 1):
			if b[i] != b[i - 1] and b[i] != b[i + 1]:
				return False
	return True
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	if allHappy(b):
		print('YES')
	else:
		print('NO')


import string
import sys

def ladybugsVedlaSeba(b, n):
	for i in range(1, n - 1):
		if b[i] != b[i - 1] and b[i] != b[i + 1]:
			return 'NO'
		return 'YES'

def check_function(b, n):
	any_character_just_once = False
	alphabet = list(string.ascii_uppercase)
	for i in alphabet:
		count_chr = b.count(i)
		if count_chr == 1:
			any_character_just_once = True
	if any_character_just_once:
		print('NO')
	elif '_' in b:
		print('YES')
	else:
		print(ladybugsVedlaSeba(b, n))
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	check_function(b, n)


from itertools import groupby
from collections import Counter
import sys
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	if '_' not in b:
		l1 = [list(group) for (key, group) in groupby(b)]
		z = 1
		for x in l1:
			if len(x) < 2:
				z = 0
				print('NO')
				break
		if z == 1:
			print('YES')
	else:
		b = b.replace('_', '')
		b = Counter(b)
		z = 1
		for x in b.values():
			if x < 2:
				z = 0
				print('NO')
				break
		if z == 1:
			print('YES')


import sys
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	d = {}
	for i in b:
		if i not in d:
			d[i] = 1
		else:
			d[i] += 1
	sol = True
	for i in d.keys():
		if d[i] < 2 and i != '_':
			sol = False
	if not sol:
		print('NO')
	else:
		cond = False
		for i in d.keys():
			if d[i] == n:
				print('YES')
				cond = True
		if not cond:
			ord = True
			if '_' in d:
				print('YES')
			else:
				for i in range(0, n):
					if i == 0:
						if b[i] != b[i + 1]:
							ord = False
					elif i == n - 1:
						if b[i] != b[i - 1]:
							ord = False
					elif b[i] != b[i + 1] and b[i] != b[i - 1]:
						ord = False
				if not ord:
					print('NO')
				else:
					print('YES')


import sys
from collections import Counter
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	c = Counter(b)
	if '_' in c.keys():
		if all([v >= 2 for (k, v) in c.items() if k != '_']):
			print('YES')
		else:
			print('NO')
	else:
		(counter, last_char) = (0, None)
		for (i, bb) in enumerate(b):
			if i == 0:
				last_char = bb
				counter = 1
			elif bb == last_char:
				counter += 1
				last_char = bb
			elif counter >= 2:
				counter = 1
				last_char = bb
			else:
				print('NO')
				break
		else:
			if counter == 1:
				print('NO')
			else:
				print('YES')


import sys
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	l = {}
	happy = 1
	for i in b:
		l[i] = 0
	for i in b:
		l[i] += 1
	for i in l:
		if i != '_' and l[i] == 1:
			happy = 0
			break
	if '_' not in l:
		for i in range(1, len(b) - 1):
			if not (b[i] == b[i - 1] or b[i] == b[i + 1]):
				happy = 0
				break
	if happy:
		print('YES')
	else:
		print('NO')


import sys
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	wo = b.replace('_', '')
	happy = False
	if len(wo) == len(b):
		happy = False if len(wo) == 1 else wo[0] == wo[1] and wo[-1] == wo[-2]
		for i in range(1, len(wo) // 2):
			if wo[2 * i] != wo[2 * i + 1] and wo[2 * i] != wo[2 * i - 1]:
				happy = False
				break
	d = {}
	for bug in b:
		if not bug in d:
			d[bug] = 0
		d[bug] += 1
	if not happy:
		happy = False if '_' not in d else d['_'] > 0
		for (v, i) in d.items():
			if v != '_' and i == 1:
				happy = False
				break
	print('YES' if happy else 'NO')


import sys
from collections import Counter
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	ans = True
	if '_' in b:
		c = Counter(b)
		for (x, y) in c.items():
			if x != '_' and y == 1:
				ans = False
	else:
		for i in range(len(b)):
			if (i == 0 or b[i] != b[i - 1]) and (i == len(b) - 1 or b[i] != b[i + 1]):
				ans = False
	print('YES' if ans else 'NO')


import sys

def valid(n, b, freq):
	if sum(freq.values()) == n:
		first = b[0]
		count = 1
		for i in range(1, n):
			if b[i] == first:
				count += 1
			elif count == 1:
				return False
			else:
				first = b[i]
				count = 1
		if count == 1:
			return False
		return True
	else:
		for value in freq.values():
			if value < 2:
				return False
		return True
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	freq = {}
	for letter in b:
		if letter == '_':
			continue
		elif letter in freq:
			freq[letter] += 1
		else:
			freq[letter] = 1
	if valid(n, b, freq):
		print('YES')
	else:
		print('NO')


from collections import Counter
import re

class Happy_Ladybugs:

	def __init__(self):
		self.patt = re.compile('(([A-Z])\\2{1,})')

	def happy(self, b):
		c = Counter(b)
		c.pop('_', None)
		if 1 in c.values():
			return 'NO'
		if sum([len(group[0]) for group in self.patt.findall(b)]) == len(b):
			return 'YES'
		elif '_' in b:
			return 'YES'
		else:
			return 'NO'

	def solve(self):
		for _ in range(int(input())):
			input()
			print(self.happy(input().strip()))
sol = Happy_Ladybugs()
sol.solve()


import sys
from collections import Counter

def already_happy(board: str):
	board2 = '_' + board + '_'
	for k in range(1, len(board) + 1):
		if board2[k] == '_':
			continue
		if board2[k] != board2[k - 1] and board2[k] != board2[k + 1]:
			return False
	return True

def happy(board: str):
	if already_happy(board):
		return True
	counter = Counter(board)
	return counter['_'] > 0 and min((k for (x, k) in counter.items() if x != '_'), default=0) != 1
for _ in range(int(input())):
	input()
	print('YES' if happy(input()) else 'NO')


from collections import Counter
from itertools import groupby
for _ in range(int(input().strip())):
	n = int(input().strip())
	b = input().strip()
	underscores = b.count('_')
	characters = b.replace('_', '')
	count_char = Counter(characters)
	if underscores > 0:
		out = 'YES'
		for i in count_char:
			if count_char[i] > 1:
				continue
			else:
				out = 'NO'
				break
		print(out)
	else:
		out = 'YES'
		for (i, j) in groupby(characters):
			if len(list(j)) > 1:
				continue
			else:
				out = 'NO'
				break
		print(out)


import sys
from collections import Counter
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	colors = Counter(b)
	if len([x[0] for x in colors.items() if x[1] == 1 and x[0] != '_']) > 0:
		print('NO')
		continue
	if colors['_'] > 0:
		print('YES')
	else:
		for i in range(len(b)):
			if i == 0 and b[i] == b[i + 1] or (i == len(b) - 1 and b[i] == b[i - 1]) or b[i] in [b[i - 1], b[i + 1]]:
				continue
			else:
				break
		if i == len(b) - 1:
			print('YES')
		else:
			print('NO')


import sys
import collections

def solve(b):
	counter = collections.Counter(b)
	for (k, v) in counter.items():
		if k == '_':
			continue
		if v < 2:
			return False
	if counter['_'] > 0:
		return True
	x = '#' + b + '#'
	for i in range(1, len(b) + 1):
		if x[i] != x[i - 1] and x[i] != x[i + 1]:
			return False
	return True
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	print('YES' if solve(b) else 'NO')


import sys
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	b = list(b)
	length = len(b)
	alpha = [0] * 26
	nou = 0
	flag_print = 0
	for i in range(length):
		if b[i] == '_':
			nou += 1
		else:
			alpha[ord(b[i]) % 65] += 1
	no_odd = 0
	for i in range(26):
		if alpha[i] == 1:
			flag_print = 1
		if alpha[i] % 2 != 0:
			no_odd += 1
	if flag_print == 1:
		print('NO')
	elif nou == 0:
		if b[0] != b[1]:
			print('NO')
		elif b[length - 1] != b[length - 2]:
			print('NO')
		else:
			for i in range(1, length - 2):
				if b[i] != b[i - 1] and b[i] != b[i + 1]:
					flag_print = 1
					break
			if flag_print == 1:
				print('NO')
			else:
				print('YES')
	else:
		print('YES')


import sys
from itertools import combinations
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	c = b.replace('_', '')
	d = ''
	if '_' in b:
		for i in c:
			for j in c:
				if i == j:
					d = d + i
		if len(d) == 0:
			print('YES')
		elif len(d) == 1:
			print('NO')
		elif len(d) == 2:
			if d[0] == d[1]:
				print('YES')
			else:
				print('NO')
		elif len(d) == 3:
			if d[0] == d[1] and d[1] == d[2]:
				print('YES')
			else:
				print('NO')
		else:
			ctr = 0
			if d[0] == d[1] and d[-1] == d[-2]:
				ctr = ctr + 2
			for i in range(1, len(d) - 1):
				if d[i] == d[i - 1] or d[i] == d[i + 1]:
					ctr = ctr + 1
				else:
					break
			if ctr == len(d):
				print('YES')
			else:
				print('NO')
	elif len(b) == 0:
		print('YES')
	elif len(b) == 1:
		print('NO')
	elif len(b) == 2:
		if b[0] == b[1]:
			print('YES')
		else:
			print('NO')
	elif len(b) == 3:
		if b[0] == b[1] and b[1] == b[2]:
			print('YES')
		else:
			print('NO')
	else:
		ctr = 0
		if b[0] == b[1] and b[-1] == b[-2]:
			ctr = ctr + 2
		for i in range(1, len(b) - 1):
			if b[i] == b[i - 1] or b[i] == b[i + 1]:
				ctr = ctr + 1
			else:
				break
		if ctr == len(b):
			print('YES')
		else:
			print('NO')


import sys
Q = int(input().strip())
for a0 in range(Q):
	res = 'YES'
	n = int(input().strip())
	b = input().strip()
	t = b
	b = b.replace('_', '')
	for i in b:
		if b.count(i) < 2:
			res = 'NO'
			break
	if t.count('_') < 1:
		for k in range(1, len(b) - 1):
			if b[k] != b[k + 1] and b[k] != b[k - 1]:
				res = 'NO'
				break
	print(res)


import sys

def allHappy(b):
	bp = '_' + b + '_'
	for (i, c) in enumerate(b, 1):
		if c != '_' and bp[i - 1] != c and (bp[i + 1] != c):
			return False
	return True
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	nBugs = [0] * 26
	nEmpty = 0
	for c in b:
		if c == '_':
			nEmpty += 1
		else:
			nBugs[ord(c) - ord('A')] += 1
	if nEmpty > 0:
		if 1 not in nBugs:
			print('YES')
		else:
			print('NO')
	elif allHappy(b):
		print('YES')
	else:
		print('NO')


from collections import Counter
import sys

def check_if_ladybugs_can_be_happy(board, n):
	count_b = Counter(board)
	if count_b['_'] == n:
		return True
	for elem in count_b.most_common()[-2:]:
		if elem[0] != '_' and elem[-1] < 2:
			return False
	if count_b['_'] == 0:
		if n == 2 and b[0] != b[1]:
			return False
		else:
			for i in range(1, n - 1):
				if board[i - 1] != board[i] and board[i + 1] != board[i]:
					return False
	return True
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	result = check_if_ladybugs_can_be_happy(b, n)
	if result:
		print('YES')
	else:
		print('NO')


g = int(input())
n = []
b = []
for i in range(g):
	n.append(int(input()))
	b.append(list(input().upper().strip()))

def isWin(board):
	num_alpha = []
	num_blank = board.count('_')
	for i in range(len(board)):
		if board[i].isalpha():
			num_alpha.append(board.count(board[i]))
	for j in num_alpha:
		if j == 1:
			return 'NO'
	if len(board) == 3 and num_blank == 0:
		if board[0] != board[1] and board[1] != board[2] and (board[0] != board[2]):
			return False
	if len(board) > 3 and num_blank == 0:
		for i in range(len(board) - 2):
			if i == 0:
				if board[i] != board[i + 1]:
					return 'NO'
			elif i == len(board) - 2:
				if board[-1] != board[-2]:
					return 'NO'
			elif board[i] != board[i + 1] and board[i + 1] != board[i + 2]:
				return 'NO'
	return 'YES'
for i in range(g):
	win = isWin(b[i])
	print(win)


def count_colors(b):
	colors = {}
	for i in b:
		if i != '_':
			if i in colors:
				colors[i] += 1
			else:
				colors[i] = 1
	return colors

def count_spaces(b):
	return sum([1 for i in b if i == '_'])

def everyone_happy(b):
	for i in range(1, len(b) - 1):
		if b[i] != b[i - 1] and b[i] != b[i + 1]:
			return False
	return True

def possible_to_make_happy(b):
	colors_dict = count_colors(b)
	num_spaces = count_spaces(b)
	for i in colors_dict.values():
		if i < 2:
			return 'NO'
	if num_spaces == 0:
		if everyone_happy(b):
			return 'YES'
		else:
			return 'NO'
	if num_spaces == len(b):
		return 'YES'
	return 'YES'
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	print(possible_to_make_happy(b))


import sys
from collections import Counter

def is_happy(board: str) -> bool:
	if '_' in board:
		cnt = Counter(board.replace('_', ''))
		return 1 not in cnt.values()
	else:
		for (i, c) in enumerate(board):
			if (i == 0 or board[i - 1] != c) and (i == len(board) - 1 or board[i + 1] != c):
				return False
		return True
Q = int(input().strip())
for _ in range(Q):
	n = int(input().strip())
	b = input().strip()
	if is_happy(b):
		print('YES')
	else:
		print('NO')


import sys

def calculate(b):
	prev_ch = '_'
	prev_happy = True
	parny = True
	for ch in b:
		if ch == '_':
			if prev_happy == False:
				break
		elif prev_ch == ch:
			prev_happy = True
		elif prev_happy == False:
			break
		else:
			prev_happy = False
		prev_ch = ch
	all_happy = prev_happy
	if all_happy:
		return True
	if not '_' in b:
		return False
	dic = {}
	for ch in b:
		if ch != '_':
			if ch in dic:
				dic[ch] += 1
			else:
				dic[ch] = 1
	for d in dic:
		if dic[d] == 1:
			return False
	return True

def happy(n, b):
	if calculate(b):
		return 'YES'
	return 'NO'
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	print(happy(n, b))


import sys
from itertools import groupby
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = list(input().strip())
	result = []
	uniq_b = set(b)
	if '_' not in uniq_b:
		for (x, y) in groupby(b):
			if len(list(y)) >= 2:
				result.append(True)
			else:
				result.append(False)
	else:
		for elm in uniq_b:
			if elm is not '_' and b.count(elm) > 1:
				result.append(True)
			elif elm is not '_' and b.count(elm) == 1:
				result.append(False)
	if False in result:
		print('NO')
	else:
		print('YES')


import sys
from collections import Counter

def continuous_check(b):
	now = [b[0], 1]
	all = [now]
	for char in b[1:]:
		if char == now[0]:
			now[1] = now[1] + 1
		else:
			now = [char, 1]
			all.append(now)
	for i in all:
		if i[1] == 1:
			output = 'NO'
			break
		else:
			output = 'YES'
	return output

def total_check(b):
	b = b.replace('_', '')
	x = Counter(b)
	output = 'YES'
	for (key, value) in x.items():
		if value < 2:
			output = 'NO'
			break
	return output

def check1(b):
	b_dict = Counter(b)
	if b == '_' * len(b):
		return 'YES'
	elif '_' not in b_dict:
		return continuous_check(b)
	elif '_' in b_dict:
		return total_check(b)
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	print(check1(b))


for _ in range(int(input())):
	n = input()
	has_empty = False
	arr = [0] * 26
	pre_char = None
	no_need_to_swap = True
	pre_char = None
	pre_char_count = 0
	for char in input():
		if char == '_':
			has_empty = True
		else:
			arr[ord(char) - ord('A')] += 1
			if not has_empty and no_need_to_swap:
				if pre_char:
					if char == pre_char:
						pre_char_count += 1
					elif pre_char_count == 1:
						no_need_to_swap = False
					else:
						pre_char = char
						pre_char_count = 1
				else:
					pre_char = char
					pre_char_count = 1
	if has_empty and all((count != 1 for count in arr)):
		print('YES')
	elif no_need_to_swap and pre_char_count > 1:
		print('YES')
	else:
		print('NO')


import sys

def allhappy(n, b):
	nempty = len([c for c in b if c == '_'])
	if nempty == 0:
		if n < 2:
			return False
		ind = [False] * n
		if b[0] == b[1]:
			ind[0] = True
		for i in range(1, n - 1):
			if b[i - 1] == b[i] or b[i] == b[i + 1]:
				ind[i] = True
		if b[n - 2] == b[n - 1]:
			ind[n - 1] = True
		nfalse = len([x for x in ind if x == False])
		if nfalse > 0:
			return False
		else:
			return True
	else:
		d = {}
		for c in b:
			if c != '_':
				if c not in d:
					d[c] = 1
				else:
					d[c] += 1
		nones = len([x for x in d if d[x] == 1])
		if nones > 0:
			return False
		else:
			return True
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	ans = allhappy(n, b)
	if ans:
		print('YES')
	else:
		print('NO')


import sys
from collections import Counter
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	cnt = Counter(list(b))
	s = list(set(b.replace('_', '')))
	u = cnt.get('_', 0)
	if u == len(b):
		print('YES')
		continue
	ex = False
	for a in s:
		if cnt.get(a) == 1:
			print('NO')
			ex = True
			break
		if u == 0:
			t = [i for (i, x) in enumerate(b) if x == a]
			d = set([j - i for (i, j) in zip(t[:-1], t[1:])])
			for k in d:
				if k != 1:
					print('NO')
					ex = True
					break
			if ex:
				break
	if ex:
		continue
	else:
		print('YES')


for i in range(int(input())):
	n = int(input())
	b = input()
	happy = True
	for color in set(b.replace('_', '')):
		if b.count(color) < 2:
			happy = False
	if b.count('_') < 1:
		for i in range(len(b)):
			left = b[i - 1] if i - 1 in range(len(b)) else ''
			mid = b[i]
			right = b[i + 1] if i + 1 in range(len(b)) else ''
			if left != mid != right:
				happy = False
	if happy:
		print('YES')
	else:
		print('NO')


from collections import Counter

def can_be_happy(n, b):
	if '_' not in b:
		for i in range(n):
			if i > 0 and b[i - 1] == b[i]:
				continue
			if i < n - 1 and b[i + 1] == b[i]:
				continue
			return 'NO'
		return 'YES'
	counts = Counter(b)
	if any((count == 1 for (char, count) in counts.items() if char != '_')):
		return 'NO'
	else:
		return 'YES'
cases = int(input())
for case in range(cases):
	n = int(input())
	b = input()
	print(can_be_happy(n, b))


import sys
import collections
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	counter = collections.Counter(b)
	if len(counter) == 1 and '_' in counter:
		print('YES')
		continue
	if all([x > 1 for (k, x) in counter.items() if k != '_']) and counter.get('_', 0) > 0:
		print('YES')
		continue
	newb = list(filter(lambda x: x != '_', b))
	current = newb[0]
	current_count = 1
	for l in newb[1:]:
		if l == current:
			current_count += 1
		else:
			if current_count < 2:
				break
			current = l
			current_count = 1
	else:
		if current_count > 1:
			print('YES')
			continue
	print('NO')


from itertools import groupby

def main():
	for i in range(int(input())):
		input()
		b = input().strip()
		if '_' in b:
			b = sorted(b)
		happy = all((k == '_' or len(list(it)) != 1 for (k, it) in groupby(b)))
		print('YES' if happy else 'NO')
main()


t = int(input())
while t > 0:
	n = int(input())
	s = input()
	space = 0
	flag = 1
	d = dict()
	for i in range(n):
		if s[i] == '_':
			space = space + 1
		elif s[i] in d:
			d[s[i]] = d[s[i]] + 1
		else:
			d[s[i]] = 1
	if space == 0:
		i = 0
		while i < n:
			x = d[s[i]]
			j = i
			while j < i + x:
				if s[j] != s[i]:
					flag = 0
					break
				j = j + 1
			if flag == 0:
				break
			else:
				i = i + x
	flag1 = 1
	for i in d:
		if d[i] == 1:
			flag1 = 0
			break
	if flag1 == 0 or flag == 0:
		print('NO')
	else:
		print('YES')
	t = t - 1


import sys
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	c = [0] * 26
	d = 0
	flag = 0
	for x in b:
		if x == '_':
			d += 1
			continue
		z = ord(x) - 65
		c[z] += 1
	if 1 in c:
		print('NO')
		continue
	if d >= 1:
		print('YES')
		continue
	if d == 0 and b[0] == b[1] and (b[n - 2] == b[n - 1]):
		flag = 1
		for i in range(1, n - 1):
			if not (b[i] == b[i - 1] or b[i] == b[i + 1]):
				flag = 2
				break
	if flag == 0 or flag == 2:
		print('NO')
	elif flag == 1:
		print('YES')


from collections import Counter
import sys

def is_happy(b, counter):
	for (elm, cnt) in counter.items():
		if cnt == 1:
			return False
		else:
			first = b.index(elm)
			for i in range(first + 1, first + cnt):
				if b[i] != elm:
					return False
	return True

def is_happy_has_empty(b, counter):
	for (elm, cnt) in counter.items():
		if cnt == 1:
			return False
		else:
			pass
	return True
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	counter = Counter(b)
	if counter['_'] == n:
		print('YES')
	elif counter['_'] == 0:
		if is_happy(b, counter):
			print('YES')
		else:
			print('NO')
	else:
		del counter['_']
		if is_happy_has_empty(b, counter):
			print('YES')
		else:
			print('NO')


abc = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

def check():
	if n == 1:
		return b[0] == '_'
	if b[0] != b[1] or b[n - 2] != b[n - 1]:
		return False
	for i in range(1, n - 1):
		if b[i] != b[i - 1] and b[i] != b[i + 1]:
			return False
	return True
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	n_ = b.count('_')
	if n_ > 0:
		if any((b.count(l) == 1 for l in abc)):
			print('NO')
		else:
			print('YES')
	else:
		print('YES' if check() else 'NO')


import sys
from collections import Counter
from itertools import groupby
Q = int(input().strip())
boards = []
for a0 in range(Q):
	n = int(input().strip())
	boards.append(input().strip())
for board in boards:
	counter = Counter()
	movable = False
	for cell in board:
		if cell == '_':
			movable = True
			continue
		counter[cell] += 1
	if movable:
		for color in counter:
			if counter[color] < 2:
				print('NO')
				break
		else:
			print('YES')
	else:
		board_grp = groupby(board)
		for (value, group) in board_grp:
			if len(list(group)) < 2:
				print('NO')
				break
		else:
			print('YES')


import sys

def check(s, n):
	if '_' in s:
		dict = {}
		for i in s:
			if i != '_':
				if i not in dict:
					dict[i] = 1
				else:
					dict[i] += 1
		for (k, r) in dict.items():
			if r < 2:
				return 'NO'
		return 'YES'
	elif len(s) == 1:
		return 'NO'
	else:
		arr = [s[0] == s[1]]
		for i in range(1, n - 1):
			if s[i] == s[i + 1]:
				arr.append(True)
			else:
				if not arr[-1]:
					return 'NO'
				arr.append(False)
		if not arr[-1]:
			return 'NO'
		return 'YES'
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	print(check(b, n))


from collections import defaultdict
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	d = defaultdict(int)
	if '_' in b:
		for c in b:
			if c != '_':
				d[c] += 1
		if 1 in d.values():
			print('NO')
		else:
			print('YES')
	else:
		b = '#' + b + '#'
		for i in range(1, len(b) - 1):
			if b[i] != b[i - 1] and b[i] != b[i + 1]:
				print('NO')
				break
		else:
			print('YES')


import sys

def f(s):
	b = '.' + s + '.'
	dct = {}
	for character in b:
		dct[character] = dct.get(character, 0) + 1
	if '_' not in dct:
		return all((b[i] in [b[i - 1], b[i + 1]] for i in range(1, len(b) - 1)))
	else:
		return not any((dct[k] == 1 for k in dct if k != '_'))
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	print('YES' if f(b) else 'NO')


import sys

def happy(astr):
	aset = set(astr)
	for elt in aset:
		if elt.isupper() and astr.count(elt) == 1:
			return 'NO'
	if astr.count('_') == 0:
		i = 0
		while i < len(astr) - 1:
			if i == 0 and astr[i] != astr[i + 1] or (astr[i] != astr[i + 1] and astr[i] != astr[i - 1]):
				return 'NO'
			i += 1
	return 'YES'
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	if len(b) == b.count('_'):
		print('YES')
	else:
		print(happy(b))


import sys
Q = int(input().strip())
for a0 in range(Q):
	m = {}
	happy = True
	current = '_'
	curCount = 2
	hasEmpty = False
	n = int(input().strip())
	b = input().strip()
	for s in b:
		if s == '_':
			hasEmpty = True
			continue
		if s == current:
			curCount += 1
		else:
			happy = happy and curCount > 1
			current = s
			curCount = 1
		ct = m.get(s, 0)
		ct += 1
		m[s] = ct
	happy = happy and curCount > 1
	if not happy and hasEmpty:
		happy = True
		for (key, value) in m.items():
			if value < 2:
				happy = False
				break
	print('YES' if happy else 'NO')


import sys
from collections import Counter
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	b1 = b.replace('_', '')
	if all((i > 0 and b1[i] == b1[i - 1] or (i + 1 < len(b1) and b1[i] == b1[i + 1]) for i in range(len(b1)))) or ('_' in b and all((t > 1 for t in Counter(b1).values()))):
		print('YES')
	else:
		print('NO')


import sys
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	b = list(b)
	c = b[:]
	b.sort()
	n = 0
	l = []
	flag = True
	while flag:
		nmbr = b.count(b[n])
		l.append(nmbr)
		if sum(l) == len(b):
			flag = False
		else:
			n = n + nmbr
	char = '_'
	if b[-1] == '_':
		count_under = l.pop()
	else:
		count_under = 0
	s = []
	loop_count = 0
	for nmbr in l:
		if nmbr == 1:
			s.append(False)
		elif nmbr % 2 == 1:
			if count_under >= 1:
				s.append(True)
			else:
				temp = c[loop_count]
				idx = c.index(temp)
				if [temp] * nmbr == c[idx:idx + nmbr]:
					s.append(True)
				else:
					s.append(False)
		elif count_under >= 1:
			s.append(True)
		else:
			temp = c[loop_count]
			idx = c.index(temp)
			if [temp] * nmbr == c[idx:idx + nmbr]:
				s.append(True)
			else:
				s.append(False)
		loop_count = loop_count + 1
	if False in s:
		print('NO')
	else:
		print('YES')


import sys
from collections import Counter
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	f = Counter(b)
	if sum([1 for (k, v) in f.most_common() if k != '_' and v == 1]) > 0:
		print('NO')
	elif '_' not in f and sum([1 for i in range(1, n - 1) if b[i - 1] != b[i] and b[i + 1] != b[i]]) > 0:
		print('NO')
	else:
		print('YES')


import sys

def isHappy(s):
	if len(s) == 1:
		return False
	elif len(s) == 2:
		if s[0] == s[1]:
			return True
		else:
			return False
	else:
		happy = True
		for i in range(1, len(s) - 1):
			if s[i] != s[i - 1] and s[i] != s[i + 1]:
				happy = False
		if s[0] != s[1] or s[len(s) - 1] != s[len(s) - 2]:
			happy = False
		return happy
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	happyRightNow = isHappy(b)
	if happyRightNow != True:
		isPossibleToBehappy = True
		c = len(list(filter(lambda x: x == '_', b)))
		if c < 1:
			isPossibleToBehappy = False
		else:
			d = {}
			for i in range(len(b)):
				if b[i] != '_':
					if b[i] in d:
						d[str(b[i])] += 1
					else:
						d[str(b[i])] = 1
			for (key, value) in d.items():
				if value == 1:
					isPossibleToBehappy = False
		if isPossibleToBehappy == True:
			print('YES')
		else:
			print('NO')
	else:
		print('YES')


Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	space = False
	counter = {}
	for pos in b:
		if pos == '_':
			space = True
		else:
			counter[pos] = counter.get(pos, 0) + 1
	if space:
		posible = True
		for key in counter:
			if counter[key] == 1:
				posible = False
				break
		print('YES' if posible else 'NO')
	else:
		alreadyHappy = True
		for i in range(n):
			goodNeighbor = False
			if i > 0:
				if b[i] == b[i - 1]:
					goodNeighbor = True
			if i < n - 1:
				if b[i] == b[i + 1]:
					goodNeighbor = True
			if not goodNeighbor:
				alreadyHappy = False
				break
		print('YES' if alreadyHappy else 'NO')


import sys
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	adj = True
	underscore = False
	lenb = len(b)
	array = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
	for k in range(lenb):
		if b[k] == '_':
			underscore = True
		else:
			array[ord(b[k]) - 65] += 1
			if k - 1 >= 0 and b[k - 1] != b[k] and (k + 1 < lenb) and (b[k + 1] != b[k]):
				adj = False
	if 1 in array:
		print('NO')
	elif underscore:
		print('YES')
	elif not adj:
		print('NO')
	else:
		print('YES')


import sys
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	bl = set(b)
	uni = [b.count(s) for s in bl if s != '_']
	if 1 in uni:
		print('NO')
	elif '_' not in b:
		i = 1
		f = 0
		while i < len(b):
			if f >= 2:
				print('NO')
				break
			elif i >= len(b) - 1:
				print('YES')
				break
			elif b[i - 1] == b[i]:
				i += 1
				f = 0
			else:
				i += 1
				f += 1
	elif '_' == b:
		print('YES')
	else:
		print('YES')


import sys

def allhappy(N, B):
	C = [B.count(ch) for ch in alphabet]
	if C[0] == N:
		return 'YES'
	if 1 in C[1:]:
		return 'NO'
	if C[0] == 0:
		if B[0] != B[1] or B[-2] != B[-1]:
			return 'NO'
		for i in range(1, N - 1):
			if B[i - 1] != B[i] and B[i] != B[i + 1]:
				return 'NO'
		return 'YES'
	return 'YES'
alphabet = '_ABCDEFGHIJKLMNOPQRSTUVWXYZ'
g = int(input().strip())
for t in range(0, g):
	n = int(input().strip())
	b = input().strip()
	print(allhappy(n, b))


import sys
from collections import defaultdict

def IsHappyState(b, n):
	if n == 1:
		return False
	returnValue = False
	for i in range(0, n):
		if b[i] != '_':
			returnValue = True
	if b[0] != b[1]:
		return False
	for i in range(1, n - 1):
		if not (b[i - 1] == b[i] or b[i] == b[i + 1]):
			return False
	if b[n - 2] != b[n - 1]:
		return False
	return returnValue

def CanBeHappy1(b, n):
	colorDict = defaultdict(int)
	for i in range(0, n):
		if b[i] != '_':
			colorDict[b[i]] += 1
	for (key, value) in colorDict.items():
		if value == 1:
			return False
	return True

def HasEmptyPos(b, n):
	for i in range(0, n):
		if b[i] == '_':
			return True
	return False
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	if CanBeHappy1(b, n):
		if HasEmptyPos(b, n):
			print('YES')
		else:
			result = IsHappyState(b, n)
			if result:
				print('YES')
			else:
				print('NO')
	else:
		print('NO')


import sys
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	b = list(b)
	d = {}
	blank = 0
	for i in b:
		if i != '_':
			d[i] = d.get(i, 0) + 1
		else:
			blank += 1
	if len(d) == 0:
		print('YES')
	else:
		if blank > 0:
			b.sort()
		while '_' in b:
			b.remove('_')
		count = 1
		prev = 0
		for i in b:
			if i == prev:
				count += 1
			elif count > 0:
				count = 0
				prev = i
			else:
				break
		if count == 0:
			print('NO')
		else:
			print('YES')


import sys
from collections import Counter
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	myList = list(b)
	d = Counter(myList)
	if len(d) < 2:
		if d['_'] == 0:
			for k in d:
				if d[k] > 1:
					print('YES')
				else:
					print('NO')
		else:
			print('YES')
	if len(d) >= 2:
		if d['_'] == 0:
			count = 1
			for k in range(1, n):
				if myList[k] == myList[k - 1]:
					count += 1
				elif count == 1:
					print('NO')
					break
				else:
					count = 1
				if k == n - 1 and count == 1:
					print('NO')
				elif k == n - 1 and count != 1:
					print('YES')
		else:
			c = False
			for x in d:
				if d[x] == 1 and x != '_':
					c = True
					print('NO')
					break
			if c == False:
				print('YES')


import sys

def ishappy(b):
	c = 0
	if len(b) == 1:
		return False
	for i in range(len(b) - 1):
		if b[i] == b[i + 1]:
			c += 1
		elif c < 1 or i == len(b) - 2:
			return False
		else:
			c = 0
	return True
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	board = list(input().strip())
	blank = board.count('_')
	if blank:
		board = list(filter(lambda c: True if c != '_' else False, board))
		board.sort()
	print('YES' if ishappy(board) else 'NO')


def happyLadybug(sequence, n):
	for i in range(1, n - 1):
		if sequence[i] != sequence[i + 1] and sequence[i] != sequence[i - 1]:
			return False
	return True
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	a = 0
	aList = []
	for letter in b:
		if letter not in aList and letter.isalpha() and (b.count(letter) == 1):
			a = 1
			aList.append(letter)
	bList = list(b)
	if a == 1:
		print('NO')
	elif b.count('_') == 0 and happyLadybug(b, n) is False:
		print('NO')
	elif happyLadybug(b, n) is True or b.count('_') != 0 or b.count('_') == n:
		print('YES')


import sys
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	if n == 0:
		print('YES')
		continue
	b = input().strip()
	bs = list(b)
	bset = set(bs)
	nspace = bs.count('_')
	if nspace == 0:
		nchange = 0
		previous = ''
		for b in bs:
			if b != previous:
				nchange += 1
				previous = b
			else:
				nchange = 0
			if nchange == 2:
				break
		if nchange >= 1:
			print('NO')
		else:
			print('YES')
		continue
	happy = True
	for item in bset:
		if item != '_' and bs.count(item) == 1:
			happy = False
			break
	if happy:
		print('YES')
	else:
		print('NO')


import sys
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	ladybugs = {}
	free_spaces = 0
	for char in list(b):
		if char == '_':
			free_spaces += 1
		else:
			ladybugs[char] = ladybugs.get(char, 0) + 1
	answer = 'YES'
	for (color, n) in ladybugs.items():
		if n == 1:
			answer = 'NO'
			break
	if answer != 'NO' and free_spaces == 0:
		for i in range(len(b)):
			if i == 0 or b[i] != b[i - 1]:
				if i == len(b) - 1 or b[i] != b[i + 1]:
					answer = 'NO'
					break
	print(answer)


g = int(input())
for game in range(g):
	n = int(input())
	s = input()
	if '_' not in s:
		if len(s) == 1:
			print('NO')
		elif len(s) == 2:
			if s[0] == s[1]:
				print('YES')
			else:
				print('NO')
		elif s[0] != s[1] or s[-1] != s[-2]:
			print('NO')
		else:
			res = 'YES'
			for i in range(1, len(s) - 1):
				if s[i - 1] != s[i] and s[i] != s[i + 1]:
					isNo = 'NO'
					break
			print(res)
	else:
		dic = {}
		for c in s:
			if c == '_':
				continue
			if c not in dic:
				dic[c] = 0
			dic[c] += 1
		res = 'YES'
		for c in dic:
			if dic[c] == 1:
				res = 'NO'
				break
		print(res)


import sys
from collections import defaultdict

def can_be_happy(b):
	color_counts = defaultdict(lambda : 0)
	color_counts['_'] = 0
	for color in b:
		color_counts[color] += 1
	for (color, count) in color_counts.items():
		if color == '_' and count == 0:
			return False
		if color != '_' and count == 1:
			return False
	return True

def is_happy(b):
	happy_status = [False] * len(b)
	for (i, c) in enumerate(b):
		happy_status[i] |= c == '_'
	for i in range(1, len(b)):
		happy_status[i] |= b[i] == b[i - 1]
	for i in range(0, len(b) - 1):
		happy_status[i] |= b[i] == b[i + 1]
	for status in happy_status:
		if status == False:
			return False
	return True
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	(can_happy, is_hap) = (can_be_happy(b), is_happy(b))
	if can_happy or is_hap:
		print('YES')
	else:
		print('NO')


import sys
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	happy = 'YES'
	letterCount = {}
	for letter in b:
		if letter in letterCount:
			letterCount[letter] += 1
		else:
			letterCount[letter] = 1
	for (letter, count) in letterCount.items():
		if count == 1 and letter != '_':
			happy = 'NO'
	if happy == 'YES' and '_' not in letterCount:
		if b[0] != b[1] or b[-1] != b[-2]:
			happy = 'NO'
		for (index, letter) in enumerate(b[1:-1]):
			if b[index - 1] != letter and letter != b[index + 1]:
				happy = 'NO'
	print(happy)


import sys
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	flag = True
	c = [0 for _ in range(27)]
	if n == 1 and b == '_':
		print('YES')
		continue
	elif n == 1:
		print('NO')
		continue
	for i in range(n):
		if i == 0 and b[i] != b[i + 1] and (b[i] != '_') and (b[i + 1] != '_'):
			flag = False
		elif i < n - 1 and b[i] != b[i - 1] and (b[i] != b[i + 1]) and (b[i] != '_') and (b[i + 1] != '_') and (b[i - 1] != '_'):
			flag = False
		elif i == n - 1 and b[i] != b[i - 1] and (b[i] != '_') and (b[i - 1] != '_'):
			flag = False
		if b[i] != '_':
			c[ord(b[i]) - 65] += 1
		else:
			c[26] += 1
	for x in c[:26]:
		if x == 1:
			flag = False
			c[26] = 0
			break
	if flag == False and c[26] == 0:
		print('NO')
	else:
		print('YES')


import sys
import collections

def has_single_ladybug(s):
	letters = collections.Counter(b)
	del letters['_']
	is_possible = sum((0 if letters[key] != 1 else 1 for key in letters.keys()))
	return is_possible < 1

def has_empty_space(s):
	return s.count('_') > 0

def all_beside_eachother(s):
	curr_letter = '?'
	besides = True
	for letter in s:
		if letter == '_':
			continue
		if curr_letter != letter:
			if not besides:
				return False
			else:
				curr_letter = letter
				besides = False
		else:
			besides = True
	return s[-1] == s[-2]
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	all_happy = has_single_ladybug(b)
	if all_happy:
		all_happy = has_empty_space(b)
		if not all_happy:
			all_happy = all_beside_eachother(b)
	print('YES' if all_happy else 'NO')


import sys
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	m = {i: b.count(i) for i in set(b)}
	if '_' not in m:
		result = []
		for (i, s) in enumerate(b):
			l = len(result)
			if l == 0 or s != b[i - 1]:
				result.append(s)
			else:
				result[l - 1] += s
		if any(map(lambda x: len(x) < 2, result)):
			print('NO')
		else:
			print('YES')
	else:
		del m['_']
		if any(map(lambda x: x < 2, m.values())):
			print('NO')
		else:
			print('YES')


import sys

def good_board(board):
	for i in range(0, len(board)):
		if board[i] == '_':
			continue
		left_happy = False
		right_happy = False
		if i > 0 and board[i] == board[i - 1]:
			left_happy = True
		if i < len(board) - 1 and board[i] == board[i + 1]:
			right_happy = True
		if not left_happy and (not right_happy):
			return False
	return True

def key_map(board):
	keys = {}
	for x in board:
		if x == '_':
			continue
		if keys.get(x) == None:
			keys[x] = 1
		else:
			keys[x] += 1
	return keys

def test_board(board):
	if len(board) <= 0:
		return False
	if good_board(board):
		return True
	bugs = key_map(board)
	for bug in bugs:
		if bugs[bug] == 1:
			return False
	if not '_' in board:
		return False
	return True

def find_open_spaces(board):
	results = []
	for i in range(0, len(board)):
		if board[i] == '_':
			results.append(i)
	return results

def run_test():
	cell_count = int(input().strip())
	board = list(input().strip())
	result = test_board(board)
	if result:
		print('YES')
	else:
		print('NO')

def run_all_test():
	test_count = int(input().strip())
	for i in range(0, test_count):
		run_test()
run_all_test()


import sys
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	has_empty = False
	tracker = {}
	happy = True
	for i in range(len(b)):
		if i == 0:
			if i + 1 == len(b):
				happy = False
				break
			if b[i] != b[i + 1]:
				happy = False
				break
		elif i == len(b) - 1:
			if i - 1 < 0:
				happy = False
				break
			if b[i] != b[i - 1]:
				happy = False
				break
		elif b[i] != b[i - 1] and b[i] != b[i + 1]:
			happy = False
			break
	if happy:
		print('YES')
		continue
	for ch in b:
		if ch == '_':
			has_empty = True
		elif ch in tracker:
			tracker[ch] = True
		else:
			tracker[ch] = False
	happy = True
	for (key, value) in tracker.items():
		if not value:
			happy = False
	if happy and has_empty:
		print('YES')
	else:
		print('NO')


import sys
import re

def happiness_possible(s):
	if len(s) == 0:
		return True
	if not re.search('[A-Z]', s):
		return True
	z = re.subn('[A-Z]{1}', '', s)
	if z[1] == 1:
		return False
	ii = 0
	lady_bug_histo = {}
	while ii < len(s):
		if not s[ii].isalpha():
			ii += 1
			continue
		try:
			lady_bug_histo[s[ii]] += 1
		except KeyError:
			lady_bug_histo[s[ii]] = 1
		ii += 1
	if 1 in lady_bug_histo.values():
		return False
	return True

def are_all_ladybugs_happy(s):
	if not happiness_possible(s):
		return False
	if len(s) == 0:
		return True
	if not re.search('[A-Z]', s):
		return True
	if re.search('_', s):
		s = ''.join(map(str, sorted(s)))
		s = s.strip('_')
	ii = 0
	while ii < len(s) - 1:
		if s[ii] == s[ii + 1]:
			ii += 1
			continue
		if s[ii] != s[ii + 1]:
			if ii == 0 or s[ii] != s[ii - 1]:
				return False
		ii += 1
	return True
Q = int(input().strip())
test_case = 0
while test_case < Q:
	n = int(input().strip())
	b = input().strip()
	if are_all_ladybugs_happy(b):
		print('YES')
	else:
		print('NO')
	test_case += 1


def valid(chars, n):
	if n > 1 and chars[0] == chars[1]:
		for i in range(1, n - 1):
			if chars[i] not in [chars[i - 1], chars[i + 1]]:
				return False
		if chars[n - 1] != chars[n - 2]:
			return False
		return True
	return False
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	b = [i for i in b]
	if '_' not in b:
		if valid(b, n):
			print('YES')
		else:
			print('NO')
	else:
		b = [i for i in b if i != '_']
		k = 0
		while b:
			if b.count(b[0]) == 1:
				k = 1
				break
			b = [i for i in b if i != b[0]]
		if k == 0:
			print('YES')
		else:
			print('NO')


import sys
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	hasEmpty = False
	isJumbled = False
	colourFreq = {}
	for i in range(0, n):
		if b[i] == '_':
			hasEmpty = True
			continue
		if colourFreq.get(b[i], None) is None:
			colourFreq[b[i]] = 1
		else:
			colourFreq[b[i]] = 2
		if not isJumbled and ((i - 1 < 0 or b[i - 1] != b[i]) and (i + 1 >= n or b[i + 1] != b[i])):
			isJumbled = True
	if len(colourFreq.values()) > 0 and (min(colourFreq.values()) == 1 or (isJumbled and (not hasEmpty))):
		print('NO')
	else:
		print('YES')


import sys

def happy(b, index):
	ladybug = b[index]
	if ladybug == '_':
		return 0
	hap = 0
	if index - 1 >= 0 and ladybug == b[index - 1]:
		hap = 1
	if index + 1 < len(b) and ladybug == b[index + 1]:
		hap = 1
	return hap
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = list(input().strip())
	colors = {}
	spaces = 0
	happy_count = 0
	all_happy = 'YES'
	for index in range(len(b)):
		happy_count += happy(b, index)
		ladybug = b[index]
		if ladybug == '_':
			spaces += 1
		elif ladybug in colors:
			colors[ladybug] += 1
		else:
			colors[ladybug] = 1
	if happy_count + spaces == len(b):
		all_happy = 'YES'
	elif spaces == 0:
		all_happy = 'NO'
	else:
		for (color, cnt) in colors.items():
			if cnt < 2:
				all_happy = 'NO'
	print(all_happy)


import sys

def contains_unhappy(b):
	for (index, tile) in enumerate(b):
		if tile != '_':
			match_count = 0
			if index > 0 and b[index - 1] == tile:
				match_count += 1
			if index < len(b) - 1 and b[index + 1] == tile:
				match_count += 1
			if match_count == 0:
				return True
	return False

def can_be_made_happy(b):
	counts = {}
	for tile in b:
		counts[tile] = counts.get(tile, 0) + 1
	print(counts, file=sys.stderr)
	return counts.get('_', 0) > 0 and all((v != 1 for (k, v) in counts.items() if k != '_'))
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	print("'{}' contains unhappy: {}".format(b, contains_unhappy(b)), file=sys.stderr)
	if not contains_unhappy(b) or can_be_made_happy(b):
		print('YES')
	else:
		print('NO')


import sys
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	mar = {}
	free = False
	for i in range(len(b)):
		if b[i] != '_':
			if b[i] in mar:
				mar[b[i]] += 1
			else:
				mar[b[i]] = 1
		else:
			free = True
	ok = True
	for i in mar:
		if mar[i] <= 1:
			ok = False
			break
	if free and ok:
		print('YES')
	else:
		order = True
		for i in range(len(b)):
			left = True
			right = True
			if i - 1 < 0:
				left = False
			if i + 1 >= len(b):
				right = False
			if left:
				if b[i - 1] == b[i]:
					continue
			if right:
				if b[i + 1] == b[i]:
					continue
			order = False
			break
		if order:
			print('YES')
		else:
			print('NO')


import sys
from collections import Counter
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	t = list(b)
	z = Counter(t)
	found = False
	_found = True
	for key in z:
		if key != '_' and z[key] == 1:
			found = True
		if key == '_':
			_found = False
	if found:
		print('NO')
	elif _found:
		for x in range(1, len(b) - 1):
			if b[x] != b[x + 1] and b[x] != b[x - 1]:
				found = True
				break
		if found:
			print('NO')
		else:
			print('YES')
		continue
	else:
		print('YES')


import sys

def get_count(b):
	count = [0] * 27
	for ch in b:
		if ch == '_':
			count[26] += 1
		else:
			v = ord(ch) - ord('A')
			count[v] += 1
	return count

def is_happy(b):
	n = len(b)
	for i in range(n):
		if b[i] == '_':
			continue
		if i == 0 and b[i] != b[i + 1]:
			return False
		elif i == n - 1 and b[i - 1] != b[i]:
			return False
		elif b[i - 1] != b[i] and b[i] != b[i + 1]:
			return False
	return True

def can_make_happy(b):
	count = get_count(b)
	for i in range(26):
		if count[i] == 1:
			return False
	if count[26] > 0:
		return True
	return is_happy(b)
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	if can_make_happy(b):
		print('YES')
	else:
		print('NO')


import sys
output = []
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	if b.find('_') == -1:
		curr = b[0]
		h_flag = 0
		for i in range(1, n):
			if b[i] == curr:
				h_flag = 1
			elif h_flag == 1:
				curr = b[i]
				h_flag = 0
			else:
				break
		output.append('YES' if h_flag else 'NO')
	else:
		colors = dict()
		for col in b:
			if col == '_':
				continue
			if col in colors:
				colors[col] += 1
			else:
				colors[col] = 1
		output.append('NO' if any([colors[col] == 1 for col in colors]) else 'YES')
print('\n'.join(output))


import sys
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = list(input().strip())
	d = {}
	fl = '_' in b
	for i in b:
		if i != '_':
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
	flag = 0
	for i in d:
		if d[i] == 1:
			flag = 1
			break
	if flag:
		print('NO')
	elif fl:
		print('YES')
	else:
		for i in range(n):
			if i == 0:
				if b[i] != b[i + 1]:
					flag = 1
					break
			if i == n - 1:
				if b[i] != b[i - 1]:
					flag = 1
					break
			if b[i] != b[i - 1] and b[i] != b[i + 1]:
				flag = 1
				break
		if flag:
			print('NO')
		else:
			print('YES')


import sys
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	fieldList = list(b)
	uniqueFields = set(fieldList)
	if uniqueFields == set(['_']):
		print('YES')
		continue
	uniqueFields.discard('_')
	fieldCounts = [fieldList.count(f) for f in uniqueFields]
	if 1 in fieldCounts:
		print('NO')
		continue
	if '_' not in fieldList:
		isNeighborSame = []
		for i in range(len(fieldList)):
			if i != 0 and fieldList[i] == fieldList[i - 1]:
				isNeighborSame.append(1)
			elif i != len(fieldList) - 1 and fieldList[i] == fieldList[i + 1]:
				isNeighborSame.append(1)
			else:
				isNeighborSame.append(0)
		if 0 in isNeighborSame:
			print('NO')
			continue
		else:
			print('YES')
			continue
	print('YES')


import sys
from collections import Counter
Q = int(input().strip())
for a0 in range(Q):
	n = int(input().strip())
	b = input().strip()
	c = Counter(b)
	if c['_'] == 0:
		ok = True
		for i in range(1, n - 1):
			if b[i].isupper() and b[i] != b[i - 1] and (b[i] != b[i + 1]):
				ok = False
				break
		if b[0].isupper() and (len(b) < 2 or b[0] != b[1]) or (b[-1].isupper() and (len(b) < 2 or b[-1] != b[-2])):
			ok = False
		print('YES' if ok else 'NO')
	else:
		print('YES' if all((x == '_' or c[x] > 1 for x in c)) else 'NO')


import sys

def isPlayable(num, val):
	count = 0
	for l in val:
		if l == '_':
			count += 1
	return count > 0

def isMatching(val):
	if '-' in val:
		return False
	elif len(val) == 0:
		return True
	elif len(val) == 1:
		return False
	end = True
	for i in range(len(val) - 2):
		if val[i + 1] != val[i] and val[i + 1] != val[i + 2]:
			end = False
	if val[0] != val[1] or val[-1] != val[-2]:
		end = False
	return end

def sortMatching(val):
	a = [l for l in val if l != '_']
	a.sort()
	return isMatching(a)

def getAns(num, val):
	if not isPlayable(num, val):
		return isMatching(val)
	else:
		return sortMatching(val)
Q = int(input().strip())
n = []
b = []
for a0 in range(Q):
	n.append(int(input().strip()))
	b.append(input().strip())
for a1 in range(Q):
	if getAns(n[a1], b[a1]):
		print('YES')
	else:
		print('NO')


g = int(input())
for game in range(g):
	n = int(input())
	b = input()
	u = b.count('_')
	happy = {'_': True}
	if u == 0:
		prior = '_'
		for bug in b:
			if bug != prior:
				if any((not happy[bug] for bug in happy)):
					break
				happy[bug] = False
				prior = bug
			else:
				happy[bug] = True
	else:
		for bug in b:
			if bug in happy:
				happy[bug] = True
			else:
				happy[bug] = False
	print('YES' if all((happy[bug] for bug in happy)) else 'NO')