#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > G;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  for (int w = 0; w < t; ++w) {
    int n, m;
    cin >> n >> m;
    G = vector<vector<int> >(n);
    for (int i = 0; i < m; ++i) {
      int x, y;
      cin >> x >> y;
      --x;
      --y;
      G[x].push_back(y);
    }
    vector<int> used(n, 0);
    for (int i = 0; i < n; ++i) {
      if (used[i] == 1) continue;
      if (used[i] == 2) {
        for (int j = 0; j < G[i].size(); ++j) used[G[i][j]] = 1;
        continue;
      }
      for (int j = 0; j < G[i].size(); ++j) {
        if (used[G[i][j]] != 1) used[G[i][j]] = 2;
      }
    }
    int ans = 0;
    for (int i = 0; i < n; ++i) {
      if (used[i] == 1) ++ans;
    }
    cout << ans << '\n';
    for (int i = 0; i < n; ++i) {
      if (used[i] == 1) cout << i + 1 << ' ';
    }
    cout << '\n';
  }
}
