#include<iostream>
#include<string>
#include<algorithm>
#include<queue>
#include<set>
#include<vector>
#include<cstring>
using namespace std;
const int N = 4e5 + 5;
int t, n, m;
int color[N];
int c[2][N];

vector<vector<int> >e;
vector<int>group[2];

void dfs1(int u) {
	color[u] = 1;
	for (auto v:e[u]) {
		if (color[v] == 0)
			dfs1(v);
		else
			group[color[v] - 1].push_back(v);
	}
	color[u] = 2;
}

void dfs2(int u, int i) {
	c[i][u] = 1;
	for (auto v : e[u]) {
		if (c[i][v] == 0)
			dfs2(v, i);
	}
	c[i][u] = 2;
}

int main() {
	cin >> t;
	while (t--) {
		cin >> n >> m;
		e = vector<vector<int> >(n + 1);
		memset(color, 0, sizeof(color));
		memset(c, 0, sizeof(c));
		group[0].clear();
		group[1].clear();
		for (int i = 0; i < m; i++) {
			int a, b;
			cin >> a >> b;
			e[a].push_back(b);
		}
		dfs1(1);
		for (int i = 0; i < 2; i++) {
			for (auto u:group[i]) {
				dfs2(u, i);
			}
		}
		for (int i = 1; i <= n; i++) {
			int ans = 0;
			if (color[i]) {
				ans = 1;
				if (c[0][i])
					ans = -1;
				else if (c[1][i])
					ans = 2;
			}
			cout << ans << ' ';
		}
		cout << endl;
	}
}