#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const ll MOD = 1e9 + 7;
const int iinf = 1 << 29;
const long long llinf = 1ll << 60;
const double PI = 3.14159265;
#define pb push_back
#define ff first
#define ss second
#define sz(a) ((int)(a).size())
#define all(x) (x).begin(),(x).end()
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll mrand(ll B) {
  return (ull)rng() % B;
}

inline long long ceil(long long x, long long y) {
    return (x + y - 1LL) / y;
}

void dfs(vector<vector<int>> &g, vector<bool> &has_self, vector<int> &status, int node, int val, vector<bool> &in_path) {
    if (val <= status[node]) return;
    if (has_self[node]) val = 3;
    status[node] = val;
    in_path[node] = true;
    for (auto v : g[node]) {
        if (val == 3 || in_path[v]) {
            if (status[v] != 3) dfs(g, has_self, status, v, 3, in_path);
            continue;
        }

        if (status[v] == 0 || status[v] == 1) dfs(g, has_self, status, v, status[v] + 1, in_path);
    }
    in_path[node] = false;
}

void work() {
    int n, m; cin >> n >> m;
    vector<vector<int>> g(n);
    vector<bool> has_self(n, false);
    vector<int> status(n, 0);
    for (int i = 0; i < m; i++) {
        int u, v; cin >> u >> v; u--; v--;
        if (u == v) {
            has_self[u] = true;
        } else {
            g[u].pb(v);
        }
    }
    vector<bool> in_path(n, false);
    dfs(g, has_self, status, 0, 1, in_path);
    for (int i = 0; i < n; i++) cout << (status[i] == 3 ? -1 : status[i]) << ' ';
    cout << '\n';
}

int main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int nt; cin>>nt; while (nt--) work();
    //work();
    return 0;
}
