#include <iostream>
#include <string>
using namespace std;
// function to convert time in format HH:MM:SS to seconds
int timeToSeconds(string time) {
int hours = stoi(time.substr(0, 2));
int minutes = stoi(time.substr(3, 2));
int seconds = stoi(time.substr(6, 2));
return hours * 3600 + minutes * 60 + seconds;
}
int main() {
// read Iftar times for each day of the week
int iftarTimes[7];
for (int i = 0; i < 7; i++) {
string day, time;
cin >> day >> time;
iftarTimes[i] = timeToSeconds(time);
}
// read number of questions
int q;
cin >> q;
// answer each question
while (q--) {
string day, time;
cin >> day >> time;
int currentSeconds = timeToSeconds(time);
// find index of current day in the week
int currentDayIndex;
if (day == "Saturday") currentDayIndex = 0;
else if (day == "Sunday") currentDayIndex = 1;
else if (day == "Monday") currentDayIndex = 2;
else if (day == "Tuesday") currentDayIndex = 3;
else if (day == "Wednesday") currentDayIndex = 4;
else if (day == "Thursday") currentDayIndex = 5;
else /*if (day == "Friday")*/ currentDayIndex = 6;
// calculate seconds until next Iftar
int secondsUntilIftar;
if (currentSeconds >= iftarTimes[currentDayIndex]) {
// next Iftar is tomorrow
secondsUntilIftar = 24 * 3600 - currentSeconds + iftarTimes[(currentDayIndex + 1) % 7];
} else {
// next Iftar is today
secondsUntilIftar = iftarTimes[currentDayIndex] - currentSeconds;
}
// output answer
cout << secondsUntilIftar << endl;
}
return 0;
}