#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define int long long
#define all(v) v.begin(),v.end()
#define mod 1000000007 
#define N 500010
#define inf LLONG_MAX

//---------------------------------------------------------------
vector<int> graph[N];
vector<int> incoming[N];
vector<int> visited(N,false);
vector<int> ans(N,0);
map<int,bool> mp;
vector<int> special;
vector<int> colour(N,0);
vector<int> parent(N,-1);
vector<bool> store(N,false);

//---------------------------------------------------------------

void dfs(int node,int mark)
{
	visited[node]=true;
	ans[node]=mark;
	
	for(auto x:graph[node])
	{
		if(!visited[x])
		{
			dfs(x,mark);
		}
	}
}


void dfs2(int node)
{
	visited[node]=true;
	if(ans[node]==1)
	{
		ans[node]=2;
	}
	for(auto x:graph[node])
	{
		if(!visited[x])
		{
			dfs2(x);
		}	
	}
}


//---------------------------------------------------------------

void cycle_wala_dfs(int node)
{
	visited[node]=true;
	colour[node]=1;
	for(auto x:graph[node])
	{
		if(!visited[x])
		{
			parent[x]=node;
			cycle_wala_dfs(x);
		}
		else if(visited[x] and colour[x]==1)
		{
			//part of cycle 
			int curr=node;
			while(curr!=x)
			{
				if(ans[curr]==-1)
				{
					break;
				}
				ans[curr]=-1;
				special.push_back(curr);
				curr=parent[curr];
			}
			ans[x]=-1;
		}
	}
	colour[node]=2;
}


//---------------------------------------------------------------

void clr_vis(int n)
{
	for(int i=0;i<n;i++)
    {
    	visited[i]=false;
    }
        
}


void clr(int n)
{
	for(int i=0;i<=n;i++)
	{
		graph[i].clear();
		incoming[i].clear();
		visited[i]=false;
		ans[i]=0;
		store[i]=false;
		parent[i]=-1;
		colour[i]=0;
		
	}
	mp.clear();
	special.clear();
}

signed main()
{
    fast
    int t;
    cin>>t;
    while(t--)
    {
        int n,m;
        cin>>n>>m;
        clr(n);
        
        
        for(int i=0;i<m;i++)
        {
            int u,v;
            cin>>u>>v;
            u--;v--;
            
            if(u==v)
            {
            	mp[u]=true;
            }
            else 
            {
            	graph[u].push_back(v);
            	incoming[v].push_back(u);
            }
            
        }
        dfs(0,1);
        for(auto x:mp)
        {
        	if(visited[x.first])
        	{
        		special.push_back(x.first);
        	}
        }
        for(int i=0;i<n;i++)
        {
        	store[i]=visited[i];
        }
        clr_vis(n);
        
        cycle_wala_dfs(0);
        
        clr_vis(n);
        
        for(auto x:special)
        {
        	if(!visited[x])
        		dfs(x,-1);
        }
        
        
        vector<int> multiple;
        for(int i=0;i<n;i++)
        {
        	if(ans[i]==1)
        	{
        		int cnt=0;
        		for(auto x:incoming[i])
        		{
        			if(store[x])
        			{
        				cnt++;
        			}
        		}
        		if(cnt>1)
        		{
        			multiple.push_back(i);
        		}
        	}
        }
        
        clr_vis(n);
        
        for(auto x:multiple)
        {
        	if(!visited[x])
        	{
        		dfs2(x);
        	}
        }
        
        
        
        // printing the final ans 
        for(int i=0;i<n;i++)
        {
        	cout<<ans[i]<<" ";
        }
        cout<<"\n";
    }
}

//Note : Always check for the following things
//1. Max value of N
//2. Mod value
//3. Test for your own case
//4. Check for special values (1 in gcd, corner case etc...)