#include <bits/stdc++.h>
using namespace std;
const int M = 1000000007;
int ll_max = 1e6;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long int n, m, i, j, d, z;
  long long int x, y, t, count, p;
  cin >> t;
  while (t--) {
    cin >> n >> m;
    vector<vector<long long int>> adj(n, vector<long long int>());
    for (long long int i = 0; i < m; i++) {
      cin >> x >> y;
      adj[x - 1].push_back(y - 1);
    }
    vector<long long int> track(n, 0);
    vector<long long int> eliminate;
    for (long long int i = 0; i < n; i++) {
      if (track[i] < 2) {
        for (long long int u : adj[i]) {
          if (track[u] == 2) continue;
          track[u] = 1 + track[i];
        }
      }
      if (track[i] == 2) {
        eliminate.push_back(i);
      }
    }
    cout << eliminate.size() << endl;
    for (long long int i = 0; i < eliminate.size(); i++) {
      cout << eliminate[i] + 1 << " ";
    }
    cout << endl;
  }
  return 0;
}
