#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e6 + 5;
int n, m, T;
int head[maxn], e[maxn], net[maxn], cnt, ind[maxn], vis[maxn], d[maxn];
queue<int> q;
void add(int u, int v) {
  e[++cnt] = v;
  net[cnt] = head[u];
  head[u] = cnt;
}
void fr(int u) {
  vis[u] = 1;
  for (int i = head[u]; i; i = net[i]) {
    ind[e[i]]--;
    if (ind[e[i]] == 0 && !vis[e[i]]) {
      q.push(e[i]);
    }
  }
}
void fl(int u) {
  for (int i = head[u]; i; i = net[i]) {
    if (!vis[e[i]]) fr(e[i]);
  }
}
void sol() {
  for (int i = 1; i <= n; i++) {
    for (int j = head[i]; j; j = net[j]) {
      if (!vis[e[j]]) d[i] = max(d[i], d[e[j]] + 1);
    }
    if (d[i] >= 2) {
      vis[i] = 1;
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> T;
  while (T--) {
    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
      int u, v;
      cin >> u >> v;
      add(v, u);
      ind[v]++;
    }
    sol();
    int ans = 0;
    for (int i = 1; i <= n; i++) {
      if (vis[i]) {
        ans++;
      }
    }
    cout << ans << "\n";
    for (int i = 1; i <= n; i++) {
      if (vis[i]) {
        ans--;
        cout << i;
        cout << " ";
      }
    }
    cout << "\n";
    for (int i = 1; i <= n; i++) head[i] = ind[i] = vis[i] = d[i] = 0;
    cnt = 0;
  }
  return 0;
}
