for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) a.append(a[0]) ok1 = True ok2 = False ok3 = False s = 0 for i in range(n): if a[i] == a[i + 1] != 1: ok1 = False if a[i] == 0: ok2 = True if a[i] == 2: ok3 = True s += a[i] print("Yes" if ok1 and ok2 and ok3 and s == n else "No")