#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll,ll> pi;
typedef vector <ll> vi;
typedef vector <pi> vpi;
typedef pair<pi,ll> pii;
typedef set <ll> si;
typedef long double ld;
#define f first
#define s second
#define FOR(i,s,e) for(ll i=s;i<=ll(e);++i)
#define DEC(i,s,e) for(ll i=s;i>=ll(e);--i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define aFOR(i,x) for (auto i: x)
#define mem(x,i) memset(x,i,sizeof x)
#define fast ios_base::sync_with_stdio(false),cin.tie(0)
#define maxn 400010

int N,M,a,b;
vi adj[maxn];
int vis[maxn], ans[maxn];

void dfs(int x){
	if (vis[x] == 2){
		if (ans[x] != -1) ans[x] = 2;
		return;
	}
	if (vis[x] == 1){
		ans[x] = -1; return;
	}
	vis[x] = 1;
	
	aFOR(i,adj[x]){
		dfs(i);
	}
	vis[x] = 2;
}
void dfs2(int x){
	ans[x] = -1;
	aFOR(i,adj[x]) if (ans[i] != -1) dfs2(i);
}
void dfs3(int x){
	ans[x] = 2;
	aFOR(i,adj[x]) if (ans[i] != 2 && ans[i] != -1) dfs3(i);
}
void solve(){
	cin >> N >> M;
	FOR(i,1,N) adj[i].clear();
	mem(ans, 0);
	mem(vis, 0);
	FOR(i,1,M){
		cin >> a >> b; adj[a].pb(b);
	} 
	
	dfs(1);
	FOR(i,1,N) if (ans[i] == -1) dfs2(i);
	FOR(i,1,N) if (ans[i] == 2) dfs3(i);
	FOR(i,1,N){
		if (ans[i] == 0){
			if (vis[i] == 0) cout << 0 << ' ';
			else cout << 1 << ' ';
		}else cout << ans[i] << ' ';
	}
	cout << '\n';
}
int main(){
	fast;
	int TC;
	cin >> TC;
	while (TC--) solve();
	
}

