use fio::*; fn solve(n: usize, mut a: Vec) -> Vec> { let mut ret = vec![vec![0; n]; n]; a.sort(); for r in 0..n - 1 { for c in 0..n - 1 { let idx = r * (n - 1) + c; let delta = a[idx] - if idx == 0 { 0 } else { a[idx - 1] }; ret[r + 1][c + 1] += delta; if r + 2 < n { ret[r + 2][(c + 1) % 2] += delta; ret[r + 2][c + 1] -= delta; } } } for r in 0..n { for c in 0..n { if r >= 2 { ret[r][c] += ret[r - 2][c]; } if c >= 2 { ret[r][c] += ret[r][c - 2]; } if r >= 2 && c >= 2 { ret[r][c] -= ret[r - 2][c - 2]; } } } ret } fn main() { let t = read::(); for _ in 0..t { let n = read::(); let a = read_vec::(); assert_eq!((n - 1) * (n - 1), a.len()); let ret = solve(n, a); for r in ret { for c in r { print!("{} ", c); } println!(); } } } mod fio { use std::{ cell::RefCell, convert::TryInto, fmt::Debug, io::{BufRead, BufWriter, StdinLock, StdoutLock, stdin, stdout}, str::FromStr, }; thread_local! { pub static STDIN: RefCell> = RefCell::new(stdin().lock()); pub static STDOUT: RefCell>> = RefCell::new(BufWriter::new(stdout().lock())); } #[allow(dead_code)] pub fn read() -> T where ::Err: Debug, { read_line().parse().unwrap() } #[allow(dead_code)] pub fn read_vec() -> Vec where ::Err: Debug, { read_line() .split_whitespace() .map(|x| x.parse().unwrap()) .collect() } #[allow(dead_code)] pub fn read_tuple() -> [T; N] where T: FromStr + Debug, ::Err: Debug, { read_vec::().try_into().unwrap() } /// whitespace at the end of the line is ignored pub fn read_line() -> String { let mut s = String::new(); STDIN.with(|cell| { cell.borrow_mut().read_line(&mut s).unwrap(); }); String::from_str(s.trim_end()).unwrap() } } #[macro_export] macro_rules! print { ($($t:tt)*) => { fio::STDOUT.with(|cell|{ use std::io::Write; write!(cell.borrow_mut(), $($t)*).unwrap() })}; } #[macro_export] macro_rules! println { ($($t:tt)*) => { fio::STDOUT.with(|cell| { use std::io::Write; writeln!(cell.borrow_mut(), $($t)*).unwrap() }) }; } #[macro_export] macro_rules! flush { () => { fio::STDOUT.with(|cell| { use std::io::Write; cell.borrow_mut().flush().unwrap() }); }; }