#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long T;
  cin >> T;
  while (T--) {
    long long N, M;
    cin >> N >> M;
    vector<vector<long long> > g(N);
    for (long long i = 0; i < M; i++) {
      long long a, b;
      cin >> a >> b;
      a--;
      b--;
      g[b].push_back(a);
    }
    vector<long long> X(N, -1);
    vector<long long> res;
    for (long long i = 0; i < N; i++) {
      X[i] = 0;
      for (long long j : g[i]) {
        X[i] = max(X[i], X[j] + 1);
      }
      if (X[i] >= 2) {
        X[i] = -1;
        res.push_back(i);
      }
    }
    cout << res.size() << "\n";
    for (long long i = 0; i < res.size(); i++) {
      if (i > 0) cout << " ";
      cout << res[i] + 1;
    }
    cout << "\n";
  }
  return 0;
}
