import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

    // Complete the lagDuration function below.
    static int lagDuration(int h1, int m1, int h2, int m2, int k) {
        // Return an integer denoting the duration of time in minutes by which the clock has been lagging.
        int h11=h1+k;
        h1=h11;
        int min=0;
        int hour=0,result=0;
        if(h1==h2)
        {
            if(m1==m2)
                result=0;
            else if(m1==0)
            {
                m1=60;
                h1--;
                result=m1-m2;
                //return min;
            }
            else if(m1<m2)
            {
                h1--;
                m1=m1+60;
                result=m1-m2;
                //return min;
            }
            else if(m1>m2)
            {
                result=m1-m2;
                //return min;
            }
        }
        else
        {
            if(m1==m2){
                result=h1-h2;
            }
             else if(m1==0)
            {
                m1=60;
                --h1;
                min=m1-m2;
                hour+=(h1-h2)*60;
                result=min+hour;
                //return min;
            }
            else if(m1<m2)
            {
                --h1;
                m1=m1+60;
                min=m1-m2;
                hour=(h1-h2)*60;
                result=min+hour;
                //return min;
            }
            else if(m1>m2)
            {
                min=m1-m2;
                hour=(h1-h2)*60;
                result=min+hour;
                //return min;
            }
        }
        return result;
    }

    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) throws IOException {
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int q = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

        for (int qItr = 0; qItr < q; qItr++) {
            String[] h1M1H2M2 = scanner.nextLine().split(" ");

            int h1 = Integer.parseInt(h1M1H2M2[0]);

            int m1 = Integer.parseInt(h1M1H2M2[1]);

            int h2 = Integer.parseInt(h1M1H2M2[2]);

            int m2 = Integer.parseInt(h1M1H2M2[3]);

            int k = scanner.nextInt();
            scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

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

            bufferedWriter.write(String.valueOf(result));
            bufferedWriter.newLine();
        }

        bufferedWriter.close();

        scanner.close();
    }
}