#include<map> #include<queue> #include<time.h> #include<limits.h> #include<cmath> #include<ostream> #include<iterator> #include<set> #include<stack> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #define rep_1(i,m,n) for(int i=m;i<=n;i++) #define mem(st) memset(st,0,sizeof st) int read() { int res=0,ch,flag=0; if((ch=getchar())=='-') //判断正负 flag=1; else if(ch>='0'&&ch<='9') //得到完整的数 res=ch-'0'; while((ch=getchar())>='0'&&ch<='9') res=res*10+ch-'0'; return flag?-res:res; } typedef long long ll; typedef pair<int,int> pii; typedef unsigned long long ull; typedef pair<double,double> pdd; const int inf = 0x3f3f3f3f; const int N = 4e5 + 5; int h[N],ne[N],idx,e[N]; int n, m; //由4/7 可知:是一个 三层完全二叉树 的子节点 //那么也就是 把三层二叉树的子节点都删掉,就行 void add(int a,int b) { e[idx]=b; ne[idx]=h[a]; h[a]=idx++; } void solve() { idx=0; cin >> n >> m; for (int i = 0; i <= n; i++) { h[i]=-1; } for (int i = 1; i <= m; i++) { int u, v; cin >> u >> v;//按拓扑序 add(v,u); } vector<int>dist(n+1,0); vector <int> ans; for (int i = 1; i <= n; i++) { for (int u=h[i]; u!=-1; u=ne[u]) { int j=e[u]; if(dist[i]==-1) continue; dist[i] = max(dist[j] + 1, dist[i]); } if (dist[i] == 2)//如果是2了,那么就是说明存在一个点,到这个点的距离是3 { dist[i] = -1;//在第三层,就要删掉 ans.push_back(i); } } cout << ans.size() << endl; for (auto it : ans) cout << it << ' '; cout << endl; } int main() { int t; cin>>t; while(t--) solve(); return 0; }