#include<stdio.h>
int main() {
int a, ta, b, tb, t, T;
scanf("%d", &T);
while (T--) {
int h, m, right, left, n, date = 0;
scanf("%d%d%d%d", &a, &ta, &b, &tb);
scanf("%d:%d", &h, &m);
t = h * 60 + m;
t -= 300;
left = t - tb;
right = t + ta;
if (left < 0)left = -1;
if (right > 1139)right = 1140;
for (n = left + 1; n < right; n++) {
if (!(n%b))date++;
}
printf("%d\n", date);
}
return 0;
}