t=int(input()) for _ in range(t): _=input() a=''.join(input().split()) res='Yes' if a[0]==a[-1]: aa=['11','00'][int(a[0])] if aa not in a: res='No' print(res)