#include <bits/stdc++.h>
#define pb push_back
#define ll long long
#define ull unsigned long long
#define mp make_pair
#define si short int
#define speed ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define pill pair<ll,ll>
#define f first
#define s second
#define pilc pair<ll,char>
#define all(a) (a).begin(),(a).end()
#define rep(s,e,step) for(int i = (s); i < (e) ; i += step)
#define vrep(s,e,step) for(int j = (s); j < (e) ; j += step)
#define ex exit(0) 
#define sz(a) (ll)(a).size()
#define triple pair<pill, ll>
#define pinode pair<node*, node*>
#define quadra pair<pill, pill>
#define ld double
using namespace std;
 
const ll N = 1e6 + 10;
const ll M = 1e14;
const ll big = 1e17;
const ll hsh2 = 1964325029 ;
const long long mod = 998244353;
const long double EPS = 1e-9;
const ll block = 1e14;
const ll shift = 2e3;
const ld pi = acos(-1.0);



ll ta, n, m;
bool was[N];
vector<ll> g[N], bg[N], ng[N], tps;

void dfs(ll v) {
	was[v] = 1;
	for(auto u : g[v]) {
		if(!was[u])
			dfs(u);			
	}      
	tps.pb(v);
}

ll col[N], cnt = 1, cont[N];
vector<ll> comp;

void bdfs(ll v) {
 	was[v] = 0;
 	col[v] = cnt;
	comp.pb(v); 	
 	for(auto u : bg[v]) {
 		if(was[u])
 			bdfs(u);
 	}
}


bool cycle[N], ncyc[N];

void cond() {	
	for(int i = 1; i <= n; i++)
		if(!was[i])
			dfs(i);
	reverse(tps.begin(), tps.end());
	for(auto u : tps) {
	    if(!was[u])continue;
		bdfs(u);
		if(comp.size() == 1) {
			if(cycle[comp.back()])
				ncyc[cnt] = 1;	
		} else ncyc[cnt] = 1;
		comp.clear();
		cnt++;
	}
	for(int i = 1; i <= n; i++) {
		for(auto u : g[i]) {
			if(col[u] == col[i])continue;
			ng[col[i]].pb(col[u]);	
		}
	}
}


ll answ[N];

void bfs() {
	for(int i = 1; i < cnt; i++)
		answ[i] = 4;
	queue<ll> x;
	if(ncyc[col[1]])
		answ[col[1]] = -1;
	else
		answ[col[1]] = 1;
	x.push(col[1]);
	was[col[1]] = 1;
	while(x.size()) {
		ll v = x.front();
		x.pop();
		for(auto u : ng[v]) {
			if(!was[u])
				was[u] = 1, x.push(u);
		}
	}
	for(int i = 1; i < cnt; i++) {
		if(was[i]) {
			for(auto u : ng[i])
				cont[u]++;						
		}
	}
	vector<ll> activate;
	for(int i = 1; i < cnt; i++) {
		if(was[i] && cont[i] == 0) {
			activate.pb(i);
		}
		was[i] = 0;
	}
	ll cur = 0;
	while(cur < activate.size()) {
		ll v = activate[cur];
		for(auto u : ng[v]) {
			if(!was[u]) {
				was[u] = 1;
				answ[u] = answ[v];
				cont[u]--;
				if(ncyc[u])
					answ[u] = -1;
			}
			else {
				if(answ[v] == -1)
					answ[u] = -1;
			 	else if(answ[u] == 1 && answ[v] != -1)
			 		answ[u] = 2;			 			
				cont[u]--;
			}
			if(cont[u] == 0)
				activate.pb(u);
		}
		cur++;
	}
}

int main() {                                                       
	speed;
	cin >> ta;
	while(ta--) {
		cin >> n >> m;
		for(int i = 1; i <= n; i++)
			g[i].clear(), ng[i].clear(), bg[i].clear(), cycle[i] = ncyc[i] = was[i] = answ[i] = col[i] = cont[i] = 0;
		tps.clear();
		for(int i = 1, a, b; i <= m; i++) {
			cin >> a >> b;
			if(a == b)cycle[a] = 1;
			g[a].pb(b);
			bg[b].pb(a);						
		}
		cond();
		bfs();
		for(int i = 1; i <= n; i++)
			cout << (answ[col[i]] == 4 ? 0 : answ[col[i]]) << " ";
		cout << '\n';	
	}
}
/*                  
4
4 6 1 9
2 5 3 2 
*/