#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
const int maxm = 2e6 + 10;
const int INF = 1e9;
int cnt, head[maxn], dis[maxn];
int n, m;
bool vis[maxn];
struct Edge {
  int from, to, w, next;
  Edge() {}
  Edge(int from, int to, int w, int next)
      : from(from), to(to), w(w), next(next) {}
} edge[maxm];
void addedge(int from, int to, int w) {
  edge[cnt] = Edge(from, to, w, head[from]);
  head[from] = cnt++;
}
map<pair<int, int>, bool> qwq;
vector<int> ans;
int main() {
  int t;
  cin >> t;
  while (t--) {
    ans.clear();
    qwq.clear();
    cnt = 0;
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
      head[i] = -1;
      dis[i] = vis[i] = 0;
    }
    for (int i = 1; i <= m; i++) {
      int f, t;
      cin >> f >> t;
      addedge(t, f, 1);
    }
    for (int u = 1; u <= n; u++) {
      for (int i = head[u]; ~i; i = edge[i].next) {
        int v = edge[i].to;
        dis[u] = max(dis[u], dis[v] + 1);
      }
      if (dis[u] == 2) {
        dis[u] = -1;
        ans.push_back(u);
      }
    }
    cout << ans.size() << endl;
    for (int x : ans) cout << x << " ";
    cout << endl;
  }
  return 0;
}
