#include <bits/stdc++.h>
using namespace std;
int main() {
int n,e,t,u,v,co;
scanf("%d",&t);
for(int z=1;z<=t;z++){
scanf("%d %d",&n,&e);
int indeg[n];
int col[n];
vector<int> gr[n];
vector<int> rgr[n];
memset(col, 0, sizeof(col));
memset(indeg, 0, sizeof(indeg));
for(int i=0;i<e;i++){
scanf("%d %d",&u,&v);
gr[u-1].push_back(v-1);
rgr[v-1].push_back(u-1);
indeg[v-1]=1;
}
co=0;
for(int i=0;i<n;i++){
if(indeg[i]==0){
//printf("%d\n",i);
indeg[i]=1;
col[i]=1;
co++;
queue<int> q;
q.push(i);
while(!q.empty()){
u=q.front();
q.pop();
for(int j=0;j<gr[u].size();j++){
if(col[gr[u][j]]==0){
col[gr[u][j]]=1;
q.push(gr[u][j]);
}
}
}
}
}
for(int i=0;i<n;i++){
if(col[i]==0){
col[i]=1;
co++;
queue<int> q;
q.push(i);
while(!q.empty()){
u=q.front();
q.pop();
for(int j=0;j<rgr[u].size();j++){
if(col[rgr[u][j]]==0){
col[rgr[u][j]]=1;
q.push(rgr[u][j]);
}
}
}
q.push(i);
while(!q.empty()){
u=q.front();
q.pop();
for(int j=0;j<gr[u].size();j++){
if(col[gr[u][j]]==0){
col[gr[u][j]]=1;
q.push(gr[u][j]);
}
}
}
}
}
printf("Case %d: %d\n",z,co);
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgppbnQgbWFpbigpIHsKCWludCBuLGUsdCx1LHYsY287CglzY2FuZigiJWQiLCZ0KTsKCWZvcihpbnQgej0xO3o8PXQ7eisrKXsKCQlzY2FuZigiJWQgJWQiLCZuLCZlKTsKCQlpbnQgaW5kZWdbbl07CgkJaW50IGNvbFtuXTsKCQl2ZWN0b3I8aW50PiBncltuXTsKCQl2ZWN0b3I8aW50PiByZ3Jbbl07CgkJbWVtc2V0KGNvbCwgMCwgc2l6ZW9mKGNvbCkpOwoJCW1lbXNldChpbmRlZyw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