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 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t; //System.out.println("Enter the no of quires "); t=sc.nextInt(); int i,j; for(i=0;i<t;i++) { int h1,m1,h2,m2,k; //System.out.println("Enter the h1 value "); h1=sc.nextInt(); // System.out.println("Enter the m1 value "); m1=sc.nextInt(); // System.out.println("Enter the h2 value "); h2=sc.nextInt(); //System.out.println("Enter the m2 value "); m2=sc.nextInt(); //System.out.println("Enter the k value "); k=sc.nextInt(); if(h1==h2) { if(h1==h2&&k==1) { // System.out.println("it satisfied the condition"); if(m1==0) { m1=60; } System.out.println(Math.abs(m1-m2)); } else { k=k*60; int time = Math.abs(m1-m2); System.out.println(Math.abs(k-time)); } } else { h1=h1+k; if(h1!=h2) { int time = Math.abs(h1-h2)*60; if(m1>m2) { time=time+Math.abs(m1-m2); } else if(m1<m2) { time=time-Math.abs(m1-m2); } System.out.println(time); } else if(h1==h2&&m1==m2) { System.out.println("0"); } } } } }