#include using namespace std; using int64 = long long; struct Constraint { int x, y, z; int64 v, low, high; }; struct Event { int64 threshold; int constraintId; int version; bool operator>(const Event& other) const { if (threshold != other.threshold) return threshold > other.threshold; if (constraintId != other.constraintId) return constraintId > other.constraintId; return version > other.version; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int testCount; cin >> testCount; while (testCount--) { int n, m; cin >> n >> m; vector constraints(m); for (Constraint& c : constraints) { cin >> c.x >> c.y >> c.z >> c.v >> c.low >> c.high; --c.x; --c.y; --c.z; } vector value(n, 0); vector version(m, 0); vector, greater>> events(n); vector pending; pending.reserve(m); auto schedule = [&](int id) { const Constraint& c = constraints[id]; int64 deficit = c.v - value[c.x] - value[c.y]; int64 increase = (deficit + 1) / 2; events[c.x].push({value[c.x] + increase, id, version[id]}); events[c.y].push({value[c.y] + increase, id, version[id]}); }; for (int id = 0; id < m; ++id) { if (constraints[id].v == 0) { pending.push_back(id); } else { schedule(id); } } while (!pending.empty()) { int id = pending.back(); pending.pop_back(); const Constraint& c = constraints[id]; if (value[c.z] >= c.low) continue; value[c.z] = c.low; auto& heap = events[c.z]; while (!heap.empty() && heap.top().threshold <= value[c.z]) { Event event = heap.top(); heap.pop(); if (event.version != version[event.constraintId]) continue; int nextId = event.constraintId; ++version[nextId]; const Constraint& next = constraints[nextId]; if (value[next.x] + value[next.y] >= next.v) { pending.push_back(nextId); } else { schedule(nextId); } } } bool possible = true; for (const Constraint& c : constraints) { if (value[c.x] + value[c.y] >= c.v && (value[c.z] < c.low || value[c.z] > c.high)) { possible = false; break; } } if (!possible) { cout << -1 << '\n'; } else { for (int i = 0; i < n; ++i) { if (i) cout << ' '; cout << value[i]; } cout << '\n'; } } return 0; }