#![allow(non_snake_case, unused_must_use, unused_imports)] use std::io::{self, prelude::*}; fn main() { let (stdin, stdout) = (io::read_to_string(io::stdin()).unwrap(), io::stdout()); let (mut stdin, mut buffer) = (stdin.split_whitespace(), io::BufWriter::new(stdout.lock())); for _ in 0..stdin.next().unwrap().parse::().unwrap() { solve(&mut stdin, &mut buffer); } } fn solve(stdin: &mut std::str::SplitWhitespace, buffer: &mut io::BufWriter) { macro_rules! input { ($t: tt, $n: expr) => { (0..$n).map(|_| input!($t)).collect::>() }; (Chars) => { input! {String}.chars().collect::>() }; (Usize1) => { stdin.next().unwrap().parse::().unwrap() - 1 }; ($t: ty) => { stdin.next().unwrap().parse::<$t>().unwrap() }; } let D = input!(isize); let mut x = input!(usize); let mut y = input!(usize); if y > x { std::mem::swap(&mut x, &mut y); } let g = gcd(x, y); let v = (x / g, y / g); let x = x as isize; let y = y as isize; let dx = -(v.1 as isize); let dy = v.0 as isize; let mut ok = 0; let mut ng = 1 << 30; while ng - ok > 1 { let m = (ok + ng) / 2; let p = x + dx * m as isize; let q = y + dy * m as isize; if 0 <= p && p <= D && 0 <= q && q <= D { ok = m; } else { ng = m; } } let p = x + dx * ok as isize; let q = y + dy * ok as isize; let ans = isize::abs_diff(p * y, x * q); writeln!(buffer, "{}", ans); } fn gcd(a: usize, b: usize) -> usize { if a < b { return gcd(b, a); } if b == 0 { return a; } else { return gcd(b, a % b); } }