#include<bits/stdc++.h>
using namespace std;
const int N=200010,M=6000010;
const int inf=0x3f3f3f3f;
typedef long long ll;
#define pii pair<int,int>
#define mp make_pair
int intersect(int l1,int r1,int l2,int r2) { return max(0,min(r1,r2)-max(l1,l2)+1); }
int head[N],ver[M],edge[M],nxt[M],d[N];
int n,m,tot=1,s=N-1,t=N-2;
void add_edge(int x,int y,int z)
{
ver[++tot]=y,edge[tot]=z,nxt[tot]=head[x],head[x]=tot;
}
void add(int x,int y,int z)
{
add_edge(x,y,z),add_edge(y,x,0);
}
queue<int> q;
bool bfs()
{
while(q.size()) q.pop();
q.push(s);
for(int i=1;i<=m+n;i++) d[i]=0;
d[t]=0,d[s]=1;
while(q.size())
{
int x=q.front(); q.pop();
for(int i=head[x];i;i=nxt[i])
{
int y=ver[i];
if(edge[i]&&!d[y])
{
d[y]=d[x]+1;
q.push(y);
}
}
}
return d[t];
}
int dinic(int x,int flow)
{
if(x==t||!flow) return flow;
int rest=flow,k;
for(int i=head[x];i&&rest;i=nxt[i])
{
int y=ver[i];
if(edge[i]&&d[y]==d[x]+1)
{
k=dinic(y,min(edge[i],rest));
if(!k) d[y]=0;
rest-=k,edge[i]-=k,edge[i^1]+=k;
}
}
return flow-rest;
}
int get_maxflow()
{
// for(int x=0;x<N;x++) if(head[x])
// {
// cerr<<x<<": \n";
// for(int i=head[x];i;i=nxt[i]) if(edge[i])
// cerr<<ver[i]<<" "<<edge[i]<<"\n";
// }
int maxflow=0,flow=0;
while(bfs()) while(flow=dinic(s,inf)) maxflow+=flow;
return maxflow;
}
map<pii,map<pii,int>> num;
map<pair<pii,pii>,int> id;
int id_cnt=0;
void sol()
{
num.clear(),id.clear(),id_cnt=0;
for(int i=1;i<=m+n;i++) head[i]=0;
head[s]=head[t]=0;
scanf("%d%d",&m,&n);
for(int i=1;i<=m;i++)
{
int x1,y1,x2,y2; scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
num[mp(x1,x2)][mp(y1,y2)]++;
}
for(auto [x,tmp]:num) for(auto [y,z]:tmp) add(s,id[mp(x,y)]=++id_cnt,z);
ll ans=0;Z`
for(int i=1;i<=n;i++)
{
int x1,y1,x2,y2; scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
add(m+i,t,1);
// cerr<<"("<<x1<<","<<x2<<"), ("<<y1<<","<<y2<<"): \n";
for(int l1=x1-3;l1<=x2-1;l1++) for(int l2=l1;l2<=l1+3;l2++) if(intersect(l1,l2,x1,x2)*2>=l2-l1+1)
for(int r1=y1-3;r1<=y2-1;r1++) for(int r2=r1;r2<=r1+3;r2++)
if(intersect(l1,l2,x1,x2)*intersect(r1,r2,y1,y2)*2>=(l2-l1+1)*(r2-r1+1))
{
// cerr<<"("<<l1<<","<<l2<<"), ("<<r1<<","<<r2<<") \n";
if(num[mp(l1,l2)].find(mp(r1,r2))!=num[mp(l1,l2)].end())
// ans+=num[mp(l1,l2)][mp(r1,r2)];
add(id[mp(mp(l1,l2),mp(r1,r2))],m+i,1);
}
}
printf("%d\n",get_maxflow());
// printf("%lld\n",ans);
}
int main()
{
int T; scanf("%d",&T);
while(T--) sol();
return 0;
}