#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long T;
  cin >> T;
  while (T--) {
    long long N, M;
    cin >> N >> M;
    vector<long long> L[N + 1];
    for (long long i = 0; i < M; i++) {
      long long a, b;
      cin >> a >> b;
      L[b].push_back(a);
    }
    vector<long long> dp(N + 1);
    vector<bool> mk(N + 1);
    vector<long long> res;
    for (long long i = 1; i <= N; i++) {
      for (auto v : L[i]) {
        if (!mk[v]) {
          dp[i] = max(dp[i], dp[v] + 1);
        }
      }
      if (dp[i] > 1) {
        mk[i] = true;
        res.push_back(i);
      }
    }
    cout << res.size() << '\n';
    for (auto x : res) {
      cout << x << " ";
    }
    cout << '\n';
  }
  return 0;
}
