#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<cstring>
#include<cmath>
#include<set>
#include<iomanip>
#include<map>
#include<stdio.h>
#define INF 0x3f3f3f3f
#define ll long long
#define pll pair<ll,ll>
#define pdl pair<double,ll>
#define pdd pair<double, double>
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) 
using namespace std;
const int N = 4e5 + 5;
const int mod = 1e9 + 7;
const double eps = 1e-4;
ll n,m;
vector<ll>g[N];
int color[N];
bool circle[N];
bool mul[N];
int ans[N];
bool vis[N];
void dfs_color(ll cur)
{
	color[cur]=1;
	for(auto i:g[cur])
	{
		if(color[i]==1)
		{
			circle[i]=1;
		}
		else if(color[i]==2)
		{
			mul[i]=1;
		}
		else if(color[i]==0)
		{
			dfs_color(i);
		}
	}
	color[cur]=2;
}
void dfs_mul(ll cur,int m)
{
	vis[cur]=1;
	if (mul[cur] == 1)
	{
		m = 1;
	}
	if(m==1)
	{
		ans[cur]=2;
	}
	
	//cout<<"cur is "<<cur<<" cir=="<<cir<<" m=="<<m<<" ans=="<<ans[cur]<<" color=="<<color[cur]<<endl;
	for(auto i:g[cur])
	{
		if(!vis[i]){
			dfs_mul(i,m);
		}

	}
}
void dfs_cir(ll cur,ll cir)
{
	vis[cur]=1;
	if (circle[cur] == 1)
	{
		cir = 1;
	}
	if(cir==1)
	{
		ans[cur]=-1;
	}
	
	for(auto i:g[cur])
	{
		if(!vis[i]){
			dfs_cir(i,cir);
		}

	}
}
void dfs_reachable(ll cur)
{
	vis[cur]=1;
	ans[cur]=1;
	for(auto i:g[cur])
	{
		if(!vis[i]){
			dfs_reachable(i);
		}

	}
}
void solve()
{
	cin>>n>>m;
	for(ll i=1;i<=n;i++)
	{
		g[i].clear();
		color[i]=0;
		circle[i]=0;
		mul[i]=0;
		ans[i]=0;
		vis[i]=0;
	}
	for(ll i=0;i<m;i++)
	{
		ll a,b;
		cin>>a>>b;
		g[a].push_back(b);
	}
	dfs_color(1);
	memset(vis,0,sizeof vis);
	dfs_reachable(1);
	memset(vis,0,sizeof vis);
	for (ll i = 1; i <= n; i++)
	{
		if (mul[i] == 1)
		{
			dfs_mul(i, mul[i]);
		}
	}
	memset(vis,0,sizeof vis);
	for (ll i = n; i >=1; i--)
	{
		if (circle[i] == 1)
		{
			dfs_cir(i,circle[i]);
		}
	}
	
	for(ll i=1;i<=n;i++)
	{
		cout<<ans[i]<<' ';
	}
	cout<<endl;
	
}
int main()
{
	IOS;
	ll _;
	cin>>_;
	while(_--)
	solve();

	
}




