#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
bool out[200005], vis[200005];
vector<int> adj[200005];
vector<int> topo;
int n, m, k;
int cr = 0;
void dfs1(int i, int rem, int pr) {
  if (rem == 0) {
    if (out[pr]) return;
    out[pr] = 1;
    k++;
    return;
  }
  for (auto x : adj[i]) {
    if (out[x]) continue;
    dfs1(x, rem - 1, pr);
  }
}
void solve() {
  cin >> n >> m;
  k = 0;
  cr = 0;
  fill(out, out + n, false);
  fill(vis, vis + n, false);
  topo.clear();
  topo.resize(n);
  for (long long i = (long long)0; i != (long long)n; i = i + 1)
    adj[i] = vector<int>(0);
  for (long long i = (long long)0; i != (long long)m; i = i + 1) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    adj[y].push_back(x);
  }
  for (long long i = (long long)0; i != (long long)n; i = i + 1) {
    sort(adj[i].begin(), adj[i].end(), greater<int>());
  }
  for (long long i = (long long)0; i != (long long)n; i = i + 1) {
    dfs1(i, 2, i);
  }
  cout << k << '\n';
  for (long long i = (long long)0; i != (long long)n; i = i + 1) {
    if (out[i]) cout << i + 1 << " ";
  }
  cout << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    solve();
  }
}
