use proconio::input; fn main() { input! { t: usize } for _ in 0..t { solve(); } } fn solve() { input! { h: i32, w: i32 } let mut current = (1, 1); for _ in 0..3 { input! { x: i32, y: i32 } assert_ne!((x, y), (-1, -1)); if (x, y) == (0, 0) { return; } let cand = if x == h { [ (x - 1, y), (x - 1, current.1), (x - 1, current.1 + 1), (x - 1, current.1 - 1), ] } else if y == w { [ (x, y - 1), (current.0, y - 1), (current.0 + 1, y - 1), (current.0 - 1, y - 1), ] } else { [ (x - 1, y), (x - 1, current.1), (x - 1, current.1 + 1), (x - 1, current.1 - 1), ] }; let (ni, nj) = cand .iter() .copied() .find(|&next| { (1..=h).contains(&next.0) && (1..=w).contains(&next.1) && current != next && check(current, next) }) .unwrap(); println!("{ni} {nj}"); current = (ni, nj); } input! { x: i32, y: i32 } assert_ne!((x, y), (-1, -1)); if (x, y) == (0, 0) { return; } unreachable!() } fn check(now: (i32, i32), prev: (i32, i32)) -> bool { now.0 == prev.0 || now.1 == prev.1 || now.0 + now.1 == prev.0 + prev.1 || now.0 + prev.1 == prev.0 + now.1 }