#!/bin/python

import math
import os
import random
import re
import sys

# Complete the lagDuration function below.
def lagDuration(h1, m1, h2, m2, k):
    # Return an integer denoting the duration of time in minutes by which the clock has been lagging.
    if(h2>12 and (h1+k)<12):
        a1=(h2-12)*60+m2
        b1=(h1)*60+m1+k*60
        c1=b1-a1
        return c1
    elif(h2>12 and (h1+k)>12):
        a1=(h2-12)*60+m2
        b1=((h1+k)-12)*60+m1
        c1=b1-a1
        return c1
    elif((h1+k)>12 and h2<12):
        a1=(h2)*60+m2
        b1=((h1+k)-12)*60+m1
        c1=b1-a1
        return c1
    else:
        a1=(h2)*60+m2
        b1=(h1)*60+m1+k*60
        c1=b1-a1
        return c1
        

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    q = int(raw_input())

    for q_itr in xrange(q):
        h1M1H2M2 = raw_input().split()

        h1 = int(h1M1H2M2[0])

        m1 = int(h1M1H2M2[1])

        h2 = int(h1M1H2M2[2])

        m2 = int(h1M1H2M2[3])

        k = int(raw_input())

        result = lagDuration(h1, m1, h2, m2, k)

        fptr.write(str(result) + '\n')

    fptr.close()