#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int , int> pii;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int maxn = 4e5 + 10;
const ll mod = 1e9+7;

#define pb push_back
#define endl '\n'
#define dokme(x) cout << x , exit(0)
#define ms(x , y) memset(x , y , sizeof x)
ll pw(ll a, ll b, ll md = mod){ll res = 1;while(b){if(b&1){res=(a*res)%md;}a=(a*a)%md;b>>=1;}return(res);}

int t, n, m, c;
vector < int > out[maxn], in[maxn];
queue < int > q;
vector < int > topo;
bool loop[maxn], inf[maxn];
int ans[maxn], col[maxn], sz[maxn];

void sfd(int v){
	col[v] = c;
	for(auto u : in[v]) if(!col[u])
		sfd(u);
}
void dfs(int v){
	col[v] = 1;
	for(auto u : out[v]) if(!col[u])
		dfs(u);
	topo.pb(v);
}

vector < int > adj[maxn];
int lsl[maxn];
int reach[maxn];

int32_t main(){
	cin.tie(0)->sync_with_stdio(0);
	cin >> t;
	while(t --){
		cin >> n >> m;
		for(int i = 1 ; i <= n ; i ++)
			out[i].clear(), in[i].clear(), ans[i] = 0 , loop[i] = 0 , col[i] = 0;
		for(int i = 1 ; i <= n ; i ++)lsl[i] = 0;
		for(int i = 1 ; i <= m ; i ++){
			int u, v;
			cin >> u >> v;
			if(u ^ v)out[u].pb(v), in[v].pb(u);
			if(u == v)loop[u] = 1;
		}
		topo.clear();
		for(int i = 1 ; i <= n ; i ++ ) if(!col[i]) dfs(i);
		reverse(topo.begin() , topo.end());
		for(int i = 1 ; i <= n ; i ++)col[i] = 0;
		c = 0;
		for(auto v : topo)
			if(!col[v])
				++c , sfd(v);
		for(int i = 1 ; i <= c ; i ++)
			sz[i] = 0, inf[i] = 0 , adj[i].clear();
		for(int i = 1 ; i <= n ; i ++)
			sz[col[i]] ++, sz[col[i]] += loop[i];
		for(int i = 1 ; i <= c ; i ++)
			if(sz[i] > 1)inf[i] = 1;
		for(int i = 1 ; i <= n ; i ++)
			for(auto u : out[i])if(col[u] ^ col[i])	
				adj[col[i]].pb(col[u]);
		for(int i = 1 ; i <= c ; i ++)
			sort(adj[i].begin(), adj[i].end()),
			adj[i].resize(unique(adj[i].begin(), adj[i].end()) - adj[i].begin());
		for(int i = 1 ; i <= c ; i ++)reach[i] = 0;
		reach[col[1]] = 1;
		q.push(col[1]);
		while(q.size()){
			int v = q.front();
			q.pop();
			for(auto u : adj[v])if(!reach[u])
				reach[u] = 1 , q.push(u);
		}
		for(int i = 1 ; i <= c ; i ++)if(reach[i])
			for(auto u : adj[i]){
			lsl[u] ++;}
		q.push(col[1]);
		ans[col[1]] = ((inf[col[1]]) ? -1 : 1);
		while(q.size()){
			int v = q.front();
			q.pop();
			for(auto u : adj[v]){
				lsl[u] --;
				if(ans[u] == 0){
					ans[u] = ans[v];
					if(inf[u])ans[u] = -1;
				}
				else if(ans[u] == 1){
					if(ans[v] == -1)ans[u] = -1;
					else ans[u] = 2;
				}
                else if(ans[u] == 2){
                    if(ans[v] == -1)ans[u] = -1;
                }
				if(lsl[u] == 0)q.push(u);
				if(inf[u])ans[u] = -1;
			}
		}
		for(int i = 1 ; i <= n ; i ++)
			cout << ans[col[i]] << ' ';
		cout << endl;	
	}
	return(0);
}