# 抄的 # 思路:随便找一个 2 的位置,循环往后移动n,确定每个位置与其后面位置大小的相关性,然后和 a 进行检查 import sys, time, random from collections import deque, Counter, defaultdict input = lambda: sys.stdin.readline().rstrip() ii = lambda: int(input()) mi = lambda: map(int, input().split()) li = lambda: list(mi()) inf = 2 ** 63 - 1 mod = 998244353 def solve(): n = ii() a = li() if 2 not in a: return 'No' p = a.index(2) d = [None] * n for i in range(p, p + n): i %= n to = None if a[i] == 2: to = '>' if a[i] == 0: to = '<' if a[i] == 1: to = d[(i - 1) % n] d[i] = to for i in range(n): if a[i] != (d[(i - 1) % n] == '<') + (d[i % n] == '>'): return 'No' return 'Yes' for _ in range(ii()): print(solve())