#include
using namespace std;
char vMon[][4]={"","jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"};
char vWek[][4]={"sun","mon","tue","wed","thu","fri","sat"};
int mtharray[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
map<string,int> mMon,mWek;
map<string,vector<string> > mrt;
void buildMonAndWekMap()
{
for(int i=1;i<=12;++i) mMon[vMon[i]]=i;
for(int i=0;i<=6;++i) mWek[vWek[i]]=i;
}
string to_string_x(int n)
{
stringstream ss;
ss<return ss.str();
}
int stoi_x(const string &str)
{
int ans;
stringstream ss(str);
ss>>ans;
return ans;
}
void toStandard(string &str)
{
int len=str.size();
for(int i=0;itolower(str[i]);
}
vector<string> splitStringAndbuildVector(string &str,int TAG)
{
str+=",";
vector<string> vret;
size_t found=str.find(",");
while(found!=string::npos)
{
string x=str.substr(0,found);
str=str.substr(found+1,str.size()-found-1);
size_t fdx=x.find("-");
if(fdx==string::npos)
{
if(TAG==1&&isalpha(x[0])) x=to_string_x(mMon[x]);
if(TAG==2&&isalpha(x[0])) x=to_string_x(mWek[x]);
if(x.size()==1) x="0"+x;
vret.push_back(x);
}
else
{
string L=x.substr(0,fdx),R=x.substr(fdx+1,x.size()-fdx-1);
int left,right;
if(TAG==0) left=stoi_x(L),right=stoi_x(R);
else if(TAG==1)
{
left=(isalpha(L[0]))?mMon[L]:stoi_x(L);
right=(isalpha(R[0]))?mMon[R]:stoi_x(R);
}
else if(TAG==2)
{
left=(isalpha(L[0]))?mWek[L]:stoi_x(L);
right=(isalpha(R[0]))?mWek[R]:stoi_x(R);
}
while(left<=right)
{
string num=to_string_x(left);
if(num.size()==1)num="0"+num;
vret.push_back(num);
++left;
}
}
found=str.find(",");
}
return vret;
}
bool isleapyear(int y)
{
return (y%4==0&&y%100)||y%400==0;
}
string getWeekday(string year,string month,string day)
{
int y=stoi_x(year),m=stoi_x(month),d=stoi_x(day);
int by=1970,countday=0;
while(by366:365;
++by;
}
for(int i=1;i1;
return "0"+to_string_x((4+countday%7)%7);
}
int main()
{
int n;
string st,et;
buildMonAndWekMap();
cin>>n>>st>>et;
string syy=st.substr(0,4),smm=st.substr(4,2),sdd=st.substr(6,2),sHH=st.substr(8,2),sMM=st.substr(10,2);
string eyy=et.substr(0,4),emm=et.substr(4,2),edd=et.substr(6,2),eHH=et.substr(8,2),eMM=et.substr(10,2);
int syInt=stoi_x(syy),eyInt=stoi_x(eyy);
while(n--)
{
vector<string> vmts,vhur,vdfm,vmth,vdfw;
string minutes,hours,dofmon,month,dofwek,command;
cin>>minutes>>hours>>dofmon>>month>>dofwek>>command;
toStandard(month);
toStandard(dofwek);
if(minutes=="*") minutes="0-59";
vmts=splitStringAndbuildVector(minutes,0);
if(hours=="*") hours="0-23";
vhur=splitStringAndbuildVector(hours,0);
if(dofmon=="*") dofmon="1-31";
vdfm=splitStringAndbuildVector(dofmon,0);
if(month=="*") month="1-12";
vmth=splitStringAndbuildVector(month,1);
if(dofwek=="*") dofwek="0-6";
vdfw=splitStringAndbuildVector(dofwek,2);
set<string> wekexist;
for(size_t i=0;i