# Cron

Sometime the classic solution are not powerful enough and we have to design our own. For the purpose of this problem you have to implement the part of the system of task scheduling.

Each task should be executed at some particular moments of time. In our system you may set the exact value for the second, minute, hour, day of the week, day and month, when the task should be executed. Moreover, one can set a special value -1 that means any value of this parameter is valid.

For example, if the parameter string is -1 59 23 -1 -1 -1, the problem will be executed every day at 23:59:00, 23:59:01, 23:59:02, …, 23:59:59 (60 times in total).

Seconds, minutes and hours are numbered starting from zero, while day, months and days of the week are numbered starting from one. The first day of the week is Monday.

There is one special case that is treated separately. If both day of the week and day are given (i.e. differ from -1) to execute the task only one of these two (at least one, if both match this is fine too) parameters should match the current time (of course, all other parameters should match too). For example, the string of parameters 0 0 12 6 3 7 means that the task will be executed both on Saturday, July 2nd, 2016 and on Sunday, July 3rd, 2016 at noon.

One should not forget about the existence of the leap years. The year is leap if it’s number is divisible by 400, or is not divisible by 100, but is divisible by 4. Each leap year has 366 days instead of usual 365, by extending February to 29 days rather than the common 28.

The current time is represented as the number of seconds passed after 00:00:00 January 1st, 1970 (Thursday).

You are given the string of six parameters, describing the moments of time the task should be executed. You are also given a number of moments of time. For each of them you have to find the first moment of time strictly greater than the current when the task will be executed.

Input

The first line of the input contains six integers smhdaydate and month (0 ≤ s, m ≤ 59, 0 ≤ h ≤ 23, 1 ≤ day ≤ 7, 1 ≤ date ≤ 31, 1 ≤ month ≤ 12). Each of the number can also be equal to  - 1. It’s guaranteed, that there are infinitely many moments of time when this task should be executed.

Next line contains the only integer n (1 ≤ n ≤ 1000) — the number of moments of time you have to solve the problem for. Each of the next n lines contains a single integer t i (0 ≤ t i ≤ 1012).

Output

Print n lines, the i-th of them should contain the first moment of time strictly greater than t i, when the task should be executed.

Examples

input

-1 59 23 -1 -1 -16146737265814674175401467417541146741759814674175991467417600

output

146741754014674175411467417542146741759914675039401467503940

input

0 0 12 6 3 73146737265814674608101467547200

output

146746080014675472001468065600

Note

The moment of time 1467372658 after the midnight of January 1st, 1970 is 11:30:58 July 1st, 2016.

Solution:

#include <bits/stdc++.h>

using namespace std;

const int kd[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

const int MAX = 24 * 60 * 60;
const int N = 1234567;

bool leap[N];
int nxt[N];

long long get_back(int day, int month, int year, int moment) {
long long ans = 0;
int yy = 1970;
int mm = 1;
while (yy < year) {
int days = (leap[yy] ? 366 : 365);
ans += days;
yy++;
}
while (mm < month) {
int rd = kd[mm] + ((mm == 2 && leap[yy]) ? 1 : 0);
ans += rd;
mm++;
}
ans += day - 1;
ans = ans * MAX + moment;
return ans;
}

int main() {
for (int i = 0; i < N; i++) {
leap[i] = false;
if (i % 400 == 0) {
leap[i] = true;
}
if (i % 100 != 0 && i % 4 == 0) {
leap[i] = true;
}
}
int s, m, h, day, date, month;
cin >> s >> m >> h >> day >> date >> month;
nxt[MAX] = MAX;
for (int i = MAX - 1; i >= 0; i--) {
int hh = i / 60 / 60;
int mm = i / 60 % 60;
int ss = i % 60;
nxt[i] = nxt[i + 1];
if (s != -1 && s != ss) {
continue;
}
if (m != -1 && m != mm) {
continue;
}
if (h != -1 && h != hh) {
continue;
}
nxt[i] = i;
}
int tt;
cin >> tt;
while (tt--) {
long long ts;
cin >> ts;
ts++;
int moment = ts % MAX;
ts /= MAX;
int yy = 1970;
int dd = 1, mm = 1;
int weekday = 4;
while (true) {
int days = (leap[yy] ? 366 : 365);
if (ts >= days) {
ts -= days;
weekday += (leap[yy] ? 2 : 1);
if (weekday > 7) {
weekday -= 7;
}
yy++;
continue;
}
while (true) {
int rd = kd[mm] + ((mm == 2 && leap[yy]) ? 1 : 0);
if (ts >= rd) {
ts -= rd;
weekday += rd % 7;
if (weekday > 7) {
weekday -= 7;
}
mm++;
continue;
}
weekday += ts % 7;
if (weekday > 7) {
weekday -= 7;
}
dd = 1 + ts;
break;
}
break;
}
while (true) {
bool ok = true;
if (month != -1 && month != mm) {
ok = false;
} else {
if (day != -1 && date != -1) {
if (day != weekday && date != dd) {
ok = false;
}
} else {
if (day != -1 && day != weekday) {
ok = false;
}
if (date != -1 && date != dd) {
ok = false;
}
}
}
if (ok) {
if (nxt[moment] < MAX) {
moment = nxt[moment];
break;
}
}
weekday++;
if (weekday > 7) {
weekday -= 7;
}
int rd = kd[mm] + ((mm == 2 && leap[yy]) ? 1 : 0);
if (dd == rd) {
dd = 1;
mm++;
if (mm > 12) {
mm = 1;
yy++;
}
} else {
dd++;
}
moment = 0;
}
long long res = get_back(dd, mm, yy, moment);
cout << res << endl;
}
return 0;
}