#include <bits/stdc++.h>
using namespace std;
const int NMAX = 200005;
int N, M;
vector<int> graph[NMAX];
vector<int> revgraph[NMAX];
vector<int> topo;
bitset<NMAX> seen;
int dp[NMAX];
vector<int> answer;
void dfs(int node) {
  seen[node] = 1;
  for (auto& x : graph[node])
    if (seen[x] == 0) dfs(x);
  topo.push_back(node);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int tests;
  cin >> tests;
  while (tests-- > 0) {
    cin >> N >> M;
    for (int i = 1; i <= M; ++i) {
      int u, v;
      cin >> u >> v;
      graph[u].push_back(v);
      revgraph[v].push_back(u);
    }
    for (int i = 1; i <= N; ++i)
      if (seen[i] == 0) dfs(i);
    reverse((topo).begin(), (topo).end());
    for (auto& i : topo) {
      for (auto& j : revgraph[i]) dp[i] = max(dp[i], dp[j] + 1);
      if (dp[i] == 2) {
        answer.push_back(i);
        dp[i] = -1;
      }
    }
    cout << answer.size() << "\n";
    for (auto& i : answer) cout << i << " ";
    cout << "\n";
    for (int i = 1; i <= N; ++i) {
      graph[i].clear();
      revgraph[i].clear();
      seen[i] = 0;
      dp[i] = 0;
    }
    answer.clear();
    topo.clear();
  }
  return 0;
}
