There is a container with two openings, B and C. Ten balls numbered 1 to 10 are dropped from opening A and placed into either B or C. A board, D, can rotate around pivot E to determine which tube to drop the balls into. Given the sequence of dropped balls, determine if both tubes can stack the balls with smaller numbers on top of larger ones. Output YES if possible, NO if not. Assume the order of the balls cannot be changed within the container and that both tubes have enough space for all 10 balls.

Input:
Multiple datasets are given. The first line contains the dataset count, N. The following N lines contain 10 numbers each, separated by spaces.

Output:
For each dataset, output YES or NO on a separate line.

Sample Input:
2
3 1 4 2 5 6 7 8 9 10
10 9 8 7 6 5 4 3 2 1

Output for the Sample Input:
YES
NO

# Buggy version:
n = int(input())
for i in range(n):
    arr = map(int, input().split())
    b,c = 0,0
    flag = True
    for i in range(len(arr)):
        if b < arr[i]:
            b = arr[i]
            continue
        if c < arr[i]:
            c = arr[i]
            continue
        print("NO")
        flag = False
        break
    if flag:
        print("YES")

# Fixed version:
n = int(input())
for i in range(n):
    str = input().split()
    arr = [int(str[j]) for j in range(len(str))]
    b,c = 0,0
    flag = True
    for i in range(len(arr)):
        if b < arr[i]:
            b = arr[i]
            continue
        if c < arr[i]:
            c = arr[i]
            continue
        print("NO")
        flag = False
        break
    if flag:
        print("YES")

# Buggy version:
n = int(input())
for i in range(n):
    left = right = []
    arr = map(int, input().split())
    for a in arr:
        if len(left) == 0 or left[-1] < a:
            left.append(a)
        elif len(right) == 0 or right[-1] < a:
            right.append(a)
        else:
            break
    if len(left) + len(right) == 10:
        print("YES")
    else:
        print("NO")

# Fixed version:
n = int(input())
for i in range(n):
    left, right = [], []
    arr = map(int, input().split())
    for a in arr:
        if len(left) == 0 or left[-1] < a:
            left.append(a)
        elif len(right) == 0 or right[-1] < a:
            right.append(a)
        else:
            break
    if len(left) + len(right) == 10:
        print("YES")
    else:
        print("NO")

# Buggy version:
ans=0
def solve(n,left,right):
    if n==10:
        ans=1
    else:
        if a[n]>left:
            ans=solve(n+1,a[n],right)
        if a[n]>right:
            ans=solve(n+1,left,a[n])
    return ans

n=int(input())
for _ in range(n):
    a=[int(i) for i in input().split()]
    print("YES" if solve(0,0,0) else "NO")
    

# Fixed version:
ans=0
def solve(n,left,right):
    global ans
    if n==10:
        ans=1
    else:
        if a[n]>left:
            ans=solve(n+1,a[n],right)
        if a[n]>right:
            ans=solve(n+1,left,a[n])
    return ans

n=int(input())
for _ in range(n):
    a=[int(i) for i in input().split()]
    print("YES" if solve(0,0,0) else "NO")
    ans=0

# Buggy version:
import sys

def solve(balls):
    ans = distribute(balls, [], [])
    if ans is True: print('YES')
    else: print("NO")
        
def distribute(balls, R, L):
    if len(balls) != 0:
        next = balls[0]
        ans = False
        #case R
        if isMutch(next, R):
            neoR = R
            neoR.append(next)
            ans = distribute(balls[1:], neoR, L)
        if isMutch(next, L):
            neoL = L
            neoL.append(next)
            ans =distribute(balls[1:], R, neoL)
        if ans: return True
    else:
        return isOrdered(R) and isOrdered(L)

def isMutch(next, lis):
    if len(lis) != 0:
        return next >= lis[len(lis)-1]
    return True
    
def isOrdered(lis):
    #check both R and L are ordered
    checker = sorted(lis)
    return checker == lis
  
limit = 2**11
sys.setrecursionlimit(limit)
  
line = input()
size = -1;
while True:
    if size == -1:
        size = int(line)
    else:
        solve(line.split(' '))
        size -= 1
        print(size)
        if size == 0: break
    line = input()

# Fixed version:
import sys

def solve(balls):
    ans = distribute(balls, [], [])
    if ans is True: print('YES')
    else: print("NO")
        
def distribute(balls, R, L):    
    if len(balls) != 0:
        next = balls[0]
        ans = False
        #case R
        if isMutch(next, R):
            neoR = R
            neoR.append(next)
            ans = distribute(balls[1:], neoR, L)
        if ans: return True
        
        if isMutch(next, L):
            neoL = L
            neoL.append(next)
            ans =distribute(balls[1:], R, neoL)
        if ans: return True
    else:
        return isOrdered(R) and isOrdered(L)

def isMutch(next, lis):
    if len(lis) != 0:
        return next >= lis[len(lis)-1]
    return True
    
def isOrdered(lis):
    #check both R and L are ordered
    checker = sorted(lis)
    return checker == lis
  
limit = 2**11
sys.setrecursionlimit(limit)
  
line = input()
size = -1;
while True:
    if size == -1:
        size = int(line)
    else:
        balls = []
        for a in line.split(' '):
            balls.append(int(a))   
        solve(balls)
        size -= 1
        if size == 0: break
    line = input()

