#include <bits/stdc++.h>
using namespace std;
void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
void ran() { srand(chrono::steady_clock::now().time_since_epoch().count()); }
long long get_rand() {
  long long a = rand();
  long long b = rand();
  return a * (RAND_MAX + 1ll) + b;
}
void solve() {
  int n, m;
  cin >> n >> m;
  vector<int> subs[n + 1], inc[n + 1];
  int labels[n + 1];
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    subs[a].push_back(b);
    inc[b].push_back(a);
  }
  vector<int> rem;
  for (int i = 1; i <= n; i++) {
    int from0 = 0, from1 = 0, from2 = 0, total = 0;
    for (auto x : inc[i]) {
      total++;
      if (labels[x] == 0) {
        from0++;
      } else if (labels[x] == 1) {
        from1++;
      } else {
        from2++;
      }
    }
    if (from2 == total) {
      labels[i] = 0;
    } else if (from1 == 0) {
      labels[i] = 1;
    } else {
      rem.push_back(i);
      labels[i] = 2;
    }
  }
  cout << rem.size() << endl;
  for (auto x : rem) cout << x << ' ';
  cout << endl;
}
signed main() {
  ran();
  fast();
  int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
}
