#include <bits/stdc++.h>
using namespace std;
vector<int> l[200009], final;
int val[200009];
int main() {
  int i, j, t1, t2, t3, t4, t, n, m;
  scanf("%d", &t);
  while (t--) {
    scanf("%d %d", &n, &m);
    final.clear();
    for (i = 1; i <= n; i++) {
      l[i].clear();
      val[i] = 0;
    }
    for (i = 0; i < m; i++) {
      scanf("%d %d", &t1, &t2);
      l[t2].push_back(t1);
    }
    for (i = 1; i <= n; i++) {
      for (auto it : l[i]) {
        val[i] = max(val[i], val[it] + 1);
      }
      if (val[i] == 2) {
        final.push_back(i);
        val[i] = -1;
      }
    }
    assert((int) final.size() * 7 <= 4 * n);
    printf("%d\n", (int) final.size());
    for (auto it : final) {
      printf("%d ", it);
    }
    printf("\n");
  }
  return 0;
}
