#include <bits/stdc++.h>

using namespace std;

vector<vector<int>> g;
vector<int> color;
vector<bool> bv[2];

void dfs1(int v) {
  color[v] = 1;
  for (int u : g[v]) {
    if (color[u] == 0) dfs1(u);
    else bv[color[u]-1][u] = true;
  }
  color[v] = 2;
}

void dfs2(int v) {
  color[v] = 1;
  for (int u : g[v]) if (color[u] == 0) dfs2(u);
}

void solve() {
  int n, m;
  cin >> n >> m;
  g.clear();
  g.resize(n);
  while (m--) {
    int a, b;
    cin >> a >> b; --a; --b;
    g[a].push_back(b);
  }
  bv[0].assign(n, false);
  bv[1] = bv[0];
  color.assign(n, 0);
  vector<int> ans(n, 0);
  dfs1(0);
  color.assign(n, 0);
  for (int i = 0; i < n; ++i) if (bv[1][i]) dfs2(i);
  for (int i = 0; i < n; ++i) if (color[i] == 1) ans[i] = 2;
  color.assign(n, 0);
  for (int i = 0; i < n; ++i) if (bv[0][i]) dfs2(i);
  for (int i = 0; i < n; ++i) if (color[i] == 1) ans[i] = -1;
  color.assign(n, 0);
  dfs2(0);
  for (int i = 0; i < n; ++i) if (ans[i] == 0 && color[i]) ans[i] = 1;
  for (int i = 0; i < n; ++i) cout << ans[i] << ' ';
  cout << endl;
}

int main() {
  int t;
  cin >> t;
  while (t--) solve();
}