#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long int n, m;
  cin >> n >> m;
  vector<vector<long long int>> v(n + 1);
  for (long long int i = 1; i <= m; i++) {
    long long int x, y;
    cin >> x >> y;
    v[y].push_back(x);
  }
  vector<long long int> paths(n + 1);
  vector<long long int> seen(n + 1);
  vector<long long int> ans;
  for (long long int i = 1; i <= n; i++) {
    for (auto &it : v[i]) {
      if (!seen[it]) {
        paths[i] = max(paths[it] + 1, paths[i]);
      }
    }
    if (paths[i] >= 2) {
      ans.push_back(i);
      seen[i] = 1;
    }
  }
  cout << (long long int)ans.size() << "\n";
  for (auto &it : ans) {
    cout << it << " ";
  }
  cout << "\n";
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t;
  cin >> t;
  while (t--) {
    solve();
  }
}
