//cf1547G
//https://blog.csdn.net/weixin_46113968 20210714
//path number,0,1,more
#include<iostream>
#include<vector>
using namespace std;
const int maxn=4e5+5;
int n,m,vis[maxn],a[maxn];
vector<int>vc[maxn];
void dfs(int x,int now)
{
	if(now)//mark loop
		a[x]=-1;
	else if(a[x]<2)//one more road
		a[x]++;
	vis[x]=1;//mark visited
	for(auto y:vc[x])//each next vertice y
	{
		if(a[y]==-1)//next loop fornmed,next vertice
			continue;
		if(vis[y]||now)//visited or loop formed
			dfs(y,1);
		else if(a[y]<2)//continue to find
			dfs(y,now);
	}
	vis[x]=0;//x go back
}
int main()
{
	int t,x,y;
	cin>>t;
	while(t--)//case number
	{
		cin>>n>>m;//line number,vertice number
		for(int i=1;i<=n;i++)//clear,a as 0,vis as 0
		{
			vc[i].clear();
			a[i]=0;
			vis[i]=0;
		}
		while(m--)//each line
		{
			cin>>x>>y;
			vc[x].push_back(y);
		}
		dfs(1,0);
		for(int i=1;i<=n;i++) 
			cout<<a[i]<<' ';
		cout<<endl;
	}
	return 0;
} 
