#include <bits/stdc++.h>
using namespace std;
int dis[200005];
vector<int> G[200005];
int main() {
  int cas;
  cin >> cas;
  while (cas--) {
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
      G[i].clear();
      dis[i] = 1;
    }
    for (int i = 0; i < m; i++) {
      int u, v;
      cin >> u >> v;
      G[u].push_back(v);
    }
    vector<int> ans;
    for (int i = 1; i <= n; i++) {
      if (dis[i] == 3) {
        ans.push_back(i);
        continue;
      }
      for (auto v : G[i]) {
        dis[v] = max(dis[v], dis[i] + 1);
      }
    }
    cout << ans.size() << endl;
    for (auto v : ans) cout << v << " ";
    cout << endl;
  }
  return 0;
}
