fn run(sc: &mut scanner::Scanner, out: &mut std::io::BufWriter) { let t: u32 = sc.next(); for _ in 0..t { let n: usize = sc.next(); let m: usize = sc.next(); let mut cond = vec![Cond::default(); m]; for p in cond.iter_mut() { p.x = sc.next(); p.y = sc.next(); p.z = sc.next(); p.x -= 1; p.y -= 1; p.z -= 1; p.v = sc.next(); p.l = sc.next(); p.r = sc.next(); } if let Some(a) = solve(n, cond) { use util::*; writeln!(out, "{}", a.iter().join(" ")).ok(); } else { writeln!(out, "-1").ok(); } } } #[derive(Clone, Copy, Default)] struct Cond { x: usize, y: usize, z: usize, v: i32, l: i32, r: i32, } fn solve(n: usize, p: Vec) -> Option> { let mut low = vec![0; n]; let mut up = vec![10i32.pow(9); n]; let mut h = vec![std::collections::BinaryHeap::new(); n]; let mut used = vec![false; p.len()]; let mut dfs = vec![]; for (i, c) in p.iter().enumerate() { if c.v == 0 { used[i] = true; dfs.push(i); } else { let v = (c.v + 1) / 2; h[c.x].push((-v, i)); h[c.y].push((-v, i)); } } while let Some(i) = dfs.pop() { let c = p[i]; low[c.z].chmax(c.l); up[c.z].chmin(c.r); while h[c.z].peek().map_or(false, |p| used[p.1] || p.0 + low[c.z] >= 0) { let (_, k) = h[c.z].pop().unwrap(); if used[k] { continue; } let c = p[k]; let val = low[c.x] + low[c.y]; if val >= c.v { used[k] = true; dfs.push(k); } else { let need = c.v - val; let p = low[c.x] + (need + 1) / 2; h[c.x].push((-p, k)); let p = low[c.y] + (need + 1) / 2; h[c.y].push((-p, k)); } } } if low.iter().zip(up.iter()).all(|p| *p.0 <= *p.1) { Some(low) } else { None } } // ---------- begin scannner ---------- #[allow(dead_code)] mod scanner { use std::str::FromStr; pub struct Scanner<'a> { it: std::str::SplitWhitespace<'a>, } impl<'a> Scanner<'a> { pub fn new(s: &'a String) -> Scanner<'a> { Scanner { it: s.split_whitespace(), } } pub fn next(&mut self) -> T { self.it.next().unwrap().parse::().ok().unwrap() } pub fn next_bytes(&mut self) -> Vec { self.it.next().unwrap().bytes().collect() } pub fn next_chars(&mut self) -> Vec { self.it.next().unwrap().chars().collect() } pub fn next_vec(&mut self, len: usize) -> Vec { (0..len).map(|_| self.next()).collect() } } } // ---------- end scannner ---------- use std::collections::*; use std::io::Write; type Map = BTreeMap; type Set = BTreeSet; type Deque = VecDeque; fn main() { use std::io::Read; let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); let mut sc = scanner::Scanner::new(&s); let out = std::io::stdout(); let mut out = std::io::BufWriter::new(out.lock()); run(&mut sc, &mut out); } mod util { pub trait Join { fn join(self, sep: &str) -> String; } impl Join for I where I: Iterator, T: std::fmt::Display, { fn join(self, sep: &str) -> String { let mut s = String::new(); use std::fmt::*; for (i, v) in self.enumerate() { if i > 0 { write!(&mut s, "{}", sep).ok(); } write!(&mut s, "{}", v).ok(); } s } } } // ---------- begin chmin, chmax ---------- pub trait ChangeMinMax { fn chmin(&mut self, x: Self) -> bool; fn chmax(&mut self, x: Self) -> bool; } impl ChangeMinMax for T { fn chmin(&mut self, x: Self) -> bool { *self > x && { *self = x; true } } fn chmax(&mut self, x: Self) -> bool { *self < x && { *self = x; true } } } // ---------- end chmin, chmax ----------