#include <bits/stdc++.h>
using namespace std;
struct edge {
  int s, f;
  bool operator<(const edge& rhs) const { return s < rhs.s; };
} A[400005];
int res[200005];
map<int, bool> incoming, unusable;
int main() {
  int T;
  cin >> T;
  for (int k = 0; k < T; k++) {
    int N, M;
    cin >> N >> M;
    for (int i = 0; i < M; i++) {
      cin >> A[i].s >> A[i].f;
    }
    sort(A, A + M);
    int cnt = 0;
    for (int i = 0; i < M; i++) {
      int now = A[i].s, next = A[i].f;
      if (unusable[now]) continue;
      if (incoming[now]) {
        unusable[next] = true;
      } else {
        incoming[next] = true;
      }
    }
    for (int i = 1; i <= N; i++) {
      if (unusable[i]) {
        res[cnt++] = i;
      }
    }
    cout << cnt << endl;
    for (int i = 0; i < cnt; i++) cout << res[i] << " ";
    cout << endl;
    incoming.clear();
    unusable.clear();
  }
  return 0;
}
