T = gets.to_i def is_kadomatsu(arr) arr.each_cons(3).each do |a, b, c| return false if a == b return false if a == c return false if b == c return false if a < b && b < c return false if a > b && b > c end true end T.times do n = gets.to_i arr = gets.split.map(&:to_i) idx = -1 1.upto(n - 2) do |i| a = arr[i - 1] b = arr[i] c = arr[i + 1] if a == b || a == c || b == c idx = i break end next if a < b && b > c next if a > b && b < c idx = i break end ok = false (idx - 1..[idx + 2, n - 2].min).each do |i| arr[i], arr[i + 1] = arr[i + 1], arr[i] if is_kadomatsu(arr) ok = true end arr[i], arr[i + 1] = arr[i + 1], arr[i] end if ok puts 'Yes' else puts 'No' end end