#include <bits/stdc++.h>
using namespace std;
const int M = 2e5 + 5;
int read() {
  int x = 0, y = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') y = ch == '-' ? -1 : 1, ch = getchar();
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  return x * y;
}
vector<int> G[M], FG[M];
map<pair<int, int>, bool> pd;
int n, m, xz, in[M], out[M], de[M], Ans[M];
bool vis[M], hav[M];
queue<int> Q;
void solve() {
  while (!Q.empty()) Q.pop();
  pd.clear();
  n = read(), m = read(), xz = n * 4 / 7;
  for (int i = 1; i <= n; i++)
    G[i].clear(), FG[i].clear(), vis[i] = hav[i] = 0, de[i] = 0;
  for (int i = 1; i <= m; i++) {
    int x = read(), y = read();
    if (pd[make_pair(x, y)]) continue;
    pd[make_pair(x, y)] = 1;
    G[x].push_back(y), in[y]++;
    FG[y].push_back(x), out[x]++;
  }
  for (int i = 1; i <= n; i++)
    if (!in[i]) Q.push(i), de[i] = 0;
  Ans[0] = 0;
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    if (de[u] >= 2) Ans[++Ans[0]] = u, hav[u] = 1;
    for (int v : G[u]) {
      in[v]--;
      if (de[u] < 2) de[v] += de[u] + 1;
      if (!in[v]) Q.push(v);
    }
  }
  if (Ans[0] > xz) return (void)(printf("/baojin\n"));
  printf("%d\n", Ans[0]);
  for (int i = 1; i <= Ans[0]; i++) printf("%d ", Ans[i]);
  printf("\n");
}
signed main() {
  int T = read();
  while (T--) solve();
}
