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] if d[i] is not None and d[i] != to: return 'No' 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())