T = int(input()) for _ in range(T): N = int(input()) A = list(map(int,input().split())) dp = [0] * 3 for a in A: dp[a] +=1 flag = True for i in range(3): if dp[i] == 0: flag = False if not flag: print('No') continue n = 0 for i in range(N): if A[i] == 0: n = i A = A[i:] + A[:i] break now = 1 flag = True for i in range(1,N): if A[i] == 0: if now == 1: flag = False break else: now = 1 elif A[i] == 1: pass else: if now == -1: flag = False break else:now = -1 if now == 1: flag = False if flag: print('Yes') else: print('No')