#include<bits/stdc++.h>
using namespace std;
long long mod = 998244353;
long long n , m , dp [400005] , wt [400005] , num [400005] , vis [400005];
vector < long long > gr [400005] , ngr [400005];
void dfs ( int x )
{
    if ( wt [x] ) return;
    vis [x] = 1;
    for ( int i = 0 ; i < ngr [x] . size () ; i ++ )
    {
        int u = ngr [x][i];
        if ( vis [u] ) continue;
        dp [u] += dp [x];
        if ( dp [u] < 1e9 ) dp [u] = min ( dp [u] , 4ll );
        num [u] --;
        if ( num [u] < 0 ) cout << 0 / 0;
        if ( num [u] == 0 ) dfs ( u );
    }
}
void go ( int x )
{
    vis [x] = 1;
    wt [x] = 1;
    for ( int i = 0 ; i < ngr [x] . size () ; i ++ )
    {
        int u = ngr [x][i];
        if ( vis [u] ) continue;
        go ( u );
    }
}
void cf ( int x )
{
    vis [x] = 1;
    for ( int i = 0 ; i < gr [x] . size () ; i ++ )
    {
        int u = gr [x][i];
        ngr [x] . push_back ( u );
        num [u] ++;
        if ( vis [u] == 1 ) wt [x] = 1;
        else if ( vis [u] == 0 ) cf ( u );
    }
    vis [x] = 2;
}
int main()
{
    int t;
    cin >> t;
    while ( t -- )
    {
        cin >> n >> m;
        for ( int i = 0 ; i <= n ; i ++ )
        {
            gr [i] . clear () , ngr [i] . clear ();
            dp [i] = 0 , wt [i] = 0 , num [i] = 0 , vis [i] = 0;
        }
        for ( int i = 0 ; i < m ; i ++ )
        {
            int x , y;
            cin >> x >> y;
            gr [x] . push_back ( y );
        }
        cf ( 1 );
        for ( int i = 1 ; i <= n ; i ++ ) vis [i] = 0;
        for ( int i = 1 ; i <= n ; i ++ )
        {
            if ( vis [i] == 0 && wt [i] ) go ( i );
        }
        for ( int i = 1 ; i <= n ; i ++ ) vis [i] = 0;
        dp [1] = 1;
        dfs ( 1 );
        for ( int i = 1 ; i <= n ; i ++ )
        {
            if ( wt [i] ) cout << -1 << ' ';
            else if ( dp [i] > 1 ) cout << 2 << ' ';
            else cout << dp [i] << ' ';
        }
        cout << endl;
    }
}
/*
3
4 5
1 2
2 1
2 3
3 4
4 2



5 5
1 5
4 5
2 3
3 4
4 2


6 7
2 3
3 4
4 2
1 5
1 6
5 6
4 5
*/
