#include <bits/stdc++.h>
using namespace std;
struct Node {
  vector<int> edges;
  int in_degree = 0;
};
using Graph = vector<Node>;
vector<int> Solve(Graph &graph) {
  queue<int> q;
  for (size_t i = 0; i < graph.size(); i += 1) {
    if (graph[i].in_degree == 0) {
      q.push(i);
    }
  }
  vector<int> depth(graph.size(), 0);
  vector<int> to_remove;
  while (!q.empty()) {
    auto node = q.front();
    q.pop();
    if (depth[node] > 1) {
      to_remove.push_back(node);
      depth[node] = -1;
    }
    for (const auto &next : graph[node].edges) {
      depth[next] = max(depth[next], depth[node] + 1);
      graph[next].in_degree -= 1;
      if (graph[next].in_degree == 0) {
        q.push(next);
      }
    }
  }
  return to_remove;
}
int main() {
  int tests;
  cin >> tests;
  for (int i = 0; i < tests; i += 1) {
    int nodes, edges;
    cin >> nodes >> edges;
    Graph graph(nodes);
    for (int j = 0; j < edges; j += 1) {
      int a, b;
      cin >> a >> b;
      graph[a - 1].edges.push_back(b - 1);
      graph[b - 1].in_degree += 1;
    }
    auto res = Solve(graph);
    cout << res.size() << "\n";
    for (const auto &node : res) {
      cout << node + 1 << " ";
    }
    cout << "\n";
  }
  return 0;
}
