#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int a,b,c,n;
cin >> a >>b >>c;
c+=b*60+a*1440;
cin >>n;
int q[n],w[n],t[n],f[n];
for(int x=0;
x<n;x++)
{ cin >> q[x] >> w[x] >> t[x];
if(q[x]==0 && t[x]+w[x]*60+a*1440>c)
q[x]=a;
else
if(q[x]==0)
q[x] = a+1;
f[x]=t[x]+w[x]*60+q[x]*1440;
if(f[x]>=c) f[x]-=c;
else f[x]=f[x]+7*1440-c; }
int g=f[0],h=0;
for(int x=0;x<n;x++)
{ if(f[x]<g){ g=f[x]; h=x; } }
cout << q[h] << “ “ << w[h] << “ “ << t[h] <<endl;
system(„PAUSE“);
return EXIT_SUCCESS;
}