import sys def solve(n, a): larger = [set() for _ in range(n)] for i in range(n): if a[i] == 0: if (i - 1) % n in larger[i]: print("contradiction", file=sys.stderr) return False if (i + 1) % n in larger[i]: print("contradiction", file=sys.stderr) return False larger[(i-1)%n].add(i) larger[(i+1)%n].add(i) elif a[i] == 2: if i in larger[(i-1)%n]: print("contradiction", file=sys.stderr) return False if i in larger[(i+1)%n]: print("contradiction", file=sys.stderr) return False larger[i].add((i - 1) % n) larger[i].add((i + 1) % n) for i in range(n): if a[i] == 1: if (i - 1) % n in larger[i]: larger[(i+1)%n].add(i) if (i + 1) % n in larger[i]: larger[(i-1)%n].add(i) print(larger, file=sys.stderr) 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")