import itertools as iter import collections as coll import heapq as hq import bisect as bis from decimal import Decimal as dec from functools import cmp_to_key import math import sys #import pypyjit #pypyjit.set_param('max_unroll_recursion=-1') sys.setrecursionlimit(10 ** 6) inp = sys.stdin.readline input = lambda : inp()[:-1] getN = lambda : int(inp()) getNs = lambda : map(int, inp().split()) getList = lambda :list(map(int, inp().split())) getStrs = lambda n : [input() for _ in [0] * n] getEdges = lambda n : [[x - 1 for x in getNs()] for _ in [0] * n] def yexit(): print("Yes"); exit(0) def nexit(): print("No"); exit(0) pi = 3.141592653589793 mod = 1000000007 MOD = 998244353 INF = 4611686018427387903 dx = [1, 0, -1, 0]; dy = [0, 1, 0, -1] #di = coll.defaultdict(int) """ Main Code """ def solve(n, a): if(2 not in a): return "No" id_start = a.index(2) dirs = [0] * n for i in range(n): j = (id_start + i) % n k = (j - 1) % n if(a[j] == 2): dirs[j], dirs[k] = 1, 0 elif(a[j] == 1): dirs[j] = dirs[k] else: dirs[j], dirs[k] = 0, 1 for i in range(n): j = (id_start + i) % n k = (j - 1) % n if(a[j] == 2 and (dirs[j], dirs[k]) != (1, 0)): return "No" if(a[j] == 1 and dirs[j] != dirs[k]): return "No" if(a[j] == 0 and (dirs[j], dirs[k]) != (0, 1)): return "No" return "Yes" t = getN() for _ in [0] * t: n = getN() a = getList() print(solve(n, a))