#include <bits/stdc++.h>
#define ri register int
using namespace std;
const int N = 110;
struct Edge
{
int to, w, next;
}e[N * N];
struct Ans
{
int id, c;
}ans[N];
int top, h[N], cnt;
int n, p, u[N], c[N];
bool in[N], out[N], vis[N];
queue<int> q;
bool cmp(Ans x, Ans y)
{
return x.id < y.id;
}
void add(int x, int y, int w)
{
e[++top] = (Edge){y, w, h[x]};
h[x] = top;
}
void topsort()
{
for (ri i = 1; i <= n; i ++)
if (in[i]) q.push(i);
while (!q.empty())
{
int x = q.front(); q.pop();
if (c[x] <= 0) continue;
for (ri i = h[x]; i; i = e[i].next)
{
int y = e[i].to, w = e[i].w;
c[y] += w * c[x];
q.push(y);
}
if (!in[x] && !vis[x])
{
c[x] -= u[x];
vis[x] = true;
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> p;
for (ri i = 1; i <= n; i ++)
{
cin >> c[i] >> u[i];
if (c[i]) in[i] = true;
}
for (ri i = 1; i <= p; i ++)
{
int x, y, w;
cin >> x >> y >> w;
add(x, y, w);
out[x] = true;
}
topsort();
bool flag = false;
for (ri i = 1; i <= n; i ++)
if (!out[i] && c[i] > 0)
{
flag = true;
ans[++cnt] = (Ans){i, c[i]};
}
if (!flag)
{
cout << "NULL";
return 0;
}
sort(ans + 1, ans + cnt + 1, cmp);
for (ri i = 1; i <= cnt; i ++)
cout << ans[i].id << ' ' << ans[i].c << '\n';
return 0;
}