T=int(input()) for _ in range(T): N=int(input()) A=list(map(int, input().split())) f=1 if A.count(0)==0 or A.count(2)==0 or sum(A)!=N: f=0 for i in range(N): if A[i-1]==A[i] and A[i]!=1: f=0 if f==1: print('Yes') else: print('No')