#include<bits/stdc++.h>
using namespace std;

#define INF 1234567890
#define ll long long

int T;
int N, M, P;
vector<int> g[400401], h[400401];
bool vis[400401], reach[400401], loop[400401];
vector<int> st;
int myscc[400401], sz[400401], dp[400401];

void DFS(int n)
{
	vis[n] = true;
	for(int next : g[n])
	{
		if (vis[next]) continue;
		DFS(next);
	}
	st.push_back(n);
}
void DFS2(int n)
{
	vis[n] = true;
	for(int next : h[n])
	{
		if (vis[next]) continue;
		DFS2(next);
	}
	myscc[n] = P;
	sz[P]++;
}
void DFS3(int n)
{
	vis[n] = true;
	for(int next : g[n])
	{
		if (vis[next]) continue;
		DFS3(next);
	}
}

bool isScc(int i)
{
	return (sz[myscc[i]] >= 2 || loop[i]);
}

int main()
{
	ios::sync_with_stdio(0); cin.tie(0);
	cin.exceptions(ios::badbit | ios::failbit);
	cin >> T;
	while(T--)
	{
		cin >> N >> M;
		// clear
		P = 0;
		for(int i=1; i<=N; i++)
		{
			g[i].clear();
			h[i].clear();
		}
		memset(vis, 0, sizeof(bool)*(N+1));
		memset(reach, 0, sizeof(bool)*(N+1));
		memset(loop, 0, sizeof(bool)*(N+1));
		st.clear();

		for(int i=0; i<M; i++)
		{
			int a, b;
			cin >> a >> b;
			if (a == b) { loop[a] = true; continue; }
			g[a].push_back(b);
			h[b].push_back(a);
		}

		DFS(1);
		for(int i=1; i<=N; i++)
			if (vis[i])
				reach[i] = true;

		for(int i=1; i<=N; i++)
			if (!vis[i])
				DFS(i);

		memset(vis, 0, sizeof(bool)*(N+1));
		reverse(st.begin(), st.end()); // DFS 종료 역순 = 위상정렬순
		for(int n : st)
		{
			if (vis[n]) continue;
			sz[P] = 0;
			DFS2(n);
			P++;
		}

		memset(vis, 0, sizeof(bool)*(N+1));
		for(int i=1; i<=N; i++)
			if (reach[i] && isScc(i) && !vis[i])
				DFS3(i); // 1 -> scc -> x가 가능한가?

		memset(dp, 0, sizeof(int)*(N+1)); // 0,1,2+
		dp[1] = 1;
		for(int n : st)
		{
			if (dp[n] > 2) dp[n] = 2;
			for(int next : g[n])
				dp[next] += dp[n];
		}

		for(int i=1; i<=N; i++)
		{
			if (reach[i] == false) cout << "0 ";
			else if (vis[i]) cout << "-1 ";
			else if (dp[i] >= 2) cout << "2 ";
			else cout << "1 ";
		}
		cout << "\n";
	}
	return 0;
}