import sys def solve(n, a): left = [False] * n right = [False] * n idx2 = -1 for i in range(n): if a[i] == 2: idx2 = i break if idx2 == -1: return False left[idx2] = True right[idx2] = True for i in range(idx2 + 1, idx2 + n): j = i % n left[j] = not right[j-1] r = a[j] - left[j] if r < 0 or r > 1: return False else: right[j] = (r == 1) return left[idx2] and not right[idx2-1] or not left[idx2] and right[idx2-1] t = int(input()) for _ in range(t): n = int(input()) a = [int(x) for x in input().split()] print("Yes" if solve(n, a) else "No")