t=int(input()) for _ in range(t): n=int(input()) a=list(map(int,input().split())) if (0 not in a) or (2 not in a): print("No") exit() i=0 while a[i]!=0: i+=1 a=a[i:]+a[:i] c=[""]*(n+1) c[0]=">" c[-1]="<" f=1 for i in range(1,n): if a[i]==0: c[i]=">" if c[i-1]!="<" and f: print("No") f=0 if a[i]==1: if c[i-1]=="<": c[i]="<" if c[i-1]==">": c[i]=">" if a[i]==2: c[i]="<" if c[i-1]!=">" and f: print("No") f=0 if f: print(["No","Yes"][c[-1]==c[n-1]])