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 nh=0,nm=0,h11=0;
        
        if(h1+k<24)
        {
            h11=h1+k;
            //m1=m1;
        }

        if(h1==h2 && m1==m2)
            return 0;
     
        else
        {
            if(h1!=h2 || k>1)
             nh=(h11-h2)*60;
             System.out.print(nh);
             
            if(m1==0)
            {
                m1=60;
            }
            
            if(m2==0)
            {
                m2=60;
            }
            
            if(m1<m2)
            {
              nm=m2-m1;
             nh=nh-nm;    
            } 
            
            else
               
            {
                nm=m1-m2;
                
            }
            
            //nm=Math.abs(m1-m2);
            return nh+nm;
        }
        
    }

    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();
    }
}