#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, m;
  cin >> n >> m;
  vector<vector<int> > ch(n);
  for (int _ = (0); _ < (m); ++_) {
    int x, y;
    cin >> x >> y;
    --x, --y;
    if (!count((ch[x]).begin(), (ch[x]).end(), y)) {
      ch[x].push_back(y);
    }
  }
  vector<int> danger(n, 0);
  int cnt = 0;
  for (int i = (0); i < (n); ++i) {
    if (danger[i] <= 1)
      for (auto &j : ch[i]) danger[j] = max(danger[j], danger[i] + 1);
    else
      ++cnt;
  }
  cout << cnt << endl;
  for (int i = (0); i < (n); ++i)
    if (danger[i] == 2) cout << i + 1 << " ";
  cout << endl;
}
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  int t;
  cin >> t;
  for (int _ = (0); _ < (t); ++_) solve();
}
