#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const long long mod = 1000000007;
const long long linf = 0x3f3f3f3f3f3f3f3f;
vector<int> g[200100];
int nivel[200100];
int in[200100];
void solve() {
  int n, m;
  cin >> n >> m;
  int a, b;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    g[a].push_back(b);
    in[b]++;
  }
  queue<int> q;
  for (int i = 1; i <= n; i++)
    if (!in[i]) q.push(i);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (auto v : g[x]) {
      if (nivel[x] == 1) nivel[v] = 2;
      if (nivel[x] == 0 && nivel[v] != 2) nivel[v] = 1;
      in[v]--;
      if (!in[v]) q.push(v);
    }
  }
  vector<int> ans;
  for (int i = 1; i <= n; i++) {
    if (nivel[i] == 2) ans.push_back(i);
  }
  cout << ans.size() << "\n";
  for (auto p : ans) cout << p << " ";
  cout << "\n";
  for (int i = 1; i <= n; i++) nivel[i] = 0, g[i].clear(), in[i] = 0;
}
int main() {
  cin.tie(0)->sync_with_stdio(0);
  ;
  int t_;
  cin >> t_;
  while (t_--) solve();
  return 0;
}
