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] == 2: f = i break if f == -1: print("No") continue A = A[f:] + A[:f] A += A idx = 0 flag = True while True: if A[idx] == 2: zero = False if two: flag = False break else: two = True elif A[idx] == 1: zero = False elif A[idx] == 0: if not two: flag = False break two = False if zero: flag = False break else: zero = True if idx >= n: break idx += 1 if idx == 2 * n: flag = False break if flag: print("Yes") else: print("No")