#include <bits/stdc++.h>
using namespace std;
const long long N = 3e5 + 2;
const long long Mod = 2520;
const long long INF = 3e9 + 1;
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    vector<vector<long long> > G(n + 1);
    vector<long long> in(n + 1), out(n + 1);
    while (m--) {
      int u, v;
      cin >> u >> v;
      G[u].push_back(v);
    }
    vector<long long> lp(n + 1);
    vector<long long> ans;
    for (int i = 1; i <= n; i++) {
      if (lp[i] == 2) {
        ans.push_back(i);
        continue;
      }
      for (auto j : G[i]) lp[j] = max(lp[j], lp[i] + 1);
    }
    cout << ans.size() << '\n';
    for (auto i : ans) cout << i << ' ';
    cout << '\n';
  }
  return 0;
}
