#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fll;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int T;
  cin >> T;
  while (T--) {
    int n, m;
    cin >> n >> m;
    vector<vector<int>> g(n);
    for (int i = 0; i < m; i++) {
      int a, b;
      cin >> a >> b;
      a--, b--;
      g[b].push_back(a);
    }
    vector<int> t(n), ans;
    for (int i = 0; i < n; i++) {
      bool tem0 = 0, tem1 = 0;
      for (int j : g[i]) {
        if (t[j] == 0) tem0 = 1;
        if (t[j] == 1) tem1 = 1;
      }
      if (tem1)
        t[i] = 2;
      else if (tem0)
        t[i] = 1;
      if (t[i] == 2) ans.push_back(i);
    }
    cout << ans.size() << '\n';
    for (int i : ans) cout << i + 1 << " ";
    cout << '\n';
  }
  exit(0);
}
