#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <string>
#include <iostream>
#include <list>
#include <cstdlib>
#include <bitset>
#define lowbit(x) (x&(-x))
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
const int inf = 0x3f3f3f3f;
using namespace std;
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
int a, ta, b, tb;
int hh, mm;
scanf("%d%d%d%d", &a, &ta, &b, &tb);
scanf("%d:%d", &hh, &mm);
int beg = 5 * 60, end = 23 * 60 + 59, t = hh * 60 + mm;
int cnt = 0;
for (int i = 0; beg + i * b <= end; ++i)
{
if (beg + i * b<t && beg + i * b + tb>t)++cnt;
if (beg + i * b >= t && beg + i * b < t + ta)++cnt;
}
printf("%d\n", cnt);
}
// system("pause");
}