#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, m;
  cin >> n >> m;
  vector<vector<int> > par(n);
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    par[v].push_back(u);
  }
  vector<int> mark(n, 0);
  vector<int> res;
  for (int i = 0; i < n; i++) {
    for (auto p : par[i]) {
      mark[i] = max(mark[i], mark[p] + 1);
    }
    if (mark[i] > 1) {
      mark[i] = -1;
      res.push_back(i);
    }
  }
  cout << res.size() << "\n";
  for (auto i : res) cout << i + 1 << " ";
  cout << "\n";
}
int main() {
  ios::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) solve();
}
