def is_kadomatsu(a, b, c): if a == b or b == c or a == c: return False if (b > a and b > c) or (b < a and b < c): return True return False def solve(): import sys 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 invalid_poses = [] for i in range(N - 2): a, b, c = A[i], A[i+1], A[i+2] if not is_kadomatsu(a, b, c): invalid_poses.append(i) K = len(invalid_poses) if K > 4: print("No") continue candidate_pos = set() for pos in invalid_poses: for j in range(3): if pos + j < N: candidate_pos.add(pos + j) positions = list(candidate_pos) candidates = set() for x in positions: for y in range(N): if x == y: continue if A[x] == A[y]: continue if x < y: candidates.add((x, y)) else: candidates.add((y, x)) for x in range(N): for y in positions: if x == y: continue if A[x] == A[y]: continue if x < y: candidates.add((x, y)) else: candidates.add((y, x)) found = False for x, y in candidates: if x >= N or y >= N: continue if A[x] == A[y]: continue A[x], A[y] = A[y], A[x] affected = set() for pos in [x-2, x-1, x]: if 0 <= pos <= N-3: affected.add(pos) for pos in [y-2, y-1, y]: if 0 <= pos <= N-3: affected.add(pos) for pos in invalid_poses: affected.add(pos) ok = True for pos in affected: if pos < 0 or pos > N-3: continue a, b, c = A[pos], A[pos+1], A[pos+2] if not is_kadomatsu(a, b, c): ok = False break if ok: found = True A[x], A[y] = A[y], A[x] break A[x], A[y] = A[y], A[x] print("Yes" if found else "No") if __name__ == "__main__": solve()