//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("O2")
//#pragma GCC optimize("O3")
////#pragma GCC optimize("unroll-loops")
////
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
#include <bits/stdc++.h>

#define all(x) (x).begin(), (x).end()
#define allp(x) (x)->begin(), (x)->end()

#define pb push_back


using namespace std;
void dout() { cerr << endl; }
//typedef long long ll;
template <typename Head, typename... Tail>
void dout(Head H, Tail... T) {
    cerr << H << ' ';
    dout(T...);
}

using ll = long long;
//#ifdef __int128
//using hll = __int128;
//#endif
using pii = pair<ll, ll>;
using ld = long double;
using pll = pair<long long, long long>;

template <typename T>
void do_uniq(vector<T> &vec){
    sort(all(vec));
    vec.resize(unique(all(vec)) - vec.begin());
}

#ifndef ONLINE_JUDGE

clock_t timestamp_start = clock();
void time_calc()
{
    cerr << (ld)(clock() - timestamp_start) / CLOCKS_PER_SEC << "\n";
}
#endif


#ifdef _getchar_nolock
#else
#define _getchar_nolock getchar_unlocked
#endif

#define integer int

integer mod = 1e9 + 7;

integer ml(integer a, integer b) {
    return (a * 1ll * b) % mod;
}

integer add(integer a, integer b) {
    return (a + b) % mod;
}

integer sub(integer a, integer b) {
    return add(a, mod - b);
}

integer sq(integer a) {
    return ml(a, a);
}

integer b_p(integer b, integer p) {
    if (p == 0) return 1;
    if (p & 1) return ml(b, b_p(b, p - 1));
    return sq(b_p(b, p >> 1));
}

#define solvsh
//#define multi

#ifdef solvsh

const int MAXN = 2e5 + 31;

void precalc() {
}

struct edge {
    int from;
    int to;
    int ci;

    int other(int i) {
        return i == from ? to : from;
    }

};

edge edgs[MAXN];

vector<int> g[MAXN];
set<int> forb[MAXN];

int assgn[MAXN];
bool usd[MAXN];

void dfs(int ver, int l =- 1) {
    if (usd[ver]) return;;
    usd[ver] = true;


    if (l == -1) {
        l = 1;
        while (forb[ver].count(l)) ++l;
    }

    assgn[ver] = l;
    for (auto e : g[ver]) {
        forb[edgs[e].other(ver)].insert(l);
    }


    for (auto e : g[ver]) {
        int to = edgs[e].other(ver);
        if (assgn[ver] == edgs[e].ci) dfs(to, -1);
        else dfs(to, edgs[e].ci);
    }
}

void solve() {

    int n, m;
    cin >> n >> m;

    int last = 0;

    for (int i = 0; i < m; ++i) {

        int f, t, c;
        cin >> f >> t >> c;
        --f;
        --t;
        edgs[last] = {f, t, c};
        g[f].push_back(last);
        g[t].push_back(last);
        ++last;
    }

    dfs(0);

    for (int i = 0; i < n; ++i) {
        if (!usd[i]) {
            cout << "No\n";
            return;
        }
    }

    for (int i = 0; i < n; ++i) {
        cout << assgn[i] << "\n";
    }
}

#else
#endif

void multisolve() {
    precalc();
    int t;

    cin >> t;

    int i = 1;
    while (t--) {
        solve();
        i++;
    }
}

#ifndef ONLINE_JUDGE

void gen() {

    int n = 2000;
    cout << n << "\n";
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            cout << (i != j ? 1 : 0) << " ";
        }
        cout << "\n";
    }
}


#endif

#define int int
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    cout << fixed << setprecision(20);


#ifdef multi
    //            gen();
        multisolve();
#else

    solve();
//    gen();
#endif
    // time_calc();
}

#include<bits/stdc++.h>
using namespace std;
#define N 100005
struct ji{
	int nex,to,len;
}edge[N<<2];
int E,n,m,x,y,z,head[N],vis[N],pd[N],ans[N];
void add(int x,int y,int z){
	edge[E].nex=head[x];
	edge[E].to=y;
	edge[E].len=z;
	head[x]=E++;
}
void dfs(int k){
	vis[k]=1;
	for(int i=head[k];i!=-1;i=edge[i].nex)
		if (!vis[edge[i].to]){
			if (edge[i].len==ans[k])ans[edge[i].to]=n+1;
			else ans[edge[i].to]=edge[i].len;
			dfs(edge[i].to);
		}
}
int main(){
	scanf("%d%d",&n,&m);
	memset(head,-1,sizeof(head));
	for(int i=1;i<=m;i++){
		scanf("%d%d%d",&x,&y,&z);
		add(x,y,z);
		add(y,x,z);
	}
	dfs(1);
	for(int i=1;i<=n;i++)
		if (!vis[i]){
			printf("No");
			return 0;
		} 
	ans[1]=n+1;
	for(int i=1;i<=n;i++)
		if (ans[i]>n){
			for(int j=head[i];j!=-1;j=edge[j].nex)pd[ans[edge[j].to]]=1;
			ans[i]=1;
			while (pd[ans[i]])ans[i]++;
			for(int j=head[i];j!=-1;j=edge[j].nex)pd[ans[edge[j].to]]=0;
		}
	for(int i=1;i<=n;i++)printf("%d\n",ans[i]);
}