#include <bits/stdc++.h>
using namespace std;
int n, p;
int u[110], in[110], out[110];
int c[110];
struct Edge
{
int to, w;
};
vector<Edge> g[110];
void toposort()
{
queue<int> q;
for (int i = 1; i <= n; i++)
if (c[i] > 0) q.push(i);
while (!q.empty())
{
int x = q.front();
q.pop();
if (c[x] <= 0) continue;
for (auto y: g[x])
{
c[y.to] += c[x] * y.w;
in[y.to]--;
if (!in[y.to]) q.push(y.to);
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> p;
for (int i = 1; i <= n; i++)
cin >> c[i] >> u[i];
for (int i = 1; i <= n; i++)
if (c[i] <= 0) c[i] = -u[i];
for (int i = 1; i <= p; i++)
{
int u, v, w;
cin >> u >> v >> w;
g[u].push_back((Edge){v, w});
in[v]++;
out[u]++;
}
toposort();
bool flag = true;
for (int i = 1; i <= n; i++)
if (!out[i] && c[i] > 0)
{
cout << i << " " << c[i] << endl;
flag = false;
}
if (flag) cout << "NULL" << endl;
return 0;
}