#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int q;
  cin >> q;
  while (q--) {
    int n, m;
    cin >> n >> m;
    vector<vector<int> > g(n);
    for (int a, b, i = 0; i < m; i++) {
      cin >> a >> b;
      g[--b].push_back(--a);
    }
    vector<int> ending(n), ans;
    for (int i = 0; i < n; i++) {
      for (int& last : g[i]) {
        ending[i] = max(ending[i], ending[last] + 1);
      }
      if (ending[i] == 2) {
        ending[i] = -1;
        ans.push_back(i);
      }
    }
    cout << ans.size() << '\n';
    for (int& e : ans) cout << e + 1 << " ";
    cout << '\n';
  }
  return 0;
}
