#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
struct Network_sap{
#define Max_V 100000 //最大点数
#define Max_E 1000000 //最大边数
int e,vs,vt;
struct edge{ int x,y,c; edge * next,* op; } * ls[ Max_V] ,* fa[ Max_V] ,* cur[ Max_V] ,g[ Max_E] ;
int d[ Max_V] ,num[ Max_V] ;
#undef Max_V
#undef Max_E
void init( ) { e= 0 ; memset ( ls,0 ,sizeof ( ls) ) ; }
void clear( ) { for ( int i= 0 ; i< e; i++ ) ls[ g[ i] .x ] = NULL ; e= 0 ; }
void push_back( int l,int r) { //把区间内反向弧的容量推回正向弧,清空流量
for ( int i= l; i<= r; i++ )
if ( i& 1 ) {
g[ i] .op - > c+ = g[ i] .c ;
g[ i] .c = 0 ;
}
}
void refresh( int r) { //只留下前r条边
clear( ) ;
push_back( 0 ,r- 1 ) ;
for ( int i= 0 ; i< r; i++ ) {
g[ i] .next = ls[ g[ i] .x ] ;
ls[ g[ i] .x ] = & g[ i] ;
}
e= r;
}
void addedge( int x,int y,int c) {
g[ e] .x = x; g[ e] .y = y; g[ e] .c = c; g[ e] .next = ls[ x] ; ls[ x] = & g[ e] ; g[ e] .op = & g[ e+ 1 ] ;
e++ ;
g[ e] .x = y; g[ e] .y = x; g[ e] .c = 0 ; g[ e] .next = ls[ y] ; ls[ y] = & g[ e] ; g[ e] .op = & g[ e- 1 ] ;
e++ ;
//无向图加双边
/*
swap(x,y);
g[e].x=x; g[e].y=y; g[e].c=c; g[e].next=ls[x]; ls[x]=&g[e]; g[e].op=&g[e+1];
e++;
g[e].x=y; g[e].y=x; g[e].c=0; g[e].next=ls[y]; ls[y]=&g[e]; g[e].op=&g[e-1];
e++;
*/
}
#define INF 0x7FFFFFFF
int sap( int n) { //注意是从1到n这个范围有点
int ret= 0 ,i,nf;
for ( i= 1 ; i<= n; i++ ) d[ i] = 0 ,num[ i] = 0 ,cur[ i] = ls[ i] ;
for ( i= vs,num[ 0 ] = n; d[ vs] < n; ) {
edge * & t= cur[ i] ;
while ( t && ! ( t- > c && d[ t- > y] + 1 == d[ i] ) ) t= t- > next;
if ( t) {
fa[ t- > y] = t;
i= t- > y;
if ( i== vt) {
for ( nf= INF; i! = vs; i= fa[ i] - > x) if ( fa[ i] - > c< nf) nf= fa[ i] - > c;
for ( i= vt ; i! = vs; i= fa[ i] - > x) { fa[ i] - > c- = nf; fa[ i] - > op- > c+ = nf; }
ret+ = nf;
}
}
else {
if ( -- num[ d[ i] ] == 0 ) return ret; // gap
d[ i] = n;
for ( t= ls[ i] ; t; t= t- > next)
if ( t- > c && d[ t- > y] + 1 < d[ i] ) d[ i] = d[ t- > y] + 1 ;
num[ d[ i] ] ++ ;
t= ls[ i] ;
if ( i! = vs) i= fa[ i] - > x;
}
}
return ret;
}
#undef INF
} Network;
int n,m,Sum;
int Low[ 1000000 ] ;
void init( ) {
Network.clear ( ) ;
Sum= 0 ;
scanf ( "%d%d" ,& n,& m) ;
Network.vs = n+ 1 ;
Network.vt = n+ 2 ;
for ( int x,y,lower,upper,i= 1 ; i<= m; i++ ) {
scanf ( "%d%d%d%d" ,& x,& y,& lower,& upper) ;
Network.addedge ( n+ 1 ,y,lower) ;
Network.addedge ( x,n+ 2 ,lower) ;
Network.addedge ( x,y,upper- lower) ;
Sum+ = lower;
Low[ Network.e - 1] = lower;
}
}
void gao( ) {
int ret= Network.sap ( n+ 2 ) ;
if ( ret== Sum) {
puts ( "YES" ) ;
for ( int i= 5 ; i< Network.e ; i+ = 6 )
printf ( "%d\n " ,Low[ i] + Network.g [ i] .c ) ;
}
else puts ( "NO" ) ;
}
int main( ) {
// freopen("input.txt","r",stdin);
int Tc,i;
scanf ( "%d" ,& Tc) ;
Network.init ( ) ;
for ( i= 1 ; i<= Tc; i++ ) {
init( ) ;
gao( ) ;
if ( i! = Tc) puts ( "" ) ;
}
}
I2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPGNzdGRsaWI+CiNpbmNsdWRlIDxjc3Rya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ldKzE9PWRbaV0pKSB0PXQtPm5leHQ7CiAgICAgICAgICAgIGlmICh0KXsKICAgICAgICAgICAgICAgIGZhW3QtPnldPXQ7CiAgICAgICAgICAgICAgICBpPXQtPnk7CiAgICAgICAgICAgICAgICBpZiAoaT09dnQpewogICAgICAgICAgICAgICAgICAgIGZvciAobmY9SU5GO2khPXZzO2k9ZmFbaV0tPngpIGlmIChmYVtpXS0+YzxuZikgbmY9ZmFbaV0tPmM7CiAgICAgICAgICAgICAgICAgICAgZm9yIChpPXZ0ICA7aSE9dnM7aT1mYVtpXS0+eCkge2ZhW2ldLT5jLT1uZjsgZmFbaV0tPm9wLT5jKz1uZjt9CiAgICAgICAgICAgICAgICAgICAgcmV0Kz1uZjsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlewogICAgICAgICAgICAgICAgaWYgKC0tbnVtW2RbaV1dPT0wKSByZXR1cm4gcmV0OyAgIC8vIGdhcAogICAgICAgICAgICAgICAgZFtpXT1uOwogICAgICAgICAgICAgICAgZm9yICh0PWxzW2ldO3Q7dD10LT5uZXh0KQogICAgICAgICAgICAgICAgICBpZiAodC0+YyAmJiBkW3QtPnldKzE8ZFtpXSkgZFtpXT1kW3QtPnldKzE7CiAgICAgICAgICAgICAgICBudW1bZFtpXV0rKzsKICAgICAgICAgICAgICAgIHQ9bHNbaV07CiAgICAgICAgICAgICAgICBpZiAoaSE9dnMpIGk9ZmFbaV0tPng7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcmV0dXJuIHJldDsKICAgIH0KICAgICN1bmRlZiBJTkYKfU5ldHdvcms7CgppbnQgbixtLFN1bTsKaW50IExvd1sxMDAwMDAwXTsKCnZvaWQgaW5pdCgpewogICAgTmV0d29yay5jbGVhcigpOwogICAgU3VtPTA7CiAgICBzY2FuZigiJWQlZCIsJm4sJm0pOwogICAgTmV0d29yay52cz1uKzE7CiAgICBOZXR3b3JrLnZ0PW4rMjsKICAgIGZvciAoaW50IHgseSxsb3dlcix1cHBlcixpPTE7aTw9bTtpKyspewogICAgICAgIHNjYW5mKCIlZCVkJWQlZCIsJngsJnksJmxvd2VyLCZ1cHBlcik7CiAgICAgICAgTmV0d29yay5hZGRlZGdlKG4rMSx5LGxvd2VyKTsKICAgICAgICBOZXR3b3JrLmFkZGVkZ2UoeCxuKzIsbG93ZXIpOwogICAgICAgIE5ldHdvcmsuYWRkZWRnZSh4LHksdXBwZXItbG93ZXIpOwogICAgICAgIFN1bSs9bG93ZXI7CiAgICAgICAgTG93W05ldHdvcmsuZS0xXT1sb3dlcjsKICAgIH0KfQoKdm9pZCBnYW8oKXsKICAgIGludCByZXQ9TmV0d29yay5zYXAobisyKTsKICAgIGlmIChyZXQ9PVN1bSl7CiAgICAgICAgcHV0cygiWUVTIik7CiAgICAgICAgZm9yIChpbnQgaT01O2k8TmV0d29yay5lO2krPTYpCiAgICAgICAgICBwcmludGYoIiVkXG4iLExvd1tpXStOZXR3b3JrLmdbaV0uYyk7CiAgICB9CiAgICBlbHNlIHB1dHMoIk5PIik7Cn0KCmludCBtYWluKCl7Ci8vICAgIGZyZW9wZW4oImlucHV0LnR4dCIsInIiLHN0ZGluKTsKICAgIGludCBUYyxpOwogICAgc2NhbmYoIiVkIiwmVGMpOwogICAgTmV0d29yay5pbml0KCk7CiAgICBmb3IgKGk9MTtpPD1UYztpKyspewogICAgICAgIGluaXQoKTsKICAgICAgICBnYW8oKTsKICAgICAgICBpZiAoaSE9VGMpIHB1dHMoIiIpOwogICAgfQp9