#include <cstdio>#include <climits>usingnamespacestd;
constintN=110;
constintDAY=24*60;
structBus{
intarriveTime, travelTime;
};
Busbus[N];
intn, curTime;
boolinput();
intsolve();
intmain()
{
#ifndef ONLINE_JUDGEfreopen("d:\\OJ\\uva_in.txt", "r", stdin);
#endifintt;
scanf("%d", &t);
for (inti=1; i<=t; i++) {
input();
printf("Case %d: %d\n", i, solve());
}
return0;
}
boolinput()
{
inth, m;
scanf("%d %d:%d", &n, &h, &m);
curTime=h*60+m;
for (inti=0; i<n; i++) {
scanf("%d:%d %d", &h, &m, &bus[i].travelTime);
bus[i].arriveTime=h*60+m;
}
returntrue;
}
intsolve()
{
intans=INT_MAX;
for (inti=0; i<n; i++) {
if (curTime>bus[i].arriveTime) {
bus[i].arriveTime+=DAY;
}
intcost=bus[i].arriveTime-curTime+bus[i].travelTime;
if (cost<ans) ans=cost;
}
returnans;
}