#include <bits/stdc++.h>
#pragma GCC optimize ("O3,unroll-loops")
#pragma GCC target ("avx,avx2,fma")
#define fastio cin.tie(0)->sync_with_stdio(0)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define compress(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define sz(x) (int)(x).size()
using namespace std;
typedef long long ll;
const ll INF = 1e18;
struct HLPP {
struct Edge { int to; ll cap; int rev; };
vector<vector<Edge>> graph;
vector<ll> ex;
vector<int> level, work;
vector<vector<int>> B;
int n, high = 0, cnt = 0;
HLPP(int n) : n(n+1), graph(n+1), level(n+1), work(n+1), ex(n+1), B(n+1) {}
void add(int u, int v, ll cap) {
graph[u].push_back({ v, cap, sz(graph[v]) });
graph[v].push_back({ u, 0, sz(graph[u])-1 });
}
void push(int u) {
if (level[u] >= n) return;
B[level[u]].push_back(u);
high = max(high, level[u]);
}
void relabel(int t) {
cnt = 0;
for (auto &b : B) b.clear();
fill(all(level), n);
queue<int> q; q.push(t);
level[t] = 0;
while (!q.empty()) {
int u = q.front(); q.pop();
int h = level[u]+1;
for (auto &e : graph[u]) {
if (graph[e.to][e.rev].cap > 0 && h < level[e.to]) {
level[e.to] = h; q.push(e.to);
if (ex[e.to] > 0) push(e.to);
}
}
}
}
void discharge(int u) {
auto &excess = ex[u];
int h = n*2, Sz = sz(graph[u]);
for (int &i = work[u], m = Sz; m--; i = (i-1 + Sz) % Sz) {
auto &e = graph[u][i];
if (!e.cap) continue;
if (level[u] != level[e.to] + 1) {
h = min(h, level[e.to]+1);
continue;
}
auto f = min(e.cap, excess);
e.cap -= f;
excess -= f;
if (!ex[e.to]) push(e.to);
ex[e.to] += f;
graph[e.to][e.rev].cap += f;
if (!excess) return;
}
cnt++; level[u] = h;
if (level[u] < n && ex[u] > 0) push(u);
}
ll flow(int s, int t) {
fill(all(ex), 0); fill(all(work), 0);
high = cnt = 0;
relabel(t);
ex[s] = INF; ex[t] = -INF;
push(s);
for (; ~high; high--) {
while (!B[high].empty()) {
int u = B[high].back();
B[high].pop_back();
if (level[u] == high) discharge(u);
if (cnt > n) relabel(t);
}
}
return ex[t] + INF;
}
};
struct Circulation {
vector<ll> demand, low;
vector<pair<int,int>> edge;
int n, S, T; HLPP dn;
Circulation(int n) : n(n), S(n+1), T(n+2), demand(n+3, 0), dn(n+2) {};
void add_demand(int u, int d) { demand[u] += d; }
int add(int u, int v, ll l, ll r) {
demand[u] -= l; demand[v] += l;
dn.add(u, v, r-l); low.push_back(l);
edge.push_back({ u, sz(dn.graph[u])-1 });
return sz(edge)-1;
}
ll solve() {
ll sum = 0, res = 0;
for (int i = 1; i <= n; i++) sum += demand[i];
if (sum != 0) return false;
for (int i = 1; i <= n; i++) {
if (demand[i] > 0) {
dn.add(S, i, demand[i]);
res += demand[i];
}
else if (demand[i] < 0) dn.add(i, T, -demand[i]);
}
ll f = dn.flow(S, T);
return (f != res ? -1 : f);
}
ll get_flow(int i) {
auto [u, idx] = edge[i];
int v = dn.graph[u][idx].to, rev = dn.graph[u][idx].rev;
return dn.graph[v][rev].cap + low[i];
}
void cut(int i) { // To get min flow: cut edge T->S(INF) and flow T->S
auto [u, idx] = edge[i];
auto& fwd = dn.graph[u][idx];
auto& bwd = dn.graph[fwd.to][fwd.rev];
fwd.cap = bwd.cap = 0;
}
};
int main() {
fastio; int N, M; cin >> N >> M;
vector<pair<int,int>> cv(N+1);
for (int i = 1; i <= N; i++) {
cin >> cv[i].first >> cv[i].second;
}
Circulation cc(N);
for (int i = 0; i < M; i++) {
int u, v, w; cin >> u >> v >> w;
if (cv[u].first == cv[v].first) continue;
if (cv[u].first > cv[v].first) swap(u, v);
cc.add(u, v, w, INF);
}
int t1 = cc.add(N, 1, 0, INF);
cc.solve();
cc.cut(t1); cc.dn.flow(N, 1);
for (int i = 0; i < M; i++) cout << cc.get_flow(i) << "\n";
return 0;
}