import sys
# It's important to increase the recursion limit for recursive solutions in Python.
sys.setrecursionlimit(200005)
def solve():
"""
Solves a single test case for the beautiful array problem using recursion with memoization.
"""
try:
n = int(sys.stdin.readline())
a = list(map(int, sys.stdin.readline().split()))
except (IOError, ValueError):
return
a_sorted = sorted(a)
# Memoization table to store results of subproblems (left_a, right_a)
memo = {}
def can_solve_recursive(left_a, right_a):
# Base case: all elements have been successfully taken.
if left_a > right_a:
return True
# Check memoization table
if (left_a, right_a) in memo:
return memo[(left_a, right_a)]
# Determine how many elements have been taken from the start and end
# to find the corresponding targets in the sorted array.
elements_taken_from_start = left_a
elements_taken_from_end = n - 1 - right_a
target_min = a_sorted[elements_taken_from_start]
target_max = a_sorted[n - 1 - elements_taken_from_end]
# --- Try taking the element from the left of A ---
res = False
# Option 1: Match a[left_a] with the smallest remaining element
if a[left_a] == target_min:
if can_solve_recursive(left_a + 1, right_a):
res = True
# Option 2: Match a[left_a] with the largest remaining element
if not res and a[left_a] == target_max:
if can_solve_recursive(left_a + 1, right_a):
res = True
# --- Try taking the element from the right of A ---
# Option 3: Match a[right_a] with the smallest remaining element
if not res and a[right_a] == target_min:
if can_solve_recursive(left_a, right_a - 1):
res = True
# Option 4: Match a[right_a] with the largest remaining element
if not res and a[right_a] == target_max:
if can_solve_recursive(left_a, right_a - 1):
res = True
# Store the result and return it
memo[(left_a, right_a)] = res
return res
if can_solve_recursive(0, n - 1):
print("YES")
else:
print("NO")
def main():
"""
Main function to handle multiple test cases.
"""
try:
num_test_cases = int(sys.stdin.readline())
for _ in range(num_test_cases):
solve()
except (IOError, ValueError):
pass
if __name__ == "__main__":
main()