import sys def is_valid_triplet(a, b, c): if a == b or b == c or a == c: return False return b == max(a, b, c) or b == min(a, b, c) 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 bad = [] for i in range(N-2): a, b, c = A[i], A[i+1], A[i+2] if a == b or b == c or a == c: bad.append(i) continue if not (b == max(a, b, c) or b == min(a, b, c)): bad.append(i) if not bad: print("Yes") continue found = False for j in bad: for dx in range(3): if dx == 0: x = j y = j+1 elif dx == 1: x = j+1 y = j+2 else: x = j y = j+2 if x >= N or y >= N: continue if x == y: continue x_min = max(0, min(x, y) - 2) x_max = min(N-3, max(x, y) + 2) all_in = True for j0 in bad: if not (x_min <= j0 <= x_max): all_in = False break if not all_in: continue all_fixed = True for j0 in bad: i = j0 a, b, c = A[i], A[i+1], A[i+2] if x in [i, i+1, i+2]: if x == i: a = A[y] elif x == i+1: b = A[y] else: c = A[y] if y in [i, i+1, i+2]: if y == i: a = A[x] elif y == i+1: b = A[x] else: c = A[x] if not is_valid_triplet(a, b, c): all_fixed = False break if not all_fixed: continue all_valid = True for i in range(x_min, x_max + 1): a, b, c = A[i], A[i+1], A[i+2] if x in [i, i+1, i+2] or y in [i, i+1, i+2]: if x == i: a = A[y] elif x == i+1: b = A[y] elif x == i+2: c = A[y] if y == i: a = A[x] elif y == i+1: b = A[x] elif y == i+2: c = A[x] if not is_valid_triplet(a, b, c): all_valid = False break if all_valid: found = True break if found: break print("Yes" if found else "No") if __name__ == '__main__': main()