# Buggy version:
N = int(input())

for testCase in range(N):
    A = list(map(int, input().split()))
    ok = False
    for i in range(2 ** 10):
        x = []
        y = []
        for j in range(10):
            print(j)
            if i & (1 << j):
                x.append(A[j])
            else:
                y.append(A[j])
        if x == sorted(x) and y == sorted(y):
            ok = True
    if ok:
        print('YES')
    else:
        print('NO')

# Fixed version:
N = int(input())

for testCase in range(N):
    A = list(map(int, input().split()))
    ok = False
    for i in range(2 ** 10):
        x = []
        y = []
        for j in range(10):
            if i & (1 << j):
                x.append(A[j])
            else:
                y.append(A[j])
        if x == sorted(x) and y == sorted(y):
            ok = True
    if ok:
        print('YES')
    else:
        print('NO')

# Buggy version:
def dfs(i, b, c):
    if i == 11:
        print(b)
        print(c)
        return (b == sorted(b) and c == sorted(c))

    if dfs(i + 1, b + [balls[i - 1]], c):
        return True

    if dfs(i + 1, b, c + [balls[i - 1]]):
        return True

    return False


if __name__ == '__main__':
    N = int(input())
    for i in range(N):
        balls = list(map(int, input().split()))
        if dfs(1, [], []):
            print("YES")
        else:
            print("NO")

# Fixed version:
def dfs(i, b, c):
    if i == 11:
        return (b == sorted(b) and c == sorted(c))

    if dfs(i + 1, b + [balls[i - 1]], c):
        return True

    if dfs(i + 1, b, c + [balls[i - 1]]):
        return True

    return False


if __name__ == '__main__':
    N = int(input())
    for i in range(N):
        balls = list(map(int, input().split()))
        if dfs(1, [], []):
            print("YES")
        else:
            print("NO")

# Buggy version:
n = int(input())
for i in range(n):
    arr = map(int, input().split())
    b,c = 0,0
    for i in range(len(arr)):
        if b < arr[i]:
            b = arr[i]
            continue
        if c < arr[i]:
            c = arr[i]
            continue
        print("NO")
        break
    else:
        print("YES")

# Fixed version:
n = int(input())
for i in range(n):
    a = list(map(int, input().split()))
    b,c = 0,0
    for i in range(len(a)):
        if b < a[i]:
            b = a[i]
            continue
        if c < a[i]:
            c = a[i]
            continue
        print("NO")
        break
    else:
        print("YES")

# Buggy version:
def ball(nums):
????????b,c = 0,0
    for i in range(len(a)):
        if b < a[i]:
            b = a[i]
            continue
        if c < a[i]:
            c = a[i]
            continue
        return "NO"
    return "YES"
??
N = int(input())
??
for _ in range(N):
????????a = list(map(int, input().split()))
????????print(ball(a))

# Fixed version:
def ball(nums):
    b, c = 0, 0
    for i in range(len(nums)):
        if b < nums[i]:
            b = nums[i]
            continue
        if c < nums[i]:
            c = nums[i]
            continue
        return "NO"
    return "YES"

N = int(input())

for _ in range(N):
    a = list(map(int, input().split()))
    print(ball(a))

# Buggy version:
def main():
    n = int(input())
    for i in range(n):
        Ball = map(int,input().split())
        l = r = 0
        for value in Ball:
            if value > r:
                r = value
            elif value > l:
                l = value
            else:
                print("NO")
                r = -777
                break
            if r < l:
                r,l = l,r
            
        if r == -777:
            continue
        else:
            print("Yes")

if __name__ == '__main__':
    main()



# Fixed version:
def main():
    n = int(input())
    for i in range(n):
        Ball = map(int,input().split())
        l = r = 0
        for value in Ball:
            if value > r:
                r = value
            elif value > l:
                l = value
            else:
                print("NO")
                r = -777
                break
            if r < l:
                r,l = l,r
            
        if r == -777:
            continue
        else:
            print("YES")

if __name__ == '__main__':
    main()

# Buggy version:
x=int(input())
for _ in range(x):
	num=list(map(int,input().split()))
	b,c=0,0
	flag=True
	for d in num:
		if b < d:
			b=i
		elif c<i:
			c=i
		else:
			print("NO")
			flag=False
			break
	if flag:
		print("YES")


# Fixed version:
x=int(input())
for _ in range(x):
	num=list(map(int,input().split()))
	b,c=0,0
	flag=True
	for i in num:
		if b<i:
			b=i
		elif c<i:
			c=i
		else:
			print("NO")
			flag=False
			break
	if flag:
		print("YES")

# Buggy version:
n=int(input())
for i in range(n):
    a=list(map(int,input().aplit()))
    b,c=0,0
    yes = True
    for j in range(10):
        if a[j]>b:b=a[j]
        elif a[j]>c:c=a[j]
        else:yes=False
    print("YES" if yes else "NO")


# Fixed version:
n = int(input())
for i in range(n):
    a = list(map(int, input().split()))
    b = c = 0
    yes = True
    for j in range(10):
        if a[j] > b: b = a[j]
        elif a[j] > c: c = a[j]
        else: yes = False
    print('YES' if yes else 'NO')