use proconio::{fastout, input}; #[fastout] fn main() { input! { x: u32, y: u32, n: usize, moving: [(u64, u64); n], } println!("{}", output(solve(x, y, moving))); } fn solve(_x: u32, y: u32, moving: Vec<(u64, u64)>) -> Vec { let mut ans = Vec::with_capacity(moving.len()); for (u, v) in moving { if u == 0 { ans.push((v - 1) % y as u64 + 1); } else if (u - 1) / y as u64 == (v - 1) / y as u64 { ans.push(v - u); } else { ans.push((u - 1) % y as u64 + (v - 1) % y as u64 + 2); } } ans } fn output(ans: Vec) -> String { ans.into_iter() .map(|x| x.to_string()) .collect::>() .join("\n") }