#include <bits/stdc++.h>
using namespace std;
const long long MXN = 4e5 + 10;
long long q, n, m;
long long sit[MXN];
vector<long long> adj[MXN], Top, Ans;
bool vis[MXN];
void dfs(long long u) {
  vis[u] = 1;
  for (auto v : adj[u]) {
    if (!vis[v]) dfs(v);
  }
  Top.push_back(u);
}
void solve() {
  cin >> n >> m, Top.clear(), Ans.clear();
  for (int i = 1; i <= n; i++) adj[i].clear(), vis[i] = 0, sit[i] = 83;
  for (int i = 1, u, v; i <= m; i++) cin >> u >> v, adj[u].push_back(v);
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) dfs(i);
  }
  reverse(Top.begin(), Top.end());
  for (int i = 0; i < n; i++) {
    long long u = Top[i];
    if (!sit[u]) continue;
    if (sit[u] == 2) {
      sit[u] = 1;
      for (auto v : adj[u]) sit[v] = 0;
    } else {
      sit[u] = 1;
      for (auto v : adj[u]) sit[v] = (!sit[v] ? 0 : 2);
    }
  }
  for (int u = 1; u <= n; u++) {
    if (!sit[u]) Ans.push_back(u);
  }
  cout << Ans.size() << '\n';
  for (auto X : Ans) cout << X << ' ';
  cout << '\n';
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> q;
  while (q--) solve();
  return 0;
}
