#include <bits/stdc++.h>
using namespace std;
const int MAX = 2e5 + 10;
vector<int> adj[MAX];
int dist[MAX];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n, m, u, v;
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
      cin >> u >> v;
      adj[u].push_back(v);
    }
    vector<int> ans;
    for (int i = 1; i <= n; i++) {
      if (dist[i] == 2)
        ans.push_back(i);
      else {
        for (auto j : adj[i]) dist[j] = max(dist[j], dist[i] + 1);
      }
    }
    cout << ans.size() << endl;
    for (auto i : ans) cout << i << " ";
    cout << endl;
    for (int i = 1; i <= n; i++) adj[i].clear(), dist[i] = 0;
  }
}
