#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
int T, n, m, num[maxn];
vector<int> now[maxn];
void solve() {
  scanf("%d%d", &n, &m);
  int cnt = 0;
  for (int i = 1; i <= n; i++) now[i].clear(), num[i] = 0;
  for (int i = 1; i <= m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    now[y].push_back(x);
  }
  for (int i = 1; i <= n; i++)
    for (auto j : now[i])
      if (num[j] < 2) num[i] = max(num[i], num[j] + 1);
  for (int i = 1; i <= n; i++)
    if (num[i] == 2) cnt++;
  printf("%d\n", cnt);
  for (int i = 1; i <= n; i++)
    if (num[i] == 2) printf("%d ", i);
  printf("\n");
}
int main() {
  scanf("%d", &T);
  while (T--) solve();
  return 0;
}
