#include <iostream>
#include <iomanip>
#include <ostream>
#include <istream>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <bitset>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <array>
#include <algorithm>
#include <functional>
#include <cmath>
#include <time.h>
#include <random>
#include <chrono>
#include <cassert>
#include <cstring>
#include <limits>

#define sz(v) ((int)(v).size())
#define all(a) (a).begin(), (a).end()
#define rall(a) a.rbegin(), a.rend()
#define YES cout<<"YES\n";return;
#define NO cout<<"NO\n";return;
#define pii pair<int, int>
#define time clock() / (double) CLOCKS_PER_SEC
#define graph vector<vector<int>>
#define F first
#define S second
using namespace std;

istream& operator>> (istream& in, pii& b) {
    in >> b.first >> b.second;
    return in;
}

ostream& operator<< (ostream& out, const pii& b) {
    out << b.first << " " << b.second << endl;
    return out;
}

template<typename T> ostream& operator<< (ostream& out, const vector<T>& a) {
    for (auto k : a) out << k;
    return out;
}

mt19937 gen(chrono::high_resolution_clock::now().time_since_epoch().count());
int inf = 2e18;
graph g;
vector<int> color;
vector<int> ans;
vector<bool> used;
set<int> a, b, aa, bb;
void color_dfs(int u){
    color[u] = 1;
    for(auto v: g[u]){
        if(!color[v]){
            color_dfs(v);
        } else if(color[v] == 1){
            a.insert(v);
        } else if(color[v] == 2){
            b.insert(v);
        }
    }
    color[u] = 2;
}
void dfs(int u, set<int> &st){
    st.insert(u);
    used[u] = 1;
    for(auto v: g[u]) {
        if (!used[v]){
            dfs(v, st);
        }
    }
}
void solve(){
    int n, m;
    cin >> n >> m;
    g.resize(0);
    used.resize(0);
    ans.resize(0);
    color.resize(0);
    set<int> t;
    a = t;
    b = t;
    aa = t;
    bb = t;
    g.resize(n);
    ans.resize(n, 1);
    color.resize(n, 0);
    used.resize(n, 0);
    for(int i = 0;i < m;i++){
        int u, v;
        cin >> u >> v;
        u--;
        v--;
        g[u].push_back(v);
    }
    color_dfs(0);
    for(int i = 0;i < n;i++){
        if(!color[i]){
            ans[i] = 0;
        }
    }
    for(auto v: a){
        if(!used[v]) {
            dfs(v, aa);
        }
    }
    for(auto v: b){
        if(!used[v]) {
            dfs(v, bb);
        }
    }
    for(auto v: bb){
        ans[v] = 2;
    }
    for(auto v: aa){
        ans[v] = -1;
    }
    for(auto val: ans){
        cout << val << " ";
    }
    cout << endl;
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int omg = 1;
    cin >> omg;
    while(omg--){
        solve();
    }
    return 0;
}