#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    vector<int> g[n];
    for (int i = 0; i < m; i++) {
      int u, v;
      cin >> u >> v;
      u--, v--;
      g[v].push_back(u);
    }
    int l[n];
    for (int i = 0; i < n; i++) l[i] = 0;
    vector<int> a;
    for (int i = 0; i < n; i++) {
      for (auto j : g[i]) l[i] = max(l[i], l[j] + 1);
      if (l[i] == 2) {
        l[i] = -1;
        a.push_back(i);
      }
    }
    cout << a.size() << "\n";
    for (auto i : a) cout << i + 1 << " ";
    cout << "\n";
  }
}
