#include<bits/stdc++.h>
using namespace std;
int n, m, c[105], u[105], tmp[105][105], ru[105], chu[105], xx[105], yy[105], zz[105];
queue <int> q;
int main(){
cin >> n >> m;
for(int i = 1; i <= n; i++){
cin >> u[i] >> c[i];
}
for(int i = 1; i <= m; i++){
cin >> xx[i] >> yy[i] >> zz[i];
tmp[xx[i]][yy[i]] = zz[i];
ru[yy[i]]++;
chu[xx[i]]++;
}
for(int i = 1; i <= n; i++){
if(ru[i] == 0){
q.push(i);
}
}
while(!q.empty()){
int y = q.front();
q.pop();
for(int i = 1; i <= n; i++){
int x = tmp[y][i];
ru[x]--;
if(ru[x] == 0){
q.push(i);
c[x] -= u[x];
}
if(c[x] > 0){
c[x] += zz[i]*c[x];
}
}
}
bool v = 0;
for(int i = 1; i <= n; i++){
if(chu[i] == 0 && c[i] > 0){
cout << i << " " << c[i] << "\n";
v = 1;
}
}
if(v == 0){
puts("NULL");
}
return 0;
}
RT