AC CODE:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int f[101][101];
bool fr[101][101];
char kf[101];
char hgd[101][101];
void make(int a,int b)
{
int j;
for(j=0; j<kf[a]; j++)
{
int t1=f[a][j];
int t2=b;
if(t1>t2)swap(t1,t2);
hgd[t1][t2]++;
}
for(j=0; j<kf[b]; j++)
{
int t1=f[b][j];
int t2=a;
if(t1>t2)swap(t1,t2);
hgd[t1][t2]++;
}
f[a][ kf[a]++ ]=b;
f[b][ kf[b]++ ]=a;
}
int main()
{
int T;
cin>>T;
while(T--)
{
int p,n,k;
cin>>p>>n>>k;
memset(fr,0,sizeof(fr));
memset(hgd,0,sizeof(hgd));
memset(kf,0,sizeof(kf));
int i,j;
int a,b;
for(i=0; i<n; i++)
{
scanf("%d%d",&a,&b);
fr[a][b]=fr[b][a]=true;
for(j=0; j<kf[a]; j++)
{
int t1=f[a][j];
int t2=b;
if(t1>t2)swap(t1,t2);
hgd[t1][t2]++;
}
for(j=0; j<kf[b]; j++)
{
int t1=f[b][j];
int t2=a;
if(t1>t2)swap(t1,t2);
hgd[t1][t2]++;
}
f[a][ kf[a]++ ]=b;
f[b][ kf[b]++ ]=a;
}
int count=0;
while(1)
{
int base=count;
for(i=0; i<p-1; i++)
{
for(j=i+1; j<p; j++)
{
if(hgd[i][j]>=k&&(!fr[i][j]))
{
fr[i][j]=true;
base++;
make(i,j);
}
}
}
if(count==base)break;
count=base;
}
cout<<count<<endl;
}
return 0;
}