for _ in range(int(input())): n = int(input()) A = list(map(int, input().split())) if n == 2: if A == [1, 0] or A == [0, 1]: print("Yes") else: print("No") else: zero = False two = False f = -1 for i in range(n): if A[i] == 0: f = i break if f == -1: print("No") continue A = A[f:] + A[:f] flag = True zero = True two = False for i in range(1, n): a = A[i] if a == 0: if not two: flag = False break two = False if zero: flag = False break else: zero = True elif a == 1: zero = False elif a == 2: zero = False if two: flag = False break else: two = True if A[0] == A[-1]: flag = False if flag: print("Yes") else: print("No")