let dividable lst =
  let rec iter (x, y) = function
    | [] -> true
    | hd :: tl when hd < x && hd < y -> false
    | hd :: tl when hd < x && hd > y -> iter (x, hd) tl
    | hd :: tl when hd > x && hd < y -> iter (hd, y) tl
    | hd :: tl when x > y -> iter (hd, y) tl
    | hd :: tl  -> iter (x, hd) tl
  in iter (0, 0) lst
                                             
let _ = 
  let n = read_int () in
  for i = 1 to n do
    let lst = List.map int_of_string @@ Str.split (Str.regexp " ") (read_line ()) in
    let rt = if dividable lst then "YES" else "NO" in
    print_endline rt
  done