#include <bits/stdc++.h>
using namespace std;
short int max(short int a, short int b) { return a > b ? a : b; }
int main() {
  int t, n, m;
  short int Clr[200000];
  pair<int, int> Sons[200000];
  scanf("%i", &t);
  while (t--) {
    scanf("%i%i", &n, &m);
    memset(Clr, 0, n * 2);
    for (int i = 0; i < n; i++) Sons[i].first = Sons[i].second = -1;
    for (int i = 0; i < m; i++) {
      int x, y;
      scanf("%i%i", &x, &y);
      if (Sons[x - 1].first == -1)
        Sons[x - 1].first = y - 1;
      else
        Sons[x - 1].second = y - 1;
    }
    int ans = 0;
    for (int i = 0; i < n; i++) {
      if (Clr[i] == 2) ans++;
      if (Clr[i] < 2 && Sons[i].first != -1)
        Clr[Sons[i].first] = max(Clr[Sons[i].first], Clr[i] + 1);
      if (Clr[i] < 2 && Sons[i].second != -1)
        Clr[Sons[i].second] = max(Clr[Sons[i].second], Clr[i] + 1);
    }
    printf("%i\n", ans);
    for (int i = 0; i < n; i++) {
      if (Clr[i] == 2) printf("%i ", i + 1);
    }
    printf("\n");
  }
}
