#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200005;
const long long MOD = 1000000007;
vector<int> edge[MAXN];
bool rmv[MAXN], one[MAXN];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ;
  int T;
  int n, m;
  cin >> T;
  while (T--) {
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
      int f, t;
      cin >> f >> t;
      edge[t].emplace_back(f);
    }
    vector<int> ans;
    for (int i = 1; i <= n; i++) {
      for (auto v : edge[i]) {
        if (one[v]) {
          rmv[i] = true;
          ans.emplace_back(i);
          break;
        }
      }
      if (rmv[i]) {
        continue;
      }
      for (auto v : edge[i]) {
        if (!rmv[v]) {
          one[i] = true;
          break;
        }
      }
    }
    cout << int(ans.size()) << '\n';
    for (int i = 0; i < int(ans.size()); i++) {
      cout << ans[i] << " \n"[i == int(ans.size()) - 1];
    }
    for (int i = 1; i <= n; i++) {
      edge[i].clear();
      edge[i].shrink_to_fit();
      rmv[i] = false;
      one[i] = false;
    }
  }
}
