public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		try {
			String mLine = br.readLine();
			int m = Integer.valueOf(mLine);
			
			String idIn = null;
			String idOut = null;
			int[] inTime = null;
			int[] outTime = null;
			
			for(int i = 0; i < m; i++) {
				String line = br.readLine();
				String[] splitted = line.split("\\s+");
				String id = splitted[0];

				String signinTimeS = splitted[1];
				int[] signinTime = new int[3];
				initTime(signinTimeS.split(":"), signinTime);
				if(inTime == null || earlier(signinTime, inTime)) {
					inTime = signinTime;
					idIn = id;
				}
				
				String signoutTimeS = splitted[2];
				int[] signoutTime = new int[3];
				initTime(signoutTimeS.split(":"), signoutTime);
				if(outTime == null || later(signoutTime, outTime)) {
					outTime = signoutTime;
					idOut = id;
				}
			}
				
			System.out.println(idIn + " " + idOut);
		} finally {
			br.close();
		}
	}