def RLE(s): result = [] count = 1 for i in range(1, len(s)): if s[i] == s[i - 1]: count += 1 else: result.append((s[i - 1], count)) count = 1 result.append((s[-1], count)) return result T = int(input()) for _ in range(T): N = int(input()) A = "".join(input().split()) cnt1 = A.count("1") cnt0 = A.count("0") rle = RLE(A) if cnt1 > cnt0: rle1 = len(rle[0::2]) if rle[0][0] == "1" else len(rle[1::2]) if cnt0 >= rle1: print("Yes") else: print("No") elif cnt1 < cnt0: rle0 = len(rle[0::2]) if rle[0][0] == "0" else len(rle[1::2]) if cnt1 >= rle0: print("Yes") else: print("No") else: print("Yes")