#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<string>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn = 10010;
const int inf = 0x3f3f3f3f;
int n, m, N, t;
int main()
{
scanf("%d", &t);
while (t--)
{
int a, ta, b, tb, th, tm;
scanf("%d %d %d %d", &a, &ta, &b, &tb);
scanf("%d:%d", &th, &tm);
int ans = 0;
n = th * 60 + tm;
m = n + ta;
if (m > 23 * 60 + 59)
m = 23 * 60 + 59;
else
m--;
n -= tb;
if (n < 5 * 60)
n = 5 * 60;
else
n++;
if ((n - 5 * 60) % b == 0)
ans++;
ans += (m - 5 * 60) / b - (n - 5 * 60) / b;
printf("%d\n", ans);
}
return 0;
}