//It is made by ljh2000
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL;
const int MAXN = 1211;
const int MAXM = 1000011;
int N,n,m,E,ecnt,first[MAXN],to[MAXM],next[MAXM],match[MAXN],pre[MAXN],vis[MAXN],Tim,dui[MAXM],head,tail,id[MAXN],Match,father[MAXN],ans;
inline void link(int x,int y){ next[++ecnt]=first[x]; first[x]=ecnt; to[ecnt]=y; next[++ecnt]=first[y]; first[y]=ecnt; to[ecnt]=x; }
inline int find(int x){ if(father[x]!=x) father[x]=find(father[x]); return father[x]; }
inline int getint(){
int w=0,q=0; char c=getchar(); while((c<‘0‘||c>‘9‘) && c!=‘-‘) c=getchar();
if(c==‘-‘) q=1,c=getchar(); while (c>=‘0‘&&c<=‘9‘) w=w*10+c-‘0‘,c=getchar(); return q?-w:w;
}
inline int lca(int x,int y){
Tim++;
while(vis[x]!=Tim) {
if(x){
x=find(x);
if(vis[x]==Tim) return x;
vis[x]=Tim;
if(match[x]!=0) x=find(pre[match[x]]);//!!!
else x=0;
}
swap(x,y);
}
return x;
}
inline void change(int x,int y,int k){
while(find(x)!=k) {
pre[x]=y; int z=match[x];//!!!
if(id[z]==1) { id[z]=0; dui[++tail]=z; }
if(find(z)==z) father[z]=k;
if(find(x)==x) father[x]=k;
y=z; x=pre[y];
}
}
inline bool bfs(int s){
for(int i=1;i<=N;i++) id[i]=-1,father[i]=i; head=tail=0;
dui[++tail]=s; id[s]=0; int u;
while(head<tail) {
head++; u=dui[head];
for(int i=first[u];i;i=next[i]) {
int v=to[i];
if(id[v]==-1) {
pre[v]=u; id[v]=1;
if(!match[v]) {
int last,t,now=v;
while(now!=0) {
t=pre[now]; last=match[t];
match[t]=now; match[now]=t;
now=last;
}
return true;
}
id[match[v]]=0; dui[++tail]=match[v];
}
else if(id[v]==0/*!!!*/ && find(u)!=find(v)) {
int g=lca(u,v);
change(u,v,g);
change(v,u,g);
}
}
}
return false;
}
inline void work(){
int T=getint(); int x,y;
while(T--) {
n=getint(); m=getint(); E=getint(); ecnt=0; memset(first,0,sizeof(first));
for(int i=1;i<=E;i++) {
x=getint(); y=getint(); x+=3*m;
link(x,(y-1)*3+1);
link(x,(y-1)*3+2);
link(x,(y-1)*3+3);
}
for(int i=1;i<=m;i++) {//每个框内部连边
int base=(i-1)*3+1;
link(base,base+1);
link(base,base+2);
link(base+1,base+2);
}
N=n+3*m; memset(match,0,sizeof(match)); memset(pre,0,sizeof(pre));
Tim=0; memset(vis,0,sizeof(vis)); Match=0;//!!!
for(int i=N;i>=1;i--) if(!match[i] && bfs(i)) Match++;
ans=Match-n;
printf("%d\n",ans);
}
}
int main()
{
work();
return 0;
}