import sys def is_valid_triple(a, b, c): if a == b or b == c or a == c: return False mid = b return (mid > a and mid > c) or (mid < a and mid < c) def is_valid_sequence(arr): n = len(arr) for i in range(n - 2): if not is_valid_triple(arr[i], arr[i+1], arr[i+2]): return False return True def can_make_valid(arr): n = len(arr) problem_indices = [] for i in range(n - 2): if not is_valid_triple(arr[i], arr[i+1], arr[i+2]): problem_indices.append(i) if not problem_indices: return True from copy import deepcopy for k in problem_indices: a, b, c = arr[k], arr[k+1], arr[k+2] for swap in [(k, k+1), (k, k+2), (k+1, k+2)]: i, j = swap if arr[i] == arr[j]: continue temp = deepcopy(arr) temp[i], temp[j] = temp[j], temp[i] affected = set() for pos in [i, j]: if pos >= 2: affected.add(pos - 2) if pos <= n - 3: affected.add(pos) if pos <= n - 3: affected.add(pos + 1) if pos <= n - 3: affected.add(pos + 2) valid = True for pos in affected: if pos >= 0 and pos <= n - 3: x, y, z = temp[pos], temp[pos+1], temp[pos+2] if not is_valid_triple(x, y, z): valid = False break if valid and is_valid_sequence(temp): return True return False def main(): input = sys.stdin.read().split() ptr = 0 T = int(input[ptr]) ptr += 1 for _ in range(T): N = int(input[ptr]) ptr += 1 A = list(map(int, input[ptr:ptr+N])) ptr += N if can_make_valid(A): print("Yes") else: print("No") if __name__ == '__main__': main()