#include <bits/stdc++.h>
using namespace std;
int main() {
  int test;
  cin >> test;
  while (test--) {
    vector<vector<int> > arr;
    vector<int> tmp;
    int n, m;
    cin >> n >> m;
    tmp.resize(n), arr.resize(n);
    for (int i = 0; i < m; i++) {
      int a, b;
      cin >> a >> b;
      a--, b--;
      arr[a].push_back(b);
    }
    vector<int> ans;
    for (int i = 0; i < n; i++) {
      if (tmp[i] < 2)
        for (int x : arr[i]) tmp[x] = max(tmp[x], tmp[i] + 1);
      else
        ans.push_back(i);
    }
    cout << ans.size() << "\n";
    for (auto it : ans) cout << it + 1 << " ";
    cout << "\n";
  }
}
