#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

// 定义一个结构体来存储闹钟时间
struct Alarm {
    int hour;
    int minute;
    int totalMinutes;  // 转换为分钟数

    // 构造函数,方便初始化
    Alarm(int h, int m) : hour(h), minute(m) {
        totalMinutes = h * 60 + m;
    }
};

int main() {
    int n;
    cin >> n;

    vector<Alarm> alarms;
    for (int i = 0; i < n; i++) {
        int h, m;
        cin >> h >> m;
        alarms.emplace_back(h, m);
    }

    int X;
    cin >> X;

    int A, B;
    cin >> A >> B;
    int classTime = A * 60 + B;

    // 筛选出能让牛牛及时到达教室的闹钟
    vector<Alarm> validAlarms;
    for (const auto& alarm : alarms) {
        if (alarm.totalMinutes + X <= classTime) {
            validAlarms.push_back(alarm);
        }
    }

    // 找出最晚的起床时间
    Alarm latestAlarm = validAlarms[0];
    for (const auto& alarm : validAlarms) {
        if (alarm.totalMinutes > latestAlarm.totalMinutes) {
            latestAlarm = alarm;
        }
    }

    cout << latestAlarm.hour << " " << latestAlarm.minute << endl;

    return 0;
}