#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 11;
vector<long long> graph[N];
long long dp[N];
int32_t main() {
  ios::sync_with_stdio(false);
  long long t;
  cin >> t;
  while (t--) {
    long long n, m;
    cin >> n >> m;
    map<pair<long long, long long>, bool> mp;
    for (long long i = 0; i < m; ++i) {
      long long x, y;
      cin >> x >> y;
      graph[x].push_back(y);
    }
    set<long long> Set;
    for (long long i = 1; i <= n; ++i) {
      for (auto x : graph[i]) {
        if (Set.count(x)) continue;
        dp[x] = max(dp[x], dp[i] + 1);
        if (dp[x] == 2) {
          Set.insert(x);
          dp[x] = -1;
        }
      }
    }
    for (long long i = 1; i <= n; ++i) {
      dp[i] = 0;
      graph[i].clear();
    }
    cout << Set.size() << '\n';
    for (auto x : Set) cout << x << " ";
    cout << '\n';
  }
  return 0;
}
