#include<bits/stdc++.h>
using namespace std;
const int N = 200;
const int mod=80112002;
int e[N], ne[N];//邻接表存储图
int h[N],in[N],out[N],w[N];
int c[N];
int idx=0;
int n, m,ans;//保存图的点数和边数
queue<int> q;
int t,flag;
int a,b;
void add(int a,int b,int z){
e[idx]=b;
w[idx]=z;
ne[idx]=h[a];
h[a]=idx++;
}
void topsort(){
while(q.size()){
int t=q.front();
q.pop();
if(c[t]<=0){
for(int i=h[t];i!=-1;i=ne[i]){
int j=e[i];
in[j]--;
if(in[j]==0) q.push(j);
}
}
else{
for(int i=h[t];i!=-1;i=ne[i]){
int j=e[i];
c[j]+=c[t]*w[i];
//topsort+dp
in[j]--;
if(in[j]==0) q.push(j);
}
}
}
}
int main(){
int u;
memset(h,-1,sizeof h);
cin>>n>>m;
for(int i=1;i<=m;i++){
cin>>c[i]>>u;
if(c[i]!=0) q.push(i);
else c[i]-=u;
}
int a,b,z;
for(int i=1;i<=m;i++){
cin>>a>>b>>z;
add(a,b,z);
in[b]++;
out[a]=1;
}
topsort();
int flag=0;
for(int i=1;i<=n;i++){
if(out[i]==0&&c[i]>0){
cout<<i<<" "<<c[i]<<endl;
flag=1;
}
}
if(flag==0) cout<<"NULL"<<endl;
}