#include <bits/stdc++.h>
#define monke_flip ios_base::sync_with_stdio(false); cin.tie(NULL);
#define random_monke srand(chrono::system_clock::now().time_since_epoch().count());
#ifdef LEL
#include <dbg.h>
#else
#define dbg(...) {/* temon kichu na; */}
#endif

using namespace std;
using ll = long long;
const int MONKE = 0;

class SCC
{
    private:
    vector <bool> vis;
    vector <int> order, component,idx;
    vector <vector <int> > adj, rev,COMPONENTS,DAG;
    vector <set<int> > _DAG;
    void init(int n,vector <pair<int,int> > &edges){
        adj.clear(),rev.clear(), COMPONENTS.clear(),DAG.clear(),_DAG.clear();
        adj.resize(n),rev.resize(n);
        for(auto &e:edges){
            adj[e.first].push_back(e.second);
            rev[e.second].push_back(e.first);
        }
    }
    void dfs1(int node){
        vis[node] = true;
        for(auto x:adj[node]){
            if(!vis[x]) dfs1(x);
        }
        order.push_back(node);
    }

    void dfs2(int node){
        vis[node] = true;
        component.push_back(node);
        for(auto x:rev[node]){
            if(!vis[x]) dfs2(x);
        }
    }
    public:
    vector <vector <int> > get_SCC(int n,vector <pair<int,int> > &edges){
        init(n,edges);
        vis.assign(n,false);
        order.clear();
        for(int i=0;i<n;i++){
            if(!vis[i]){
                dfs1(i);
            }
        }
        vis.assign(n,false);
        idx.assign(n,0);
        reverse(order.begin(), order.end());
        int i = 0;
        for(auto v:order){
            component.clear();
            if(!vis[v]) dfs2(v);
            if(!component.empty()){
                COMPONENTS.push_back(component);
                for(auto x:component){
                    idx[x] = i;
                }
                i++;
            }
        }
        int sz = COMPONENTS.size();
        _DAG.resize(sz), DAG.resize(sz);
        for(auto x:edges){
            _DAG[idx[x.first]].insert(idx[x.second]);
        }
        for(int i=0;i<sz;i++){
            for(auto x:_DAG[i]){
                if(x==i) continue;
                DAG[i].push_back(x);
            }
        }
        return COMPONENTS;
    }
    vector <int> get_idx(){
        return idx;
    }
    vector <vector <int> > get_DAG(){
        return DAG;
    }
};

void solve(){
    int _n, m;
    cin >> _n >> m;
    SCC G;
    set <int> self;
    vector <pair<int,int>> edges(m);
    for(auto &[u,v]:edges){
        cin >> u >> v;
        u--, v--;
        if(u == v)
            self.insert(u);
    }
    vector <vector <int> > scc, DAG;
    vector <int> new_idx;
    
    scc = G.get_SCC(_n,edges);
    DAG = G.get_DAG();
    new_idx = G.get_idx();

    int n = scc.size();
    vector <bool> inf(n);
    for(int i=0; i<n; i++){
        inf[i] = (int(scc[i].size()) > 1);
    }
    for(auto x:self){
        inf[new_idx[x]] = true;
    }
    vector <vector <int> > REV(n);
    for(int u = 0; u<n; u++){
        for(auto v:DAG[u]){
            REV[v].push_back(u);
        }
    }

    int root = new_idx[0];
    vector <int> dp(n);
    vector <bool> vis(n);
    dp[root] = (inf[root] ? -1:1);
    vis[root] = true;

    function <int(int)> call = [&](int node){
        if(vis[node])
            return dp[node];
        vis[node] = true;
        int cnt1 = 0, cnt2 = 0;
        for(auto x:REV[node]){
            int c = call(x);
            c == -1? cnt2++: cnt1 += c;
        }
        if(cnt2 == 0 and cnt1 == 0){
            return dp[node] = 0;
        }
        if(inf[node] or cnt2 > 0)
            return dp[node] = -1;
        return dp[node] = (cnt1 > 1 ? 2:1);
    };
    vector <int> ans(_n);
    for(int i=0;i<n;i++){
        int val = call(i);
        for(auto x:scc[i])
            ans[x] = val;
    }
    for(auto x:ans)
        cout<<x<<' ';
    cout<<'\n';
}

int main()
{
    monke_flip
    int t = 1; 
    cin>>t;
    for(int tc=1; tc<=t; tc++){
        solve();
    }
    return MONKE;
}