pub mod input { use std::io::{BufRead, ErrorKind}; pub trait ReadBytes { fn read_bytes(&mut self) -> Vec; } impl ReadBytes for R { #[inline] fn read_bytes(&mut self) -> Vec { let mut res = Vec::new(); loop { let buf = match self.fill_buf() { Ok(buf) => buf, Err(e) if e.kind() == ErrorKind::Interrupted => continue, Err(e) => panic!("{}", e), }; let (done, used, buf) = match buf.iter().position(u8::is_ascii_whitespace) { Some(i) => (i > 0 || res.len() > 0, i + 1, &buf[..i]), None => (buf.is_empty(), buf.len(), buf), }; res.extend_from_slice(buf); self.consume(used); if done { return res; } } } } #[macro_export] macro_rules! read { ($r:expr, [$t:tt; $n:expr]) => ((0..$n).map(|_| read!($r, $t)).collect::>()); ($r:expr, [$t:tt]) => (read!($r, [$t; read!($r, usize)])); ($r:expr, ($($t:tt),*)) => (($(read!($r, $t)),*)); ($r:expr, Usize1) => (read!($r, usize) - 1); ($r:expr, String) => (String::from_utf8(read!($r, Bytes)).unwrap()); ($r:expr, Bytes) => ($r.read_bytes()); ($r:expr, $t:ty) => (read!($r, String).parse::<$t>().unwrap()); } #[macro_export] macro_rules! input { ($r:expr, $($($v:ident)* : $t:tt),* $(,)?) => { $(let $($v)* = read!($r, $t);)* }; } } use input::ReadBytes; fn run(reader: &mut R, writer: &mut W) { for _ in 0..read!(reader, usize) { let (x, a) = read!(reader, (u64, u64)); let f = |n: u64| { let p = (a / n).pow(2) * (n - a % n); let q = (a / n + 1).pow(2) * (a % n); p + q + n * x }; let (mut left, mut right) = (1, a); while left + 2 < right { let l = left + (right - left) / 3; let r = right - (right - left) / 3; if f(l) < f(r) { right = r; } else { left = l; } } let ans = (left..=right).map(|n| f(n)).min().unwrap(); writeln!(writer, "{}", ans).ok(); } } fn main() { let (stdin, stdout) = (std::io::stdin(), std::io::stdout()); let mut reader = std::io::BufReader::new(stdin.lock()); let mut writer = std::io::BufWriter::new(stdout.lock()); run(&mut reader, &mut writer); }