#include <iostream>
#include <vector>
#include <queue>
using namespace std;
struct Node
{
int bias{};
int state{};
vector<pair<int,int> > weight;
};
int n,p;
int main(int argc, char const *argv[])
{
vector<Node> node;
queue<int> q;
queue<int> ans;
cin >> n >> p;
for (int i = 0; i < n; i++)
{
Node t;
cin >> t.state >> t.bias;
if (t.state == 0) {
t.state -= t.bias;
} else {
q.push(i);
}
node.push_back(t);
}
for (int k = 0; k < p; k++)
{
int i,j,w;
cin >> i >> j >> w;
node[i-1].weight.push_back(make_pair(j-1,w));
}
for (int i = 0; i < n; i++)
{
if (node[i].weight.empty())
{
ans.push(i);
}
}
bool flag = true;
while (!q.empty()) {
if (node[q.front()].state <= 0)
{
q.pop();
continue;
}
for (int i = 0; i < node[q.front()].weight.size(); ++i) {
node[node[q.front()].weight[i].first].state += node[q.front()].state * node[q.front()].weight[i].second;
q.push(node[q.front()].weight[i].first);
}
q.pop();
}
while (!ans.empty())
{
if (node[ans.front()].state > 0)
{
cout << ans.front() + 1 << " " << node[ans.front()].state << endl;
flag = false;
}
ans.pop();
}
if (flag)
{
cout << "NULL" << endl;
}
return 0;
}