#include #include #include #include #include #include #include #include using namespace std; const int mod = 1000000007; int rd[100005]; bool vis[100005]; bool flag[100005]; vector > G; int main() { int t; scanf("%d",&t); while(t--) { int n,m,k; scanf("%d%d%d",&n,&m,&k); G.clear(); G.resize(n+5); memset(vis,0,sizeof(vis)); memset(flag,0,sizeof(flag)); memset(rd,0,sizeof(rd)); while(m--) { int l,r; scanf("%d%d",&l,&r); G[l].push_back(r); rd[r]++; } priority_queue,greater > q; for(int i=1;i<=n;i++) { if(rd[i]<=k) { vis[i]=1; q.push(i); } } long long _i=1; long long res=0; while(!q.empty()) { int v=q.top(); q.pop(); vis[v]=0; if(rd[v]>k) continue; flag[v]=1; //cout<