def solve(N,A): last=A[0] lis=[] streak=0 for i in range(N): if last!=A[i]: lis.append(streak) streak=1 last=A[i] else: streak+=1 lis.append(streak) if A.count(1)==0 or A.count(0)==0: return False if len(lis)%2==1 and lis[1:-1].count(1)==len(lis)-2: return False return True T=int(input()) for _ in range(T): N=int(input()) A=list(map(int,input().split())) if solve(N,A): print('Yes') else: print('No')