//二分匹配的最小路径覆盖
//对于第i次ride,如果在第i次ride结束后还能在第j次ride出发前赶到第j次的出发点
//那么i到j就有一条边
//根据最小路径覆盖 = N - 最大匹配即可得到答案
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
const int maxn = 510;
int line[maxn][maxn];
int match[maxn] ;
int vis[maxn] ;
int N;
int find(int start)
{
for(int i = 1 ; i <= N ; i++)
{
if(!vis[i] && line[start][i])
{
vis[i] = 1;
if(match[i] == -1 || find(match[i]))
{
match[i] = start ;
return 1;
}
}
}
return 0;
}
void Match()
{
memset(match , -1 ,sizeof(match)) ;
int ans = 0;
for(int i = 1 ; i <= N ;i++)
{
memset(vis, 0 ,sizeof(vis));
if(find(i))
ans ++ ;
}
printf("%d\n", N-ans);
}
struct node
{
int time_st;
int time_en;
int a,b,c,d;
}ride[maxn];
int main()
{
//freopen("in.txt" , "r" ,stdin);
int T;
scanf("%d", &T);
int h , m ;
while(T--)
{
memset(line, 0 ,sizeof(line));
scanf("%d", &N);
for(int i = 1 ; i <= N ;i++)
{
scanf("%d:%d",&h ,&m);
// cout<<h<<" "<<m<<endl;
ride[i].time_st = 60*h + m;
scanf("%d %d %d %d" , &ride[i].a ,&ride[i].b,&ride[i].c,&ride[i].d);
ride[i].time_en = abs(ride[i].a-ride[i].c) + abs(ride[i].b-ride[i].d) + ride[i].time_st;
for(int j = 1 ; j <= i ;j++)
{
int temp_1 = abs(ride[j].a-ride[i].c) + abs(ride[j].b-ride[i].d);
int temp_2 = abs(ride[i].a-ride[j].c) + abs(ride[i].b-ride[j].d);
if(ride[i].time_en+temp_1 < ride[j].time_st)
line[i][j] = 1;
else if(ride[j].time_en+temp_2 < ride[i].time_st)
line[j][i] = 1;
}
}
Match();
}
return 0;
}