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

#define   fi(a,b) for(int i=a;i<b;i++)
#define   fj(a,b) for(int j=a;j<b;j++)
#define   ff first
#define   ss second
#define   ll long long
#define   ld long double
#define   ull unsigned long long
#define   bp(x) __builtin_popcount(x)
#define   pr(x) for(auto it: x) cout<<it<<" "; cout<<endl;
#define   getMax(x) max_element(x.begin(),x.end())
#define   getMin(x) min_element(x.begin(),x.end())
#define   endl "\n"
typedef   vector<int> vi;
typedef   vector< pair<int, int> > vii;
typedef   vector<long long> vl;
typedef   pair<int, int> pii;
typedef   pair<ll, ll> pll;
typedef   vector< pair<ll, ll> > vll;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// auto dist = uniform_int_distribution<int>(l, r);
// int dx[] = {1, 0, -1, 0};
// int dy[] = {0, 1, 0, -1};
// int dx[] = { -1, 0, 1, 1, 1, 0, -1, -1};
// int dy[] = { -1, -1, -1, 0, 1, 1, 1, 0};

stack<int>order;
vector<bool>vis;
vector<vi>adj, Radj; //Adjacency list and its reverse //Strongly connected components
int sz = 0;

void dfs1(int node) {
    vis[node] = 1;
    for (auto it : adj[node]) {
        if (!vis[it]) dfs1(it);
    }
    order.push(node);
}

void dfs2(int node, vector<vi>&components) {
    vis[node] = 0;
    for (auto it : Radj[node]) {
        if (vis[it]) dfs2(it, components);
    }
    components[sz].push_back(node);
}

void init(int n) {
    adj.assign(n + 1, vi({}));
    Radj.assign(n + 1, vi({}));
    vis.assign(n + 1, 0);
    sz = 0;
}

/*****************In main function******************/

void omae_wa_mou_shindeiru(int tc) {
    ll n, m;
    cin >> n >> m;
    vl self;
    init(n);
    fi(0, m) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        Radj[v].push_back(u);
        if (u == v) self.push_back(u);
    }
    vector<int>cnt(n, 0);
    queue<int>q;
    vector<vi>components;
    q.push(1);
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        cnt[u - 1]++;
        if (cnt[u - 1] > 2) continue;
        for (auto v : adj[u]) q.push(v);
    }

    fi(0, n) {
        if (!vis[i]) dfs1(i);
    }

    while (!order.empty()) {
        int node = order.top();
        order.pop();
        if (vis[node]) {
            components.push_back({});
            dfs2(node, components);
            sz++;
        }
    }

    fi(0, sz) {
        if (components[i].size() <= 1) continue;
        for (auto v : components[i]) {
            if (cnt[v - 1]) self.push_back(v);
        }
    }
    // pr(cnt);
    for (auto v : self) {
        if (cnt[v - 1]) q.push(v);
    }
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        cnt[u - 1] = -1;
        for (auto v : adj[u]) {
            if (cnt[v - 1] != -1)
                q.push(v);
        }
    }
    fi(0, n) {
        cnt[i] = min(cnt[i], 2);
    }
    pr(cnt);

}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int tc = 1;
    cin >> tc;
    fi(1, tc + 1) {
        omae_wa_mou_shindeiru(i);
    }
}
