#include <bits/stdc++.h>
#define SZ(x) ((int) (x).size())
using namespace std;

typedef long long i64;

const int daysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int64_t offset;
int64_t period;

inline bool isLeap(int i) {
    return i % 400 == 0 || (i % 100 != 0 && i % 4 == 0);
}

int mdays(int y, int i) {
    return i == 1 && isLeap(y) ? 29: daysInMonth[i];
}

int getYear(int64_t t) {
    int64_t cp = t % period;
    int year = 0;
    for (int i = 0; i < 400; ++i) {
        int64_t c = 365LL * 24 * 3600;
        if (isLeap(i)) {
            c += 24 * 3600;
        }
        if (cp < c) {
            break;
        } else {
            cp -= c;
            year++;
        }
    }
    return year;
}

int getMonth(int64_t t) {
    int64_t cp = t % period;
    int year = 0;
    for (int i = 0; i < 400; ++i) {
        int64_t c = 365LL * 24 * 3600;
        if (isLeap(i)) {
            c += 24 * 3600;
        }
        if (cp < c) {
            break;
        } else {
            cp -= c;
            year++;
        }
    }

    int month = 0;
    for (int i = 0; i < 12; ++i) {
        int64_t c = 24LL * 3600 * mdays(year, i);
        if (cp < c) {
            break;
        } else {
            cp -= c;
            month++;
        }
    }
    return month;
}

int getDate(int64_t t) {
    int64_t cp = t % period;
    int year = 0;
    for (int i = 0; i < 400; ++i) {
        int64_t c = 365LL * 24 * 3600;
        if (isLeap(i)) {
            c += 24 * 3600;
        }
        if (cp < c) {
            break;
        } else {
            cp -= c;
            year++;
        }
    }

    int month = 0;
    for (int i = 0; i < 12; ++i) {
        int64_t c = 24LL * 3600 * mdays(year, i);
        if (cp < c) {
            break;
        } else {
            cp -= c;
            month++;
        }
    }
    return cp / (24 * 3600);
}

bool check(int s, int m, int h, int day, int date, int month, int64_t t) {
    if (s >= 0 && t % 60 != s) {
        return false;
    }
    if (m >= 0 && t / 60 % 60 != m) {
        return false;
    }
    if (h >= 0 && t / 3600 % 24 != h) {
        return false;
    }

    if (day != -1 && date != -1) {
        if ((day != -1 && t / (3600 * 24) % 7 != day) &&
            (date != -1 && getDate(t) != date)) {
            return false;
        }
    } else if (day != -1 && t / (3600 * 24) % 7 != day) {
       return false;
    } else if (date != -1 && getDate(t) != date) {
        return false;
    }
    if (month != -1 && getMonth(t) != month) {
        return false;
    }
    return true;
}

int64_t matchDay(int s, int m, int h, int day, int month, int64_t t) {
    while (!check(s, m, h, day, -1, month, t)) {
        if (month != -1 && getMonth(t) != month) {
            t -= t % (24 * 3600);
            t -= getDate(t) * 24 * 3600LL;
            int x = getMonth(t);
            int y = getYear(t);
            while (x != month) {
                t += mdays(y, x) * 24LL * 3600;
                x++;
                if (x == 12) {
                    x = 0;
                    y++;
                }
            }
            continue;
        }
        if (day != -1 && t / (3600 * 24) % 7 != day) {
            int week = 24 * 3600 * 7;
            int add = (day * 3600 * 24 - t % week + week) % week;
            t += add;
            continue;
        }
        if (h != -1 && t / 3600 % 24 != h) {
            int day = 24 * 3600;
            int add = (h * 3600 - t % day + day) % day;
            t += add;
            continue;
        }
        if (m != -1 && t / 60 % 60 != m) {
            int add = (m * 60 - t % 3600 + 3600) % 3600;
            t += add;
            continue;
        }
        if (s != -1 && t % 60 != s) {
            int add = (s - t % 60 + 60) % 60;
            t += add;
            continue;
        }
    }
    return t;
}

int64_t matchDate(int s, int m, int h, int date, int month, int64_t t) {
    while (!check(s, m, h, -1, date, month, t)) {
        if (month != -1 && getMonth(t) != month) {
            t -= t % (24 * 3600);
            t -= getDate(t) * 24 * 3600LL;
            int x = getMonth(t);
            int y = getYear(t);
            while (x != month) {
                t += mdays(y, x) * 24LL * 3600;
                x++;
                if (x == 12) {
                    x = 0;
                    y++;
                }
            }
            continue;
        }
        if (date != -1 && getDate(t) != date) {
            int mon = getMonth(t);
            int y = getYear(t);
            int d = getDate(t);
            t -= t % (24 * 3600);
            while (d != date) {
                t += 24 * 3600;
                d++;
                if (d == mdays(y, mon)) {
                    d = 0;
                    mon++;
                }
                if (mon == 12) {
                    mon = 0;
                    y++;
                }
            }
            continue;
        }
        if (h != -1 && t / 3600 % 24 != h) {
            int day = 24 * 3600;
            int add = (h * 3600 - t % day + day) % day;
            t += add;
            continue;
        }
        if (m != -1 && t / 60 % 60 != m) {
            int add = (m * 60 - t % 3600 + 3600) % 3600;
            t += add;
            continue;
        }
        if (s != -1 && t % 60 != s) {
            int add = (s - t % 60 + 60) % 60;
            t += add;
            continue;
        }
    }
    return t;
}

int main() {
    #ifdef LOCAL_RUN
    freopen("task.in", "r", stdin);
    freopen("task.out", "w", stdout);
    //freopen("task.err", "w", stderr);
    #endif // ONLINE_JUDGE
    ios::sync_with_stdio(false);
    cin.tie(0);

    int s, m, h, day, date, month;
    cin >> s >> m >> h >> day >> date >> month;

    int T;
    cin >> T;

    offset = 1970LL * 365 * 24 * 3600;
    for (int i = 0; i < 1970; ++i) {
        if (isLeap(i)) {
            offset += 24 * 3600;
        }
    }

    period = 400LL * 365 * 24 * 3600;
    for (int i = 0; i < 400; ++i) {
        if (isLeap(i)) {
            period += 24 * 3600;
        }
    }

    if (month != -1) {
        month--;
    }
    if (day != -1) {
        day--;
        day = (day + 2) % 7;
    }
    if (date != -1) {
        date--;
    }

    while (T-- > 0) {
        int64_t t;
        cin >> t;

        t += offset + 1;

        int64_t t1 = 1LL << 62;
        if (day != -1) {
            t1 = matchDay(s, m, h, day, month, t);
        }
        int64_t t2 = 1LL << 62;
        if (date != -1) {
            t2 = matchDate(s, m, h, date, month, t);
        }
        if (day == -1 && date == -1) {
            t1 = matchDay(s, m, h, day, month, t);
        }

        cout << min(t1, t2) - offset << '\n';
    }
}


#include <iostream>

using namespace std;

struct my_time {
	int sec;
	int min;
	int hour;
	int wod;
	int day;
	int mon;
	int year;
};

bool isLeapYear(int x) {
	if (x % 4) return false;
	if (x % 100) return true;
	return !(x % 400);
}

int getDays(int y) {
	return y * 365 + y / 4 - y / 100 + y / 400;
}

void getTime(my_time* cal, long long t) {
	cal->sec = t % 60;
	t /= 60;

	cal->min = t % 60;
	t /= 60;

	cal->hour = t % 24;
	t /= 24;

	cal->wod = (t + 3) % 7 + 1;

	// find year with binary search
	int l = 1970, r = 31710;
	while(l <= r) {
		const int m = (l + r) / 2;
		const int w = getDays(m-1) - getDays(1969);

		if (w > t) {
			r = m - 1;
		} else if (w < t) {
			cal->year = m;
			l = m + 1;
		} else {
			cal->year = m;
			break;
		}
	}

	t -= getDays(cal->year - 1) - getDays(1969);
	int m[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	if (isLeapYear(cal->year)) m[2] = 29;

	for(int i = 1; i <= 12; ++i) {
		if (t < m[i]) {
			cal->mon = i;
			cal->day = t + 1;
			return;
		}
		t -= m[i];
	}
}

int main() {
	my_time cron;
	cin >> cron.sec\
	    >> cron.min\
	    >> cron.hour\
	    >> cron.wod\
	    >> cron.day\
	    >> cron.mon;

	int tc;
	scanf("%d", &tc);
	while(tc--) {
		long long t;
		cin >> t;

		t += 1;

		my_time cal;
		while(true) {
			getTime(&cal, t);
			/*cerr << cal.year << "/" << cal.mon << "/" << cal.day\
				<< " (" << cal.wod << ") " << cal.hour << ":"\
				<< cal.min << ":" << cal.sec << " * " << t << endl;*/

			if (cron.sec != -1 && cron.sec != cal.sec) {
				if (cron.sec > cal.sec) t += cron.sec - cal.sec;
				else t += 60 + (cron.sec - cal.sec);
				continue;
			}

			if (cron.min != -1 && cron.min != cal.min) {
				if (cron.min > cal.min) {
					t += (cron.min - cal.min) * 60;
				} else {
					t += (60 + cron.min - cal.min) * 60;
				}
				t -= cal.sec;
				continue;
			}

			if (cron.hour != -1 && cron.hour != cal.hour) {
				if (cron.hour > cal.hour) {
					t += (cron.hour - cal.hour) * 3600;
				} else {
					t += (24 + cron.hour - cal.hour) * 3600;
				}
				t -= cal.sec + cal.min * 60;
				continue;
			}

			if (cron.mon != -1 && cron.mon != cal.mon) {
				t += 86400;
				continue;
			}

			if (cron.wod != -1 && cron.day != -1) {
				if (cron.wod != cal.wod && cron.day != cal.day) {
					t += 86400;
					continue;
				} else {
					break;
				}
			}

			if (cron.wod != -1) {
				if (cron.wod != cal.wod) {
					t += 86400;
					continue;
				} else {
					break;
				}
			} else if (cron.day != -1) {
				if (cron.day != cal.day) {
					t += 86400;
					continue;
				} else {
					break;
				}
			}

			break;
		}

		cout << t << endl;
	}

	return 0;
}


#include <iostream>

using namespace std;

struct my_time {
	int sec;
	int min;
	int hour;
	int wod;
	int day;
	int mon;
	int year;
};

bool isLeapYear(int x) {
	if (x % 4) return false;
	if (x % 100) return true;
	return !(x % 400);
}

int getDays(int y) {
	return y * 365 + y / 4 - y / 100 + y / 400;
}

void getTime(my_time* cal, long long t) {
	cal->sec = t % 60;
	t /= 60;

	cal->min = t % 60;
	t /= 60;

	cal->hour = t % 24;
	t /= 24;

	cal->wod = (t + 3) % 7 + 1;

	// find year with binary search
	int l = 1970, r = 40000;
	while(l <= r) {
		const int m = (l + r) / 2;
		const int w = getDays(m-1) - getDays(1969);

		if (w > t) {
			r = m - 1;
		} else if (w < t) {
			cal->year = m;
			l = m + 1;
		} else {
			cal->year = m;
			break;
		}
	}

	t -= getDays(cal->year - 1) - getDays(1969);
	int m[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	if (isLeapYear(cal->year)) m[2] = 29;

	for(int i = 1; i <= 12; ++i) {
		if (t < m[i]) {
			cal->mon = i;
			cal->day = t + 1;
			return;
		}
		t -= m[i];
	}
}

int main() {
	my_time cron;
	cin >> cron.sec\
	    >> cron.min\
	    >> cron.hour\
	    >> cron.wod\
	    >> cron.day\
	    >> cron.mon;

	int tc;
	scanf("%d", &tc);
	while(tc--) {
		long long t;
		cin >> t;

		t += 1;

		my_time cal;
		while(true) {
			getTime(&cal, t);
			/*cerr << cal.year << "/" << cal.mon << "/" << cal.day\
				<< " (" << cal.wod << ") " << cal.hour << ":"\
				<< cal.min << ":" << cal.sec << " * " << t << endl;*/

			if (cron.sec != -1 && cron.sec != cal.sec) {
				if (cron.sec > cal.sec) t += cron.sec - cal.sec;
				else t += 60 + cron.sec - cal.sec;
				continue;
			}

			if (cron.min != -1 && cron.min != cal.min) {
				if (cron.min > cal.min) {
					t += (cron.min - cal.min) * 60;
				} else {
					t += (60 + cron.min - cal.min) * 60;
				}
				t -= cal.sec;
				continue;
			}

			if (cron.hour != -1 && cron.hour != cal.hour) {
				if (cron.hour > cal.hour) {
					t += (cron.hour - cal.hour) * 3600;
				} else {
					t += (24 + cron.hour - cal.hour) * 3600;
				}
				t -= cal.sec + cal.min * 60;
				continue;
			}

			if (cron.mon != -1 && cron.mon != cal.mon) {
				t += 86400;
				t -= cal.sec + cal.min * 60 + cal.hour * 3600;
				continue;
			}

			if (cron.wod != -1 && cron.day != -1) {
				if (cron.wod != cal.wod && cron.day != cal.day) {
					t += 86400;
					t -= cal.sec + cal.min * 60 + cal.hour * 3600;
					continue;
				} else {
					break;
				}
			}

			if (cron.wod != -1) {
				if (cron.wod != cal.wod) {
					t += 86400;
					t -= cal.sec + cal.min * 60 + cal.hour * 3600;
					continue;
				} else {
					break;
				}
			} else if (cron.day != -1) {
				if (cron.day != cal.day) {
					t += 86400;
					t -= cal.sec + cal.min * 60 + cal.hour * 3600;
					continue;
				} else {
					break;
				}
			}

			break;
		}

		cout << t << endl;
		/*cerr << "---" << endl;*/
	}

	return 0;
}


#include <iostream>

using namespace std;

struct my_time {
	int sec;
	int min;
	int hour;
	int wod;
	int day;
	int mon;
	int year;
};

bool isLeapYear(int x) {
	if (x % 4) return false;
	if (x % 100) return true;
	return !(x % 400);
}

int getDays(int y) {
	return y * 365 + y / 4 - y / 100 + y / 400;
}

void getTime(my_time* cal, long long t) {
	cal->sec = t % 60;
	t /= 60;

	cal->min = t % 60;
	t /= 60;

	cal->hour = t % 24;
	t /= 24;

	cal->wod = (t + 3) % 7 + 1;

	// find year with binary search
	int l = 1970, r = 31710;
	while(l <= r) {
		const int m = (l + r) / 2;
		const int w = getDays(m-1) - getDays(1969);

		if (w > t) {
			r = m - 1;
		} else if (w < t) {
			cal->year = m;
			l = m + 1;
		} else {
			cal->year = m;
			break;
		}
	}

	t -= getDays(cal->year - 1) - getDays(1969);
	int m[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	if (isLeapYear(cal->year)) m[2] = 29;

	for(int i = 1; i <= 12; ++i) {
		if (t < m[i]) {
			cal->mon = i;
			cal->day = t + 1;
			return;
		}
		t -= m[i];
	}
}

int main() {
	my_time cron;
	cin >> cron.sec\
	    >> cron.min\
	    >> cron.hour\
	    >> cron.wod\
	    >> cron.day\
	    >> cron.mon;

	int tc;
	scanf("%d", &tc);
	while(tc--) {
		long long t;
		cin >> t;

		t += 1;

		my_time cal;
		while(true) {
			getTime(&cal, t);
			//cerr << cal.year << "/" << cal.mon << "/" << cal.day\
			//	<< " (" << cal.wod << ") " << cal.hour << ":"\
			//	<< cal.min << ":" << cal.sec << " * " << t << endl;

			if (cron.sec != -1 && cron.sec != cal.sec) {
				if (cron.sec > cal.sec) t += cron.sec - cal.sec;
				else t += 60 + cron.sec - cal.sec;
				continue;
			}

			if (cron.min != -1 && cron.min != cal.min) {
				if (cron.min > cal.min) {
					t += (cron.min - cal.min) * 60;
				} else {
					t += (60 + cron.min - cal.min) * 60;
				}
				t -= cal.sec;
				continue;
			}

			if (cron.hour != -1 && cron.hour != cal.hour) {
				if (cron.hour > cal.hour) {
					t += (cron.hour - cal.hour) * 3600;
				} else {
					t += (24 + cron.hour - cal.hour) * 3600;
				}
				t -= cal.sec + cal.min * 60;
				continue;
			}

			if (cron.mon != -1 && cron.mon != cal.mon) {
				t += 86400;
				t -= cal.sec + cal.min * 60 + cal.hour * 3600;
				continue;
			}

			if (cron.wod != -1 && cron.day != -1) {
				if (cron.wod != cal.wod && cron.day != cal.day) {
					t += 86400;
					t -= cal.sec + cal.min * 60 + cal.hour * 3600;
					continue;
				} else {
					break;
				}
			}

			if (cron.wod != -1) {
				if (cron.wod != cal.wod) {
					t += 86400;
					t -= cal.sec + cal.min * 60 + cal.hour * 3600;
					continue;
				} else {
					break;
				}
			} else if (cron.day != -1) {
				if (cron.day != cal.day) {
					t += 86400;
					t -= cal.sec + cal.min * 60 + cal.hour * 3600;
					continue;
				} else {
					break;
				}
			}

			break;
		}

		cout << t << endl;
		//cerr << "--" << endl;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int mdays[][12] = {
	{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
	{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};

int second, minute, hour, day, date, month;
long long times, days;
vector <int> T, D;

bool valid_time(int h, int m, int s) {
	if (second != -1 && s != second) return 0;
	if (minute != -1 && m != minute) return 0;
	if (hour != -1 && h != hour) return 0;
	return 1;
}

bool valid_day(int m, int d, int today) {
	if (month != -1 && m != month) return 0;
	return day == -1 && date == -1 || date != -1 && d == date || day != -1 && today == day;
}

bool leap(int y) {
	return y % 4 == 0 && y % 100 != 0 || y % 400 == 0;
}

void prepare() {
	for (int h = 0; h < 24; h ++) {
		for (int m = 0; m < 60; m ++) {
			for (int s = 0; s < 60; s ++, times ++) if (valid_time(h, m, s)) T.push_back(times);
		}
	}
	for (int y = 1970, today = 3; y < 1970 + 400; y ++) {
		for (int m = 0; m < 12; m ++) {
			for (int d = 0; d < mdays[leap(y)][m]; d ++, days ++) {
				if (valid_day(m, d, today)) D.push_back(days);
				today = (today + 1) % 7;
			}
		}
	}
}

int q;

int main() {
//	freopen("e.in", "r", stdin);
	scanf("%d %d %d %d %d %d", &second, &minute, &hour, &day, &date, &month);
	if (day != -1) day --;
	if (date != -1) date --;
	if (month != -1) month --;
	prepare();
	scanf("%d", &q);
	while (q --) {
		long long x, y, d, r, ans;
		scanf("%I64d", &x);
		y = x / (days * times), r = x % (days * times);
		d = r / times, r %= times;
		if (d > D.back() || d == D.back() && r >= T.back()) {
			ans = (y + 1) * days * times + D[0] * times + T[0];
		}
		else if (r >= T.back() || !binary_search(D.begin(), D.end(), d)) {
			ans = y * days * times + *upper_bound(D.begin(), D.end(), d) * times + T[0];
		}
		else ans = y * days * times + d * times + *upper_bound(T.begin(), T.end(), r);
		printf("%I64d\n", ans);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define N 200200

const int mod = 1e9 + 7, base = 107;

int n, H[N], ex[N];
char s[N];
map < pair <int, int>, vector <int> > mp[N];
map < pair <int, int>, vector <int> > :: iterator it;

int Hash(int l, int r) {
	return ((H[r] - 1ll * H[l-1] * ex[r-l+1]) % mod + mod) % mod;
}

int main() {
//	freopen("e.in", "r", stdin);
	scanf("%d %s", &n, s + 1);
	ex[0] = 1;
	for (int i = 1; i <= n; i ++) {
		H[i] = (1ll * H[i-1] * base + (s[i] - 'a' + 1)) % mod;
		ex[i] = 1ll * ex[i-1] * base % mod;
	}
	for (int i = 1; i <= n; i ++) mp[1][make_pair(Hash(i,i),1)].push_back(i);
	int ans = 0;
	for (int i = 1; i <= n; i ++) if (!mp[i].empty()) {
		ans = i;
		for (it = mp[i].begin(); it != mp[i].end(); it ++) {
			vector <int> v = it -> second;
			int len = it -> first.second, sz = v.size();
			vector <int> T;
			for (int j = 0; j < sz - 1; j ++) T.push_back(Hash(v[j] + len, v[j+1] - 1));
			for (int l = 0, r = 1; l < sz - 1; l = r, r = l + 1) {
				while (r < sz - 1 && T[r-1] == T[r]) r ++;
				int nxti = i + r - l, nxtlen = v[r] + len - v[l];
				mp[nxti][make_pair(Hash(v[l],v[l]+nxtlen-1),nxtlen)].push_back(v[l]);
			}
		}
	}
	printf("%d\n", ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int mdays[][12] = {
	{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
	{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};

int second, minute, hour, day, date, month;
long long times, days;
vector <int> T, D;

bool valid_time(int h, int m, int s) {
	if (second != -1 && s != second) return 0;
	if (minute != -1 && m != minute) return 0;
	if (hour != -1 && h != hour) return 0;
	return 1;
}

bool valid_day(int m, int d, int today) {
	if (month != -1 && m != month) return 0;
	return day == -1 && date == -1 || date != -1 && d == date || day != -1 && today == day;
}

bool leap(int y) {
	return y % 4 == 0 && y % 100 != 0 || y % 400 == 0;
}

void prepare() {
	for (int h = 0; h < 24; h ++) {
		for (int m = 0; m < 60; m ++) {
			for (int s = 0; s < 60; s ++, times ++) if (valid_time(h, m, s)) T.push_back(times);
		}
	}
	for (int y = 1970, today = 3; y < 1970 + 400; y ++) {
		for (int m = 0; m < 12; m ++) {
			for (int d = 0; d < mdays[leap(y)][m]; d ++, days ++) {
				if (valid_day(m, d, today)) D.push_back(days);
				today = (today + 1) % 7;
			}
		}
	}
}

int q;

int main() {
	freopen("e.in", "r", stdin);
	freopen("e.out", "w", stdout);
	scanf("%d %d %d %d %d %d", &second, &minute, &hour, &day, &date, &month);
	if (day != -1) day --;
	if (date != -1) date --;
	if (month != -1) month --;
	prepare();
	scanf("%d", &q);
	while (q --) {
		long long x, y, d, r, ans;
		scanf("%I64d", &x);
		y = x / (days * times), r = x % (days * times);
		d = r / times, r %= times;
		if (d > D.back() || d == D.back() && r >= T.back()) {
			ans = (y + 1) * days * times + D[0] * times + T[0];
		}
		else if (r >= T.back() || !binary_search(D.begin(), D.end(), d)) {
			ans = y * days * times + *upper_bound(D.begin(), D.end(), d) * times + T[0];
		}
		else ans = y * days * times + d * times + *upper_bound(T.begin(), T.end(), r);
		printf("%I64d\n", ans);
	}
	return 0;
}


#include <cstdio>
using namespace std;
int dayofyear(int y) {
    y += 1970;
    return (y % 400 == 0) || (y % 100 != 0 && y % 4 == 0)
            ? 366 : 365;
}
int sdoy[401];
int sumdayofyear(int y) {
//    printf("y%d\n", y);
    return (y / 400) * sdoy[400] + sdoy[y % 400];
}
int getyear(int d) {
    int y = 1;
    while(sumdayofyear(y) <= d)
        y <<= 1;
    int ans = 0;
    for(int j = y; j >= 1; j >>= 1)
        if(sumdayofyear(ans + j) <= d)
            ans += j;
    return ans;
}
int getday(long long t) {
    return (t / 86400 + 3) % 7;
}
constexpr int dom[12] =
    {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int domy(int m, int y) {
    if(m == 2) {
        y += 1970;
        return (y % 400 == 0) || (y % 100 != 0 && y % 4 == 0)
                ? dom[m] + 1 : dom[m];
    } else
        return dom[m];
}
int getdate(long long t) {
    int d = t / 86400;
    int y = getyear(d);
    d -= sumdayofyear(y);
    for(int i = 0; i < 12; i++) {
        if(d < domy(i, y))
            return d;
        d -= domy(i, y);
    }
    __builtin_unreachable();
}
int getmonth(long long t) {
    int d = t / 86400;
    int y = getyear(d);
    d -= sumdayofyear(y);
    for(int i = 0; i < 12; i++) {
        if(d < domy(i, y))
            return i;
        d -= domy(i, y);
    }
    __builtin_unreachable();
}
long long solve(long long t, int s, int m, int h,
        int day, int date, int month) {
    if(s != -1)
        while(t % 60 != s)
            t++;
    if(m != -1)
        while(t / 60 % 60 != m) {
            t += 60;
            if(s == -1)
                t -= t % 60;
        }
    if(h != -1)
        while(t / 3600 % 24 != h) {
            t += 3600;
            if(m == -1)
                t -= t / 60 % 60 * 60;
            if(s == -1)
                t -= t % 60;
        }
    while((month != -2 && getmonth(t) != month) ||
            (day != -2 && date != -2 && getday(t) != day && getdate(t) != date) ||
            (day != -2 && date == -2 && getday(t) != day) ||
            (day == -2 && date != -2 && getdate(t) != date)) {
        if(month != -2 && getmonth(t) != month) {
            t += (domy(t, getyear(t / 86400)) - getday(t)) * 86400;
        } else {
            t += 86400;
        }
        if(h == -1)
            t -= t / 3600 % 24 * 3600;
        if(m == -1)
            t -= t / 60 % 60 * 60;
        if(s == -1)
            t -= t % 60;
    }
    return t;
}
int main() {
    for(int i = 0; i < 400; i++)
        sdoy[i + 1] = sdoy[i] + dayofyear(i);

    int s, m, h, day, date, month;
    scanf("%d%d%d%d%d%d", &s, &m, &h, &day, &date, &month);
    int n;
    scanf("%d", &n);
    while(n--) {
        long long t;
        scanf("%lld", &t);
        printf("%lld\n", solve(t + 1, s, m, h, day - 1,
                    date - 1, month - 1));
    }
}


#include <cstdio>
using namespace std;
constexpr int dayofyear(int y) {
    y += 1970;
    return (y % 400 == 0) || (y % 100 != 0 && y % 4 == 0)
            ? 366 : 365;
}
int sdoy[401];
int sumdayofyear(int y) {
    return (y / 400) * sdoy[400] + sdoy[y % 400];
}
int getyear(int d) {
    int y = 1;
    while(sumdayofyear(y) <= d)
        y <<= 1;
    int ans = 0;
    for(int j = y; j >= 1; j >>= 1)
        if(sumdayofyear(ans + j) <= d)
            ans += j;
    return ans;
}
constexpr int getday(long long t) {
    return (t / 86400 + 3) % 7;
}
constexpr int dom[12] =
    {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
constexpr int domy(int m, int y) {
    if(m == 1) {
        y += 1970;
        return (y % 400 == 0) || (y % 100 != 0 && y % 4 == 0)
                ? dom[m] + 1 : dom[m];
    } else
        return dom[m];
}
int getdate(long long t) {
    int d = t / 86400;
    int y = getyear(d);
    d -= sumdayofyear(y);
    for(int i = 0; i < 12; i++) {
        if(d < domy(i, y))
            return d;
        d -= domy(i, y);
    }
    __builtin_unreachable();
}
int getmonth(long long t) {
    int d = t / 86400;
    int y = getyear(d);
    d -= sumdayofyear(y);
    for(int i = 0; i < 12; i++) {
        if(d < domy(i, y))
            return i;
        d -= domy(i, y);
    }
    __builtin_unreachable();
}
long long solve(long long t, int s, int m, int h,
        int day, int date, int month) {
    if(s != -1)
        while(t % 60 != s)
            t++;
    if(m != -1)
        while(t / 60 % 60 != m) {
            t += 60;
            if(s == -1)
                t -= t % 60;
        }
    if(h != -1)
        while(t / 3600 % 24 != h) {
            t += 3600;
            if(m == -1)
                t -= t / 60 % 60 * 60;
            if(s == -1)
                t -= t % 60;
        }
    while((month != -2 && getmonth(t) != month) ||
            (day != -2 && date != -2 && getday(t) != day && getdate(t) != date) ||
            (day != -2 && date == -2 && getday(t) != day) ||
            (day == -2 && date != -2 && getdate(t) != date)) {
        if(month != -2 && getmonth(t) != month) {
            t += (domy(getmonth(t), getyear(t / 86400)) - getdate(t)) * 86400;
        } else {
            t += 86400;
        }
        if(h == -1)
            t -= t / 3600 % 24 * 3600;
        if(m == -1)
            t -= t / 60 % 60 * 60;
        if(s == -1)
            t -= t % 60;
    }
    return t;
}
int main() {
    for(int i = 0; i < 400; i++)
        sdoy[i + 1] = sdoy[i] + dayofyear(i);

    int s, m, h, day, date, month;
    scanf("%d%d%d%d%d%d", &s, &m, &h, &day, &date, &month);
    int n;
    scanf("%d", &n);
    while(n--) {
        long long t;
        scanf("%lld", &t);
        printf("%lld\n", solve(t + 1, s, m, h, day - 1,
                    date - 1, month - 1));
    }
}


#include <cstdio>
using namespace std;
int dayofyear(int y) {
    y += 1970;
    return (y % 400 == 0) || (y % 100 != 0 && y % 4 == 0)
            ? 366 : 365;
}
int sdoy[401];
int sumdayofyear(int y) {
//    printf("y%d\n", y);
    return (y / 400) * sdoy[400] + sdoy[y % 400];
}
int getyear(int d) {
    int y = 1;
    while(sumdayofyear(y) <= d)
        y <<= 1;
    int ans = 0;
    for(int j = y; j >= 1; j >>= 1)
        if(sumdayofyear(ans + j) <= d)
            ans += j;
    return ans;
}
int getday(long long t) {
    return (t / 86400 + 3) % 7;
}
constexpr int dom[12] =
    {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int domy(int m, int y) {
    if(m == 2) {
        y += 1970;
        return (y % 400 == 0) || (y % 100 != 0 && y % 4 == 0)
                ? dom[m] + 1 : dom[m];
    } else
        return dom[m];
}
int getdate(long long t) {
    int d = t / 86400;
    int y = getyear(d);
    d -= sumdayofyear(y);
    for(int i = 0; i < 12; i++) {
        if(d < domy(i, y))
            return d;
        d -= domy(i, y);
    }
    __builtin_unreachable();
}
int getmonth(long long t) {
    int d = t / 86400;
    int y = getyear(d);
    d -= sumdayofyear(y);
    for(int i = 0; i < 12; i++) {
        if(d < domy(i, y))
            return i;
        d -= domy(i, y);
    }
    __builtin_unreachable();
}
long long solve(long long t, int s, int m, int h,
        int day, int date, int month) {
    if(s != -1)
        while(t % 60 != s)
            t++;
    if(m != -1)
        while(t / 60 % 60 != m) {
            t += 60;
            if(s == -1)
                t -= t % 60;
        }
    if(h != -1)
        while(t / 3600 % 24 != h) {
            t += 3600;
            if(m == -1)
                t -= t / 60 % 60 * 60;
            if(s == -1)
                t -= t % 60;
        }
    while((month != -2 && getmonth(t) != month) ||
            (day != -2 && date != -2 && getday(t) != day && getdate(t) != date) ||
            (day != -2 && date == -2 && getday(t) != day) ||
            (day == -2 && date != -2 && getdate(t) != date)) {
        t += 86400;
        if(h == -1)
            t -= t / 3600 % 24 * 3600;
        if(m == -1)
            t -= t / 60 % 60 * 60;
        if(s == -1)
            t -= t % 60;
    }
    return t;
}
int main() {
    for(int i = 0; i < 400; i++)
        sdoy[i + 1] = sdoy[i] + dayofyear(i);

//    printf("%d\n", getyear(1467372658 / 86400));
//    printf("%d\n", getmonth(1467372658));
//    printf("%d\n", getdate(1467372658));

    int s, m, h, day, date, month;
    scanf("%d%d%d%d%d%d", &s, &m, &h, &day, &date, &month);
    int n;
    scanf("%d", &n);
    while(n--) {
        long long t;
        scanf("%lld", &t);
        printf("%lld\n", solve(t + 1, s, m, h, day - 1,
                    date - 1, month - 1));
    }
}


#include <cstdio>
using namespace std;
 int dayofyear(int y) {
    y += 1970;
    return (y % 400 == 0) || (y % 100 != 0 && y % 4 == 0)
            ? 366 : 365;
}
int sdoy[401];
int sumdayofyear(int y) {
    return (y / 400) * sdoy[400] + sdoy[y % 400];
}
int getyear(int d) {
    int y = 1;
    while(sumdayofyear(y) <= d)
        y <<= 1;
    int ans = 0;
    for(int j = y; j >= 1; j >>= 1)
        if(sumdayofyear(ans + j) <= d)
            ans += j;
    return ans;
}
 int getday(long long t) {
    return (t / 86400 + 3) % 7;
}
 int dom[12] =
    {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
 int domy(int m, int y) {
    if(m == 1) {
        y += 1970;
        return (y % 400 == 0) || (y % 100 != 0 && y % 4 == 0)
                ? dom[m] + 1 : dom[m];
    } else
        return dom[m];
}
int getdate(long long t) {
    int d = t / 86400;
    int y = getyear(d);
    d -= sumdayofyear(y);
    for(int i = 0; i < 12; i++) {
        if(d < domy(i, y))
            return d;
        d -= domy(i, y);
    }
    __builtin_unreachable();
}
int getmonth(long long t) {
    int d = t / 86400;
    int y = getyear(d);
    d -= sumdayofyear(y);
    for(int i = 0; i < 12; i++) {
        if(d < domy(i, y))
            return i;
        d -= domy(i, y);
    }
    __builtin_unreachable();
}
long long solve(long long t, int s, int m, int h,
        int day, int date, int month) {
    if(s != -1)
        while(t % 60 != s)
            t++;
    if(m != -1)
        while(t / 60 % 60 != m) {
            t += 60;
            if(s == -1)
                t -= t % 60;
        }
    if(h != -1)
        while(t / 3600 % 24 != h) {
            t += 3600;
            if(m == -1)
                t -= t / 60 % 60 * 60;
            if(s == -1)
                t -= t % 60;
        }
    while((month != -2 && getmonth(t) != month) ||
            (day != -2 && date != -2 && getday(t) != day && getdate(t) != date) ||
            (day != -2 && date == -2 && getday(t) != day) ||
            (day == -2 && date != -2 && getdate(t) != date)) {
        if(month != -2 && getmonth(t) != month) {
            t += (domy(getmonth(t), getyear(t / 86400)) - getdate(t)) * 86400;
        } else {
            t += 86400;
        }
        if(h == -1)
            t -= t / 3600 % 24 * 3600;
        if(m == -1)
            t -= t / 60 % 60 * 60;
        if(s == -1)
            t -= t % 60;
    }
    return t;
}
int main() {
    for(int i = 0; i < 400; i++)
        sdoy[i + 1] = sdoy[i] + dayofyear(i);

    int s, m, h, day, date, month;
    scanf("%d%d%d%d%d%d", &s, &m, &h, &day, &date, &month);
    int n;
    scanf("%d", &n);
    while(n--) {
        long long t;
        scanf("%lld", &t);
        printf("%lld\n", solve(t + 1, s, m, h, day - 1,
                    date - 1, month - 1));
    }
}


#include <cstdio>
using namespace std;
constexpr int dayofyear(int y) {
    y += 1970;
    return (y % 400 == 0) || (y % 100 != 0 && y % 4 == 0)
            ? 366 : 365;
}
int sdoy[401];
int sumdayofyear(int y) {
    return (y / 400) * sdoy[400] + sdoy[y % 400];
}
int getyear(int d) {
    int y = 1;
    while(sumdayofyear(y) <= d)
        y <<= 1;
    int ans = 0;
    for(int j = y; j >= 1; j >>= 1)
        if(sumdayofyear(ans + j) <= d)
            ans += j;
    return ans;
}
constexpr int getday(long long t) {
    return (t / 86400 + 3) % 7;
}
constexpr int dom[12] =
    {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
constexpr int domy(int m, int y) {
    if(m == 1) {
        y += 1970;
        return (y % 400 == 0) || (y % 100 != 0 && y % 4 == 0)
                ? dom[m] + 1 : dom[m];
    } else
        return dom[m];
}
int getdate(long long t) {
    int d = t / 86400;
    int y = getyear(d);
    d -= sumdayofyear(y);
    for(int i = 0; i < 12; i++) {
        if(d < domy(i, y))
            return d;
        d -= domy(i, y);
    }
    __builtin_unreachable();
}
int getmonth(long long t) {
    int d = t / 86400;
    int y = getyear(d);
    d -= sumdayofyear(y);
    for(int i = 0; i < 12; i++) {
        if(d < domy(i, y))
            return i;
        d -= domy(i, y);
    }
    __builtin_unreachable();
}
long long solve(long long t, int s, int m, int h,
        int day, int date, int month) {
    if(s != -1)
        while(t % 60 != s)
            t++;
    if(m != -1)
        while(t / 60 % 60 != m) {
            t += 60;
            if(s == -1)
                t -= t % 60;
        }
    if(h != -1)
        while(t / 3600 % 24 != h) {
            t += 3600;
            if(m == -1)
                t -= t / 60 % 60 * 60;
            if(s == -1)
                t -= t % 60;
        }
    while((month != -2 && getmonth(t) != month) ||
            (day != -2 && date != -2 && getday(t) != day && getdate(t) != date) ||
            (day != -2 && date == -2 && getday(t) != day) ||
            (day == -2 && date != -2 && getdate(t) != date)) {
        if(month != -2 && getmonth(t) != month) {
            t += (domy(getmonth(t), getyear(t / 86400)) - getday(t)) * 86400;
        } else {
            t += 86400;
        }
        if(h == -1)
            t -= t / 3600 % 24 * 3600;
        if(m == -1)
            t -= t / 60 % 60 * 60;
        if(s == -1)
            t -= t % 60;
    }
    return t;
}
int main() {
    for(int i = 0; i < 400; i++)
        sdoy[i + 1] = sdoy[i] + dayofyear(i);

    int s, m, h, day, date, month;
    scanf("%d%d%d%d%d%d", &s, &m, &h, &day, &date, &month);
    int n;
    scanf("%d", &n);
    while(n--) {
        long long t;
        scanf("%lld", &t);
        printf("%lld\n", solve(t + 1, s, m, h, day - 1,
                    date - 1, month - 1));
    }
}


#include <cstdio>
using namespace std;
int dayofyear(int y) {
    y += 1970;
    return (y % 400 == 0) || (y % 100 != 0 && y % 4 == 0)
            ? 366 : 365;
}
int sdoy[401];
int sumdayofyear(int y) {
//    printf("y%d\n", y);
    return (y / 400) * sdoy[400] + sdoy[y % 400];
}
int getyear(int d) {
    int y = 1;
    while(sumdayofyear(y) <= d)
        y <<= 1;
    int ans = 0;
    for(int j = y; j >= 1; j >>= 1)
        if(sumdayofyear(ans + j) <= d)
            ans += j;
    return ans;
}
int getday(long long t) {
    return (t / 86400 + 3) % 7;
}
constexpr int dom[12] =
    {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int domy(int m, int y) {
    if(m == 2) {
        y += 1970;
        return (y % 400 == 0) || (y % 100 != 0 && y % 4 == 0)
                ? dom[m] + 1 : dom[m];
    } else
        return dom[m];
}
int getdate(long long t) {
    int d = t / 86400;
    int y = getyear(d);
    d -= sumdayofyear(y);
    for(int i = 0; i < 12; i++) {
        if(d < domy(i, y))
            return d;
        d -= domy(i, y);
    }
    __builtin_unreachable();
}
int getmonth(long long t) {
    int d = t / 86400;
    int y = getyear(d);
    d -= sumdayofyear(y);
    for(int i = 0; i < 12; i++) {
        if(d < domy(i, y))
            return i;
        d -= domy(i, y);
    }
    __builtin_unreachable();
}
long long solve(long long t, int s, int m, int h,
        int day, int date, int month) {
    if(s != -1)
        while(t % 60 != s)
            t++;
    if(m != -1)
        while(t / 60 % 60 != m) {
            t += 60;
            if(s == -1)
                t -= t % 60;
        }
    if(h != -1)
        while(t / 3600 % 24 != h) {
            t += 3600;
            if(m == -1)
                t -= t / 60 % 60 * 60;
            if(s == -1)
                t -= t % 60;
        }
    while((month != -2 && getmonth(t) != month) ||
            (day != -2 && date != -2 && getday(t) != day && getdate(t) != date) ||
            (day != -2 && date == -2 && getday(t) != day) ||
            (day == -2 && date != -2 && getdate(t) != date)) {
        if(month != -2 && getmonth(t) != month) {
            t += (domy(getmonth(t), getyear(t / 86400)) - getday(t)) * 86400;
        } else {
            t += 86400;
        }
        if(h == -1)
            t -= t / 3600 % 24 * 3600;
        if(m == -1)
            t -= t / 60 % 60 * 60;
        if(s == -1)
            t -= t % 60;
    }
    return t;
}
int main() {
    for(int i = 0; i < 400; i++)
        sdoy[i + 1] = sdoy[i] + dayofyear(i);

    int s, m, h, day, date, month;
    scanf("%d%d%d%d%d%d", &s, &m, &h, &day, &date, &month);
    int n;
    scanf("%d", &n);
    while(n--) {
        long long t;
        scanf("%lld", &t);
        printf("%lld\n", solve(t + 1, s, m, h, day - 1,
                    date - 1, month - 1));
    }
}


#include <cstdio>
using namespace std;
int dayofyear(int y) {
    y += 1970;
    return (y % 400 == 0) || (y % 100 != 0 && y % 4 == 0)
            ? 366 : 365;
}
int sdoy[401];
int sumdayofyear(int y) {
//    printf("y%d\n", y);
    return (y / 400) * sdoy[400] + sdoy[y % 400];
}
int getyear(int d) {
    int y = 1;
    while(sumdayofyear(y) <= d)
        y <<= 1;
    int ans = 0;
    for(int j = y; j >= 1; j >>= 1)
        if(sumdayofyear(ans + j) <= d)
            ans += j;
    return ans;
}
int getday(long long t) {
    return (t / 86400 + 3) % 7;
}
constexpr int dom[12] =
    {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int domy(int m, int y) {
    if(m == 1) {
        y += 1970;
        return (y % 400 == 0) || (y % 100 != 0 && y % 4 == 0)
                ? dom[m] + 1 : dom[m];
    } else
        return dom[m];
}
int getdate(long long t) {
    int d = t / 86400;
    int y = getyear(d);
    d -= sumdayofyear(y);
    for(int i = 0; i < 12; i++) {
        if(d < domy(i, y))
            return d;
        d -= domy(i, y);
    }
    __builtin_unreachable();
}
int getmonth(long long t) {
    int d = t / 86400;
    int y = getyear(d);
    d -= sumdayofyear(y);
    for(int i = 0; i < 12; i++) {
        if(d < domy(i, y))
            return i;
        d -= domy(i, y);
    }
    __builtin_unreachable();
}
long long solve(long long t, int s, int m, int h,
        int day, int date, int month) {
    if(s != -1)
        while(t % 60 != s)
            t++;
    if(m != -1)
        while(t / 60 % 60 != m) {
            t += 60;
            if(s == -1)
                t -= t % 60;
        }
    if(h != -1)
        while(t / 3600 % 24 != h) {
            t += 3600;
            if(m == -1)
                t -= t / 60 % 60 * 60;
            if(s == -1)
                t -= t % 60;
        }
    while((month != -2 && getmonth(t) != month) ||
            (day != -2 && date != -2 && getday(t) != day && getdate(t) != date) ||
            (day != -2 && date == -2 && getday(t) != day) ||
            (day == -2 && date != -2 && getdate(t) != date)) {
        if(month != -2 && getmonth(t) != month) {
            t += (domy(getmonth(t), getyear(t / 86400)) - getday(t)) * 86400;
        } else {
            t += 86400;
        }
        if(h == -1)
            t -= t / 3600 % 24 * 3600;
        if(m == -1)
            t -= t / 60 % 60 * 60;
        if(s == -1)
            t -= t % 60;
    }
    return t;
}
int main() {
    for(int i = 0; i < 400; i++)
        sdoy[i + 1] = sdoy[i] + dayofyear(i);

    int s, m, h, day, date, month;
    scanf("%d%d%d%d%d%d", &s, &m, &h, &day, &date, &month);
    int n;
    scanf("%d", &n);
    while(n--) {
        long long t;
        scanf("%lld", &t);
        printf("%lld\n", solve(t + 1, s, m, h, day - 1,
                    date - 1, month - 1));
    }
}


// CONTEST SOURCE
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <algorithm>
#include <map>
#include <queue>
#include <sstream>
#include <set>
//#include <priority_queue>
using namespace std;
#define ll long long
#define x first
#define y second
#define pii pair<int, int>
#define pdd pair<double, double>
#define L(s) (int)(s).size()
#define VI vector<int>
#define all(s) (s).begin(), (s).end()
#define pb push_back
#define mp make_pair
#define inf 1000000000

vector<int>  day_seq;
vector<int> main_seq;

int sec, mn, hr, dw, dm, month;
int mlen[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
int main() {
  cin >> sec >> mn >> hr >> dw >> dm >> month;
  if (dw > 0)--dw;
  if (dm > 0)--dm;
  if (month > 0) --month;

  int ctr0, ctr1;

  ctr0 = 0;
  for(int h = 0; h < 24; ++h) {
    for(int m = 0; m < 60; ++m) {
      for(int s = 0; s < 60; ++s) {
        if (h == hr || hr == -1) {
          if (m == mn || mn == -1) {
            if (s == sec || sec == -1) {
              day_seq.pb(ctr0);
            }
          }
        }
        ++ctr0;
      }
    }
  }

  int st = 3;
  ctr1 = 0;
  for(int y = 0; y < 400; ++y) {
    int is_leap = 0;
    if (y % 4 == 0 && (y % 400 == 0 || y % 100 > 0)) is_leap = 1;
    if (is_leap) mlen[1]++;

    for(int m = 0; m < 12; ++m) {
      for(int d = 0; d < mlen[m]; ++d) {

        if (m == month || month == -1) {
          if (dm != -1 && dw != -1) {
            if (d == dm || st == dw) {
              main_seq.pb(ctr1);
            }
          }
          if (dm == -1 && dw == -1) {
            main_seq.pb(ctr1);
          }
          if (dm == -1 && st == dw) {
            main_seq.pb(ctr1);
          }
          if (dw == -1 && d == dm) {
            main_seq.pb(ctr1);
          }
        }

        ++st; if (st == 7) st = 0;
        ++ctr1;
      }
    }

    if (is_leap) mlen[1]--;
  }

  int ctr;
  cin >> ctr;
  while(ctr--) {
    ll now; cin >> now;
    ll main_div = now / (1LL * ctr0 * ctr1);
    ll main_mod = now % (1LL * ctr0 * ctr1);
    ll day_div = main_mod / ctr0;
    ll day_mod = main_mod % ctr0;

    if (day_div > main_seq.back() || (day_div == main_seq.back() && day_mod >= day_seq.back())) {
      day_div = main_seq[0];
      day_mod = day_seq[0];
      ++main_div;
    } else
      if (day_mod >= day_seq.back() || (*lower_bound(all(main_seq), day_div)) != day_div) {
        day_div = (*upper_bound(all(main_seq), day_div));
        day_mod = day_seq[0];
      } else {
        day_mod = (*upper_bound(all(day_seq), day_mod));
      }

    cout << main_div * ctr0 * ctr1 + day_div * ctr0 + day_mod << endl;
  }

}


// CONTEST SOURCE
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <algorithm>
#include <map>
#include <queue>
#include <sstream>
#include <set>
//#include <priority_queue>
using namespace std;
#define ll long long
#define x first
#define y second
#define pii pair<int, int>
#define pdd pair<double, double>
#define L(s) (int)(s).size()
#define VI vector<int>
#define all(s) (s).begin(), (s).end()
#define pb push_back
#define mp make_pair
#define inf 1000000000

vector<int>  day_seq;
vector<int> main_seq;

int sec, mn, hr, dw, dm, month;
int mlen[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
int main() {
  cin >> sec >> mn >> hr >> dw >> dm >> month;
  if (dw > 0)--dw;
  if (dm > 0)--dm;
  if (month > 0) --month;

  int ctr0, ctr1;

  ctr0 = 0;
  for(int h = 0; h < 24; ++h) {
    for(int m = 0; m < 60; ++m) {
      for(int s = 0; s < 60; ++s) {
        if (h == hr || hr == -1) {
          if (m == mn || mn == -1) {
            if (s == sec || sec == -1) {
              day_seq.pb(ctr0);
            }
          }
        }
        ++ctr0;
      }
    }
  }

  int st = 3;
  ctr1 = 0;
  for(int _y = 0; _y < 400; ++_y) {
    int is_leap = 0;
    int y = _y + 1970;
    if (y % 4 == 0 && (y % 400 == 0 || y % 100 > 0)) is_leap = 1;
    if (is_leap) mlen[1]++;

    for(int m = 0; m < 12; ++m) {
      for(int d = 0; d < mlen[m]; ++d) {

        if (m == month || month == -1) {
          if (dm != -1 && dw != -1) {
            if (d == dm || st == dw) {
              main_seq.pb(ctr1);
            }
          }
          if (dm == -1 && dw == -1) {
            main_seq.pb(ctr1);
          }
          if (dm == -1 && st == dw) {
            main_seq.pb(ctr1);
          }
          if (dw == -1 && d == dm) {
            main_seq.pb(ctr1);
          }
        }

        ++st; if (st == 7) st = 0;
        ++ctr1;
      }
    }

    if (is_leap) mlen[1]--;
  }

  int ctr;
  cin >> ctr;
  while(ctr--) {
    ll now; cin >> now;
    ll main_div = now / (1LL * ctr0 * ctr1);
    ll main_mod = now % (1LL * ctr0 * ctr1);
    ll day_div = main_mod / ctr0;
    ll day_mod = main_mod % ctr0;

    if (day_div > main_seq.back() || (day_div == main_seq.back() && day_mod >= day_seq.back())) {
      day_div = main_seq[0];
      day_mod = day_seq[0];
      ++main_div;
    } else
      if (day_mod >= day_seq.back() || (*lower_bound(all(main_seq), day_div)) != day_div) {
        day_div = (*upper_bound(all(main_seq), day_div));
        day_mod = day_seq[0];
      } else {
        day_mod = (*upper_bound(all(day_seq), day_mod));
      }

    cout << main_div * ctr0 * ctr1 + day_div * ctr0 + day_mod << endl;
  }

}


#include<bits/stdc++.h>
using namespace std;

typedef long long LL;

int sec[86500], cs, day[150000], ds, cur;
int s, m, h, d, dt, mt, T;
LL perioid, dd, ss, x, ans;

bool leap(int y){
	if(y % 4) return false;
	if(y % 100) return true;
	if(y % 400) return false;
	return true;
}

int leap_month(int y, int m){
	return (leap(y) && m == 1);
}

bool valid(int _mt, int _dt, int _d){
	if(mt != -1 && mt != _mt) return false;
	if( (dt == -1 && d == -1) || dt == _dt || d == _d) return true;
	return false;
}

const int dmon[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

void prepare(){
	for(int i = 0; i < 24; i ++) if(h == -1 || h == i)
		for(int j = 0; j < 60; j ++) if(m == -1 || m == j)
			for(int k = 0; k < 60; k ++) if(s == -1 || s == k)
				sec[cs ++] = i * 3600 + j * 60 + k;
	for(int i = 0; i < 400; i ++){
		for(int j = 0; j < 12; j ++){
			int tot = dmon[j] + leap_month(i + 1970, j);
			for(int k = 0; k < tot; k ++){
				if(valid(j, k, (cur + 3) % 7) ) day[ds ++] = cur;
				cur ++;
			}
		}
	}
	perioid = cur * 86400ll;
}

int main(){
	scanf("%d %d %d %d %d %d", &s, &m, &h, &d, &dt, &mt);
   if(d > 0) d --;
   if(dt > 0) dt --;
   if(mt > 0) mt --;
	prepare();
	for(scanf("%d", &T); T --; ){
		scanf("%I64d", &x);
		ans = x - x % perioid;
		x = x - ans;
		dd = x / 86400;
		ss = x % 86400;
		int it = lower_bound(day, day + ds, dd) - day;
		if(it == ds) ans += perioid + day[0] * 86400ll + sec[0];
		else if(day[it] > dd) ans += day[it] * 86400ll + sec[0];
		else{
			int id = upper_bound(sec, sec + cs, ss) - sec;
			if(id < cs) ans += day[it] * 86400ll + sec[id];
			else{
				it ++;
				if(it == ds) ans += perioid + day[0] * 86400ll + sec[0];
				else ans += day[it] * 86400ll + sec[0];
			}
		}
		printf("%I64d\n", ans);
	}
}


#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")

#include <bits/stdc++.h>

#define pb push_back
#define fi first
#define se second
#define all(v) v.begin(), v.end()

using namespace std;
using ll = int64_t;

constexpr int N = 1000;
constexpr ll full_day = 24 * 60 * 60;
constexpr ll INF = 1e18;

ll ts, tmin, th, tdow, tday, tmonth;
int n;
pair<ll, int> moments[N];
ll results[N];
bool already[N];
vector<ll> time_moments;

ll month_days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

inline bool is_leap(int year) {
  return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
}

inline int get_days(int year, int month) {
  return month_days[month - 1] + (month == 2 && is_leap(year));
}

inline bool is_match(int month, int day, int dow) {
  bool ok = (tmonth == -1 || month == tmonth);

  if (tdow != -1 && tday != -1) {
    ok &= (day == tday || dow == tdow);
  } else {
    ok &= (day == tday || tday == -1);
    ok &= (dow == tdow || tdow == -1);
  }

  return ok;
}

void try_to_update(ll day_moment, int index) {
  ll input_moment = moments[index].fi;
  index = moments[index].se;

  if (already[index])
    return;

  ll result = INF;

  if (day_moment + time_moments[0] > input_moment) {
    result = day_moment + time_moments[0];
  }

  ll remain = input_moment % full_day;
  auto it = upper_bound(all(time_moments), remain);

  if (it != time_moments.end()) {
    result = min(result, day_moment + *it);
  }

  if (result != INF) {
    already[index] = true;
    results[index] = result;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  cin >> ts >> tmin >> th >> tdow >> tday >> tmonth;

  for (ll s = 0; s <= 59; s++) {
    for (ll m = 0; m <= 59; m++) {
      for (ll h = 0; h <= 23; h++) {
        ll moment = h * 60 * 60 + m * 60 + s;
        bool ok = (ts == -1 || s == ts);
        ok &= (tmin == -1 || m == tmin);
        ok &= (th == -1 || h == th);
        if (ok)
          time_moments.pb(moment);
      }
    }
  }
  sort(all(time_moments));
  assert(!time_moments.empty());

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> moments[i].fi;
    moments[i].se = i;
  }
  sort(moments, moments + n);

  ll moment = 0;
  int dow = 4;
  int cursor = 0;

  for (int year = 1970; cursor < n; ++year) {
    for (int month = 1; month <= 12; month++) {
      int days = get_days(year, month);
      for (int day = 1; day <= days; day++) {

        if (is_match(month, day, dow)) {
          //cerr << "match: " << moment << " " << year << " " << month << " " << day << " " << tdow << endl;
          for (int i = cursor; i < n; i++) {
            if (moments[i].fi >= moment + full_day)
              break;
            try_to_update(moment, i);
          }
          while (cursor < n && already[cursor]) {
            ++cursor;
          }
        }

        dow = (dow) % 7 + 1;
        moment += full_day;
      }
    }
  }

  for (int i = 0; i < n; i++)
    cout << results[i] << endl;

  return 0;
}


#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")

#include <bits/stdc++.h>

#define pb push_back
#define fi first
#define se second
#define all(v) v.begin(), v.end()

using namespace std;
using ll = int64_t;

constexpr int N = 1000;
constexpr ll full_day = 24 * 60 * 60;
constexpr ll INF = 1e18;

ll ts, tmin, th, tdow, tday, tmonth;
int n;
pair<ll, int> moments[N];
ll results[N];
bool already[N];
vector<ll> time_moments;

ll month_days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

inline bool is_leap(int year) {
  return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
}

inline int get_days(int year, int month) {
  return month_days[month - 1] + (month == 2 && is_leap(year));
}

inline bool is_match(int month, int day, int dow) {
  bool ok = (tmonth == -1 || month == tmonth);

  if (tdow != -1 && tday != -1) {
    ok &= (day == tday || dow == tdow);
  } else {
    ok &= (day == tday || tday == -1);
    ok &= (dow == tdow || tdow == -1);
  }

  return ok;
}

void try_to_update(ll day_moment, int index) {
  ll input_moment = moments[index].fi;
  index = moments[index].se;

  if (already[index])
    return;

  ll result = INF;

  if (day_moment + time_moments[0] > input_moment) {
    result = day_moment + time_moments[0];
  }

  ll remain = input_moment % full_day;
  auto it = upper_bound(all(time_moments), remain);

  if (it != time_moments.end()) {
    result = min(result, day_moment + *it);
  }

  if (result != INF) {
    already[index] = true;
    results[index] = result;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  cin >> ts >> tmin >> th >> tdow >> tday >> tmonth;

  for (ll s = 0; s <= 59; s++) {
    for (ll m = 0; m <= 59; m++) {
      for (ll h = 0; h <= 23; h++) {
        ll moment = h * 60 * 60 + m * 60 + s;
        bool ok = (ts == -1 || s == ts);
        ok &= (tmin == -1 || m == tmin);
        ok &= (th == -1 || h == th);
        if (ok)
          time_moments.pb(moment);
      }
    }
  }
  sort(all(time_moments));
  assert(!time_moments.empty());

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> moments[i].fi;
    moments[i].se = i;
  }
  sort(moments, moments + n);

  ll moment = 0;
  int dow = 4;
  int cursor = 0;

  for (int year = 1970; cursor < n; ++year) {
    for (int month = 1; month <= 12; month++) {
      int days = get_days(year, month);
      for (int day = 1; day <= days; day++) {

        if (is_match(month, day, dow)) {
          //cerr << "match: " << moment << " " << year << " " << month << " " << day << " " << tdow << endl;
          for (int i = cursor; i < n; i++) {
            if (moments[i].fi >= moment + full_day) {
              //cerr << cursor << " " << i << endl;
              break;
            }
            try_to_update(moment, i);
          }
          while (cursor < n && already[moments[cursor].se]) {
            ++cursor;
          }
        }

        dow = (dow) % 7 + 1;
        moment += full_day;
      }
    }
  }

  for (int i = 0; i < n; i++)
    cout << results[i] << endl;

  return 0;
}


#pragma comment(linker, "/STACK:66777216")
#pragma warning(disable : 4996)

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#pragma hdrstop

static constexpr int kDayMonth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

#include <cstdint>

#ifdef _MSC_VER
#include <intrin.h>

#else
#define popcount(a) __builtin_popcount(a)
#define clz(a) __builtin_clz(a)
#define ctz(a) __builtin_ctz(a)
#endif

#ifdef _MSC_VER

#endif

template<typename T, size_t N>
struct MakeVector {

};

template<typename T>
struct MakeVector<T, 1> {
	/// caide keep
	template<typename R = std::vector<T>>
	static R make_vector(std::size_t size, const T& value) {
		return R(size, value);
	}
};

#ifdef _MSC_VER

#else
#define LLD "%lld"
#define LLU "%llu"
#endif

#define ll long long

#include <iterator>

template<typename T>
class IntegerIterator : public std::iterator<std::input_iterator_tag, T, std::ptrdiff_t, T*, T> {
public:
	using value_type = T;

	constexpr explicit IntegerIterator(const value_type value) : value_(value) {}

	IntegerIterator& operator++() {
		++value_;
		return *this;
	}

	constexpr value_type operator*() const {
		return value_;
	}

	constexpr bool operator ==(const IntegerIterator rhs) const {
		return value_ == rhs.value_;
	}

	constexpr bool operator !=(const IntegerIterator rhs) const {
		return !(*this == rhs);
	}

private:
	value_type value_;

};

template<typename T>
class IntegerRange {
public:
	using const_iterator = IntegerIterator<T>;

	constexpr IntegerRange(const T begin, const T end) : begin_(begin), end_(end) {}

	constexpr const_iterator begin() const {
		return const_iterator(begin_);
	}

	constexpr const_iterator end() const {
		return const_iterator(end_);
	}

private:
	T begin_;
	T end_;

};

template<typename T>
constexpr IntegerRange<T> range(const T to) {
	return IntegerRange<T>(0, to);
}

template<typename T>
inline bool is_leap_year(const T year) {
	return year % 400 == 0 || (year % 100 != 0 && (year & 3) == 0);
}

constexpr const int BASE_MOD = 1000000007;  /// caide keep

template<typename T>
inline T& add_mod(T& a, const T b, const T mod = BASE_MOD) {
	if ((a += b) >= mod) {
		a -= mod;
	}
	return a;
}

template<typename T, T MOD = BASE_MOD>
class ModInt {
public:

	/// caide keep
	constexpr ModInt() : ModInt(0) {}

private:
	T value_;

};

using namespace std;

int __;

struct Date {
	int year;
	int month;
	int day;
	int weekday;

	Date() : year(1970), month(0), day(0), weekday(3) {}

	bool is_leap() const {
		return is_leap_year(year);
	}

};

inline int get_days_in_month(const int month, const int year) {
	const int days = kDayMonth[month] + (month == 1 && is_leap_year(year) ? 1 : 0);
	return days;
}

struct Moment {
	Date date;
	int hour;
	int minute;
	int second;

	Moment() : date(), hour(0), minute(0), second(0) {}

	friend istream& operator >>(istream& in, Moment& rhs) {
		in >> rhs.second >> rhs.minute >> rhs.hour >> rhs.date.weekday >> rhs.date.day >> rhs.date.month;
		if (rhs.date.weekday != -1) {
			--rhs.date.weekday;
		}
		if (rhs.date.day != -1) {
			--rhs.date.day;
		}
		if (rhs.date.month != -1) {
			--rhs.date.month;
		}
		return in;
	}

	void next_month() {
		const int days_rem = get_days_in_month(date.month, date.year) - date.day;
		add_mod(date.weekday, days_rem % 7, 7);
		++date.month;
		if (date.month == 12) {
			date.month = 0;
			++date.year;
		}
		date.day = 0;
		hour = 0;
		minute = 0;
		second = 0;
	}

	void next_day() {
		const int days = get_days_in_month(date.month, date.year);
		++date.day;
		add_mod(date.weekday, 1, 7);
		hour = 0;
		minute = 0;
		second = 0;
		if (date.day == days) {
			next_month();
		}
	}

	void next_hour() {
		++hour;
		minute = 0;
		second = 0;
		if (hour == 24) {
			next_day();
		}
	}

	void next_minute() {
		++minute;
		second = 0;
		if (minute == 60) {
			next_hour();
		}
	}

	void next_second() {
		++second;
		if (second == 60) {
			next_minute();
		}
	}

	void set_month(const int month) {
		bool first = true;
		while (date.month != month || first) {
			first = false;
			next_month();
		}
	}

};

namespace parser {

inline void parse_year(Date& date, ll& time_passed) {
	while (true) {
		const int days = 365 + (date.is_leap() ? 1 : 0);
		const int seconds = days * 86400;
		if (time_passed < seconds) {
			break;
		}
		time_passed -= seconds;
		++date.year;
		add_mod(date.weekday, days % 7, 7);
	}
}

inline void parse_month(Date& date, ll& time_passed) {
	for (size_t month = 0; month < 12; ++month) {
		const int days = get_days_in_month(month, date.year);// kDayMonth[i] + (i == 1 && date.is_leap() ? 1 : 0);
		const int seconds = days * 86400;
		if (time_passed < seconds) {
			break;
		}
		time_passed -= seconds;
		++date.month;
		add_mod(date.weekday, days % 7, 7);
	}
}

inline void parse_day(Date& date, ll& time_passed) {
	const int seconds = 86400;
	date.day = time_passed / seconds;
	add_mod(date.weekday, date.day % 7, 7);
	time_passed %= seconds;
}

inline Date parse_date(ll& time_passed) {
	Date date;
	parse_year(date, time_passed);
	parse_month(date, time_passed);
	parse_day(date, time_passed);
	return date;
}

inline int parse_hour(ll& time_passed) {
	const int seconds = 3600;
	const int hour = time_passed / seconds;
	time_passed %= seconds;
	return hour;
}

inline int parse_minute(ll& time_passed) {
	const int seconds = 60;
	const int minute = time_passed / seconds;
	time_passed %= seconds;
	return minute;
}

inline int parse_second(ll& time_passed) {
	const int second = time_passed;
	time_passed = 0;
	return second;
}

inline Moment parse_moment(ll time_passed) {
	Moment moment;
	moment.date = parse_date(time_passed);
	moment.hour = parse_hour(time_passed);
	moment.minute = parse_minute(time_passed);
	moment.second = parse_second(time_passed);
	return moment;
}

}  // namespace parser

namespace encoder {

inline ll encode_year(const Date& date) {
	ll time = 0;
	for (int year = 1970; year < date.year; ++year) {
		const int days = 365 + (is_leap_year(year) ? 1 : 0);
		const int seconds = days * 86400;
		time += seconds;
	}
	return time;
}

inline ll encode_month(const Date& date) {
	ll time = 0;
	for (int month = 0; month < date.month; ++month) {
		const int days = get_days_in_month(month, date.year); // kDayMonth[month] + (month == 1 && date.is_leap() ? 1 : 0);
		const int seconds = days * 86400;
		time += seconds;
	}
	return time;
}

inline ll encode_day(const Date& date) {
	const int seconds = 86400;
	return date.day * seconds;
}

inline ll encode_date(const Date& date) {
	ll time = 0;
	time += encode_year(date);
	time += encode_month(date);
	time += encode_day(date);
	return time;
}

inline ll encode_hour(const int hour) {
	const int seconds = 3600;
	return hour * seconds;
}

inline ll encode_minute(const int minute) {
	const int seconds = 60;
	return minute * seconds;
}

inline ll encode_second(const int second) {
	return second;
}

inline ll encode_moment(const Moment& moment) {
	ll time = 0;
	time += encode_date(moment.date);
	time += encode_hour(moment.hour);
	time += encode_minute(moment.minute);
	time += encode_second(moment.second);
	return time;
}

}  // namespace encoder

bool test_minute(Moment& good_moment, const Moment& config) {
	Moment cur = good_moment;
	for (int second = cur.second; second < 60; ++second) {
		bool ok = (config.second == -1 || config.second == cur.second);
		if (ok) {
			good_moment = cur;
			return true;
		}
		cur.next_second();
	}
	return false;
}

bool test_hour(Moment& good_moment, const Moment& config) {
	Moment cur = good_moment;
	for (int minute = cur.minute; minute < 60; ++minute) {
		bool ok = (config.minute == -1 || config.minute == cur.minute);
		if (ok && test_minute(cur, config)) {
			good_moment = cur;
			return true;
		}
		cur.next_minute();
	}
	return false;
}

bool test_day(Moment& good_moment, const Moment& config) {
	Moment cur = good_moment;
	for (int hour = cur.hour; hour < 24; ++hour) {
		bool ok = (config.hour == -1 || config.hour == cur.hour);
		if (ok && test_hour(cur, config)) {
			good_moment = cur;
			return true;
		}
		cur.next_hour();
	}
	return false;
}

bool test_month(Moment& good_moment, const Moment& config) {
	const int days = get_days_in_month(good_moment.date.month, good_moment.date.year);
	Moment cur = good_moment;
	for (int day = good_moment.date.day; day < days; ++day) {
		bool ok = false;
		if (config.date.day == -1) {
			ok |= (config.date.weekday == -1 || config.date.weekday == cur.date.weekday);
		} else {
			if (config.date.weekday != -1) {
				ok |= (config.date.day == cur.date.day || config.date.weekday == cur.date.weekday);
			} else {
				ok |= (config.date.day == cur.date.day);
			}
		}
		if (ok && test_day(cur, config)) {
			good_moment = cur;
			return true;
		}
		cur.next_day();
	}
	return false;
}

void solve(std::istream& in, std::ostream& out) {
	Moment config;
	in >> config >> __;
	for (int _ : range(__)) {
		ll time_passed;
		in >> time_passed;
		const Moment current_moment = parser::parse_moment(time_passed);
		// const ll encoded = encoder::encode_moment(current_moment);
		// out << boolalpha << current_moment << " " << encoded << " " << (encoded == time_passed) << endl;
		Moment good_moment = current_moment;
		good_moment.next_second();

		if (config.date.month != -1 && config.date.month != good_moment.date.month) {
			good_moment.set_month(config.date.month);
		}

		while (!test_month(good_moment, config)) {
			if (config.date.month != -1) {
				good_moment.set_month(config.date.month);
			}
			else {
				good_moment.next_month();
			}
		}

		const ll ans = encoder::encode_moment(good_moment);
		out << ans << endl;
	}
}

#include <fstream>

int main() {
    srand(time(NULL));

    ios_base::sync_with_stdio(0);
    cin.tie(0);

    istream& in = cin;

    ostream& out = cout;

    out << fixed << setprecision(16);
    solve(in, out);
    return 0;
}


#pragma comment(linker, "/STACK:66777216")
#pragma warning(disable : 4996)

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#pragma hdrstop

static constexpr int kDayMonth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

#include <cstdint>

#ifdef _MSC_VER
#include <intrin.h>

#else
#define popcount(a) __builtin_popcount(a)
#define clz(a) __builtin_clz(a)
#define ctz(a) __builtin_ctz(a)
#endif

#ifdef _MSC_VER

#endif

template<typename T, size_t N>
struct MakeVector {

};

template<typename T>
struct MakeVector<T, 1> {
	/// caide keep
	template<typename R = std::vector<T>>
	static R make_vector(std::size_t size, const T& value) {
		return R(size, value);
	}
};

#ifdef _MSC_VER

#else
#define LLD "%lld"
#define LLU "%llu"
#endif

#define ll long long

#include <iterator>

template<typename T>
class IntegerIterator : public std::iterator<std::input_iterator_tag, T, std::ptrdiff_t, T*, T> {
public:
	using value_type = T;

	constexpr explicit IntegerIterator(const value_type value) : value_(value) {}

	IntegerIterator& operator++() {
		++value_;
		return *this;
	}

	constexpr value_type operator*() const {
		return value_;
	}

	constexpr bool operator ==(const IntegerIterator rhs) const {
		return value_ == rhs.value_;
	}

	constexpr bool operator !=(const IntegerIterator rhs) const {
		return !(*this == rhs);
	}

private:
	value_type value_;

};

template<typename T>
class IntegerRange {
public:
	using const_iterator = IntegerIterator<T>;

	constexpr IntegerRange(const T begin, const T end) : begin_(begin), end_(end) {}

	constexpr const_iterator begin() const {
		return const_iterator(begin_);
	}

	constexpr const_iterator end() const {
		return const_iterator(end_);
	}

private:
	T begin_;
	T end_;

};

template<typename T>
constexpr IntegerRange<T> range(const T to) {
	return IntegerRange<T>(0, to);
}

template<typename T>
inline bool is_leap_year(const T year) {
	return year % 400 == 0 || (year % 100 != 0 && (year & 3) == 0);
}

constexpr const int BASE_MOD = 1000000007;  /// caide keep

template<typename T>
inline T& add_mod(T& a, const T b, const T mod = BASE_MOD) {
	if ((a += b) >= mod) {
		a -= mod;
	}
	return a;
}

template<typename T, T MOD = BASE_MOD>
class ModInt {
public:

	/// caide keep
	constexpr ModInt() : ModInt(0) {}

private:
	T value_;

};

using namespace std;

int __;

struct Date {
	int year;
	int month;
	int day;
	int weekday;

	Date() : year(1970), month(0), day(0), weekday(3) {}

	bool is_leap() const {
		return is_leap_year(year);
	}

};

inline int get_days_in_month(const int month, const int year) {
	const int days = kDayMonth[month] + (month == 1 && is_leap_year(year) ? 1 : 0);
	return days;
}

struct Moment {
	Date date;
	int hour;
	int minute;
	int second;

	Moment() : date(), hour(0), minute(0), second(0) {}

	friend istream& operator >>(istream& in, Moment& rhs) {
		in >> rhs.second >> rhs.minute >> rhs.hour >> rhs.date.weekday >> rhs.date.day >> rhs.date.month;
		if (rhs.date.weekday != -1) {
			--rhs.date.weekday;
		}
		if (rhs.date.day != -1) {
			--rhs.date.day;
		}
		if (rhs.date.month != -1) {
			--rhs.date.month;
		}
		return in;
	}

	void next_month() {
		const int days_rem = get_days_in_month(date.month, date.year);
		add_mod(date.weekday, days_rem % 7, 7);
		++date.month;
		if (date.month == 12) {
			date.month = 0;
			++date.year;
		}
		date.day = 0;
		hour = 0;
		minute = 0;
		second = 0;
	}

	void next_day() {
		const int days = get_days_in_month(date.month, date.year);
		++date.day;
		add_mod(date.weekday, 1, 7);
		hour = 0;
		minute = 0;
		second = 0;
		if (date.day == days) {
			next_month();
		}
	}

	void next_hour() {
		++hour;
		minute = 0;
		second = 0;
		if (hour == 24) {
			next_day();
		}
	}

	void next_minute() {
		++minute;
		second = 0;
		if (minute == 60) {
			next_hour();
		}
	}

	void next_second() {
		++second;
		if (second == 60) {
			next_minute();
		}
	}

	void set_month(const int month) {
		bool first = true;
		while (date.month != month || first) {
			first = false;
			next_month();
		}
	}

};

namespace parser {

inline void parse_year(Date& date, ll& time_passed) {
	while (true) {
		const int days = 365 + (date.is_leap() ? 1 : 0);
		const int seconds = days * 86400;
		if (time_passed < seconds) {
			break;
		}
		time_passed -= seconds;
		++date.year;
		add_mod(date.weekday, days % 7, 7);
	}
}

inline void parse_month(Date& date, ll& time_passed) {
	for (size_t month = 0; month < 12; ++month) {
		const int days = get_days_in_month(month, date.year);// kDayMonth[i] + (i == 1 && date.is_leap() ? 1 : 0);
		const int seconds = days * 86400;
		if (time_passed < seconds) {
			break;
		}
		time_passed -= seconds;
		++date.month;
		add_mod(date.weekday, days % 7, 7);
	}
}

inline void parse_day(Date& date, ll& time_passed) {
	const int seconds = 86400;
	date.day = time_passed / seconds;
	add_mod(date.weekday, date.day % 7, 7);
	time_passed %= seconds;
}

inline Date parse_date(ll& time_passed) {
	Date date;
	parse_year(date, time_passed);
	parse_month(date, time_passed);
	parse_day(date, time_passed);
	return date;
}

inline int parse_hour(ll& time_passed) {
	const int seconds = 3600;
	const int hour = time_passed / seconds;
	time_passed %= seconds;
	return hour;
}

inline int parse_minute(ll& time_passed) {
	const int seconds = 60;
	const int minute = time_passed / seconds;
	time_passed %= seconds;
	return minute;
}

inline int parse_second(ll& time_passed) {
	const int second = time_passed;
	time_passed = 0;
	return second;
}

inline Moment parse_moment(ll time_passed) {
	Moment moment;
	moment.date = parse_date(time_passed);
	moment.hour = parse_hour(time_passed);
	moment.minute = parse_minute(time_passed);
	moment.second = parse_second(time_passed);
	return moment;
}

}  // namespace parser

namespace encoder {

inline ll encode_year(const Date& date) {
	ll time = 0;
	for (int year = 1970; year < date.year; ++year) {
		const int days = 365 + (is_leap_year(year) ? 1 : 0);
		const int seconds = days * 86400;
		time += seconds;
	}
	return time;
}

inline ll encode_month(const Date& date) {
	ll time = 0;
	for (int month = 0; month < date.month; ++month) {
		const int days = get_days_in_month(month, date.year); // kDayMonth[month] + (month == 1 && date.is_leap() ? 1 : 0);
		const int seconds = days * 86400;
		time += seconds;
	}
	return time;
}

inline ll encode_day(const Date& date) {
	const int seconds = 86400;
	return date.day * seconds;
}

inline ll encode_date(const Date& date) {
	ll time = 0;
	time += encode_year(date);
	time += encode_month(date);
	time += encode_day(date);
	return time;
}

inline ll encode_hour(const int hour) {
	const int seconds = 3600;
	return hour * seconds;
}

inline ll encode_minute(const int minute) {
	const int seconds = 60;
	return minute * seconds;
}

inline ll encode_second(const int second) {
	return second;
}

inline ll encode_moment(const Moment& moment) {
	ll time = 0;
	time += encode_date(moment.date);
	time += encode_hour(moment.hour);
	time += encode_minute(moment.minute);
	time += encode_second(moment.second);
	return time;
}

}  // namespace encoder

bool test_minute(Moment& good_moment, const Moment& config) {
	Moment cur = good_moment;
	for (int second = cur.second; second < 60; ++second) {
		bool ok = (config.second == -1 || config.second == cur.second);
		if (ok) {
			good_moment = cur;
			return true;
		}
		cur.next_second();
	}
	return false;
}

bool test_hour(Moment& good_moment, const Moment& config) {
	Moment cur = good_moment;
	for (int minute = cur.minute; minute < 60; ++minute) {
		bool ok = (config.minute == -1 || config.minute == cur.minute);
		if (ok && test_minute(cur, config)) {
			good_moment = cur;
			return true;
		}
		cur.next_minute();
	}
	return false;
}

bool test_day(Moment& good_moment, const Moment& config) {
	Moment cur = good_moment;
	for (int hour = cur.hour; hour < 24; ++hour) {
		bool ok = (config.hour == -1 || config.hour == cur.hour);
		if (ok && test_hour(cur, config)) {
			good_moment = cur;
			return true;
		}
		cur.next_hour();
	}
	return false;
}

bool test_month(Moment& good_moment, const Moment& config) {
	const int days = get_days_in_month(good_moment.date.month, good_moment.date.year);
	Moment cur = good_moment;
	for (int day = good_moment.date.day; day < days; ++day) {
		bool ok = false;
		if (config.date.day == -1) {
			ok |= (config.date.weekday == -1 || config.date.weekday == cur.date.weekday);
		} else {
			if (config.date.weekday != -1) {
				ok |= (config.date.day == cur.date.day || config.date.weekday == cur.date.weekday);
			} else {
				ok |= (config.date.day == cur.date.day);
			}
		}
		if (ok && test_day(cur, config)) {
			good_moment = cur;
			return true;
		}
		cur.next_day();
	}
	return false;
}

void solve(std::istream& in, std::ostream& out) {
	Moment config;
	in >> config >> __;
	for (int _ : range(__)) {
		ll time_passed;
		in >> time_passed;
		const Moment current_moment = parser::parse_moment(time_passed);
		// const ll encoded = encoder::encode_moment(current_moment);
		// out << boolalpha << current_moment << " " << encoded << " " << (encoded == time_passed) << endl;
		Moment good_moment = current_moment;
		good_moment.next_second();

		if (config.date.month != -1 && config.date.month != good_moment.date.month) {
			good_moment.set_month(config.date.month);
		}

		while (!test_month(good_moment, config)) {
			if (config.date.month != -1) {
				good_moment.set_month(config.date.month);
			}
			else {
				good_moment.next_month();
			}
		}

		const ll ans = encoder::encode_moment(good_moment);
		out << ans << endl;
	}
}

#include <fstream>

int main() {
    srand(time(NULL));

    ios_base::sync_with_stdio(0);
    cin.tie(0);

    istream& in = cin;

    ostream& out = cout;

    out << fixed << setprecision(16);
    solve(in, out);
    return 0;
}


#pragma comment(linker, "/STACK:66777216")
#pragma warning(disable : 4996)

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#pragma hdrstop

static constexpr int kDayMonth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

#include <cstdint>

#ifdef _MSC_VER
#include <intrin.h>

#else
#define popcount(a) __builtin_popcount(a)
#define clz(a) __builtin_clz(a)
#define ctz(a) __builtin_ctz(a)
#endif

#ifdef _MSC_VER

#endif

template<typename T, size_t N>
struct MakeVector {

};

template<typename T>
struct MakeVector<T, 1> {
	/// caide keep
	template<typename R = std::vector<T>>
	static R make_vector(std::size_t size, const T& value) {
		return R(size, value);
	}
};

#ifdef _MSC_VER

#else
#define LLD "%lld"
#define LLU "%llu"
#endif

#define ll long long

#include <iterator>

template<typename T>
class IntegerIterator : public std::iterator<std::input_iterator_tag, T, std::ptrdiff_t, T*, T> {
public:
	using value_type = T;

	constexpr explicit IntegerIterator(const value_type value) : value_(value) {}

	IntegerIterator& operator++() {
		++value_;
		return *this;
	}

	constexpr value_type operator*() const {
		return value_;
	}

	constexpr bool operator ==(const IntegerIterator rhs) const {
		return value_ == rhs.value_;
	}

	constexpr bool operator !=(const IntegerIterator rhs) const {
		return !(*this == rhs);
	}

private:
	value_type value_;

};

template<typename T>
class IntegerRange {
public:
	using const_iterator = IntegerIterator<T>;

	constexpr IntegerRange(const T begin, const T end) : begin_(begin), end_(end) {}

	constexpr const_iterator begin() const {
		return const_iterator(begin_);
	}

	constexpr const_iterator end() const {
		return const_iterator(end_);
	}

private:
	T begin_;
	T end_;

};

template<typename T>
constexpr IntegerRange<T> range(const T to) {
	return IntegerRange<T>(0, to);
}

template<typename T>
inline bool is_leap_year(const T year) {
	return year % 400 == 0 || (year % 100 != 0 && (year & 3) == 0);
}

constexpr const int BASE_MOD = 1000000007;  /// caide keep

template<typename T>
inline T& add_mod(T& a, const T b, const T mod = BASE_MOD) {
	if ((a += b) >= mod) {
		a -= mod;
	}
	return a;
}

template<typename T, T MOD = BASE_MOD>
class ModInt {
public:

	/// caide keep
	constexpr ModInt() : ModInt(0) {}

private:
	T value_;

};

using namespace std;

int __;

struct Date {
	int year;
	int month;
	int day;
	int weekday;

	Date() : year(1970), month(0), day(0), weekday(3) {}

	bool is_leap() const {
		return is_leap_year(year);
	}

};

inline int get_days_in_month(const int month, const int year) {
	const int days = kDayMonth[month] + (month == 1 && is_leap_year(year) ? 1 : 0);
	return days;
}

struct Moment {
	Date date;
	int hour;
	int minute;
	int second;

	Moment() : date(), hour(0), minute(0), second(0) {}

	friend istream& operator >>(istream& in, Moment& rhs) {
		in >> rhs.second >> rhs.minute >> rhs.hour >> rhs.date.weekday >> rhs.date.day >> rhs.date.month;
		if (rhs.date.weekday != -1) {
			--rhs.date.weekday;
		}
		if (rhs.date.day != -1) {
			--rhs.date.day;
		}
		if (rhs.date.month != -1) {
			--rhs.date.month;
		}
		return in;
	}

	void next_month() {
		const int days_rem = get_days_in_month(date.month, date.year);
		add_mod(date.weekday, days_rem % 7, 7);
		++date.month;
		if (date.month == 12) {
			date.month = 0;
			++date.year;
		}
		date.day = 0;
		hour = 0;
		minute = 0;
		second = 0;
	}

	void next_day() {
		const int days = get_days_in_month(date.month, date.year);
		++date.day;
		add_mod(date.weekday, 1, 7);
		hour = 0;
		minute = 0;
		second = 0;
		if (date.day == days) {
			next_month();
		}
	}

	void next_hour() {
		++hour;
		minute = 0;
		second = 0;
		if (hour == 24) {
			next_day();
		}
	}

	void next_minute() {
		++minute;
		second = 0;
		if (minute == 60) {
			next_hour();
		}
	}

	void next_second() {
		++second;
		if (second == 60) {
			next_minute();
		}
	}

	void set_month(const int month) {
		bool first = true;
		while (date.month != month || first) {
			first = false;
			next_month();
		}
	}

};

namespace parser {

inline void parse_year(Date& date, ll& time_passed) {
	while (true) {
		const int days = 365 + (date.is_leap() ? 1 : 0);
		const int seconds = days * 86400;
		if (time_passed < seconds) {
			break;
		}
		time_passed -= seconds;
		++date.year;
		add_mod(date.weekday, days % 7, 7);
	}
}

inline void parse_month(Date& date, ll& time_passed) {
	for (size_t month = 0; month < 12; ++month) {
		const int days = get_days_in_month(month, date.year);// kDayMonth[i] + (i == 1 && date.is_leap() ? 1 : 0);
		const int seconds = days * 86400;
		if (time_passed < seconds) {
			break;
		}
		time_passed -= seconds;
		++date.month;
		add_mod(date.weekday, days % 7, 7);
	}
}

inline void parse_day(Date& date, ll& time_passed) {
	const int seconds = 86400;
	date.day = time_passed / seconds;
	add_mod(date.weekday, date.day % 7, 7);
	time_passed %= seconds;
}

inline Date parse_date(ll& time_passed) {
	Date date;
	parse_year(date, time_passed);
	parse_month(date, time_passed);
	parse_day(date, time_passed);
	return date;
}

inline int parse_hour(ll& time_passed) {
	const int seconds = 3600;
	const int hour = time_passed / seconds;
	time_passed %= seconds;
	return hour;
}

inline int parse_minute(ll& time_passed) {
	const int seconds = 60;
	const int minute = time_passed / seconds;
	time_passed %= seconds;
	return minute;
}

inline int parse_second(ll& time_passed) {
	const int second = time_passed;
	time_passed = 0;
	return second;
}

inline Moment parse_moment(ll time_passed) {
	Moment moment;
	moment.date = parse_date(time_passed);
	moment.hour = parse_hour(time_passed);
	moment.minute = parse_minute(time_passed);
	moment.second = parse_second(time_passed);
	return moment;
}

}  // namespace parser

namespace encoder {

inline ll encode_year(const Date& date) {
	ll time = 0;
	for (int year = 1970; year < date.year; ++year) {
		const int days = 365 + (is_leap_year(year) ? 1 : 0);
		const int seconds = days * 86400;
		time += seconds;
	}
	return time;
}

inline ll encode_month(const Date& date) {
	ll time = 0;
	for (int month = 0; month < date.month; ++month) {
		const int days = get_days_in_month(month, date.year); // kDayMonth[month] + (month == 1 && date.is_leap() ? 1 : 0);
		const int seconds = days * 86400;
		time += seconds;
	}
	return time;
}

inline ll encode_day(const Date& date) {
	const int seconds = 86400;
	return date.day * seconds;
}

inline ll encode_date(const Date& date) {
	ll time = 0;
	time += encode_year(date);
	time += encode_month(date);
	time += encode_day(date);
	return time;
}

inline ll encode_hour(const int hour) {
	const int seconds = 3600;
	return hour * seconds;
}

inline ll encode_minute(const int minute) {
	const int seconds = 60;
	return minute * seconds;
}

inline ll encode_second(const int second) {
	return second;
}

inline ll encode_moment(const Moment& moment) {
	ll time = 0;
	time += encode_date(moment.date);
	time += encode_hour(moment.hour);
	time += encode_minute(moment.minute);
	time += encode_second(moment.second);
	return time;
}

}  // namespace encoder

bool test_minute(Moment& good_moment, const Moment& config) {
	Moment cur = good_moment;
	for (int second = cur.second; second < 60; ++second) {
		bool ok = (config.second == -1 || config.second == cur.second);
		if (ok) {
			good_moment = cur;
			return true;
		}
		cur.next_second();
	}
	return false;
}

bool test_hour(Moment& good_moment, const Moment& config) {
	Moment cur = good_moment;
	for (int minute = cur.minute; minute < 60; ++minute) {
		bool ok = (config.minute == -1 || config.minute == cur.minute);
		if (ok && test_minute(cur, config)) {
			good_moment = cur;
			return true;
		}
		cur.next_minute();
	}
	return false;
}

bool test_day(Moment& good_moment, const Moment& config) {
	Moment cur = good_moment;
	for (int hour = cur.hour; hour < 24; ++hour) {
		bool ok = (config.hour == -1 || config.hour == cur.hour);
		if (ok && test_hour(cur, config)) {
			good_moment = cur;
			return true;
		}
		cur.next_hour();
	}
	return false;
}

bool test_month(Moment& good_moment, const Moment& config) {
	const int days = get_days_in_month(good_moment.date.month, good_moment.date.year);
	Moment cur = good_moment;
	for (int day = good_moment.date.day; day < days; ++day) {
		bool ok = false;
		if (config.date.day == -1) {
			ok |= (config.date.weekday == -1 || config.date.weekday == cur.date.weekday);
		} else {
			if (config.date.weekday != -1) {
				ok |= (config.date.day == cur.date.day || config.date.weekday == cur.date.weekday);
			} else {
				ok |= (config.date.day == cur.date.day);
			}
		}
		if (ok && test_day(cur, config)) {
			good_moment = cur;
			return true;
		}
		cur.next_day();
	}
	return false;
}

void solve(std::istream& in, std::ostream& out) {
	Moment config;
	in >> config >> __;
	for (int _ : range(__)) {
		ll time_passed;
		in >> time_passed;
		const Moment current_moment = parser::parse_moment(time_passed);
		// const ll encoded = encoder::encode_moment(current_moment);
		// out << boolalpha << current_moment << " " << encoded << " " << (encoded == time_passed) << endl;
		Moment good_moment = current_moment;
		good_moment.next_second();

		if (config.date.month != -1 && config.date.month < good_moment.date.month) {
			good_moment.set_month(config.date.month);
		}

		if (!test_month(good_moment, config)) {
			if (config.date.month != -1) {
				good_moment.set_month(config.date.month);
			}
			else {
				good_moment.next_month();
			}
			test_month(good_moment, config);
		}

		const ll ans = encoder::encode_moment(good_moment);
		out << ans << endl;
	}
}

#include <fstream>

int main() {
    srand(time(NULL));

    ios_base::sync_with_stdio(0);
    cin.tie(0);

    istream& in = cin;

    ostream& out = cout;

    out << fixed << setprecision(16);
    solve(in, out);
    return 0;
}


#pragma comment(linker, "/STACK:66777216")
#pragma warning(disable : 4996)

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#pragma hdrstop

static constexpr int kDayMonth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

#include <cstdint>

#ifdef _MSC_VER
#include <intrin.h>

#else
#define popcount(a) __builtin_popcount(a)
#define clz(a) __builtin_clz(a)
#define ctz(a) __builtin_ctz(a)
#endif

#ifdef _MSC_VER

#endif

template<typename T, size_t N>
struct MakeVector {

};

template<typename T>
struct MakeVector<T, 1> {
	/// caide keep
	template<typename R = std::vector<T>>
	static R make_vector(std::size_t size, const T& value) {
		return R(size, value);
	}
};

#ifdef _MSC_VER

#else
#define LLD "%lld"
#define LLU "%llu"
#endif

#define ll long long

#include <iterator>

template<typename T>
class IntegerIterator : public std::iterator<std::input_iterator_tag, T, std::ptrdiff_t, T*, T> {
public:
	using value_type = T;

	constexpr explicit IntegerIterator(const value_type value) : value_(value) {}

	IntegerIterator& operator++() {
		++value_;
		return *this;
	}

	constexpr value_type operator*() const {
		return value_;
	}

	constexpr bool operator ==(const IntegerIterator rhs) const {
		return value_ == rhs.value_;
	}

	constexpr bool operator !=(const IntegerIterator rhs) const {
		return !(*this == rhs);
	}

private:
	value_type value_;

};

template<typename T>
class IntegerRange {
public:
	using const_iterator = IntegerIterator<T>;

	constexpr IntegerRange(const T begin, const T end) : begin_(begin), end_(end) {}

	constexpr const_iterator begin() const {
		return const_iterator(begin_);
	}

	constexpr const_iterator end() const {
		return const_iterator(end_);
	}

private:
	T begin_;
	T end_;

};

template<typename T>
constexpr IntegerRange<T> range(const T to) {
	return IntegerRange<T>(0, to);
}

template<typename T>
inline bool is_leap_year(const T year) {
	return year % 400 == 0 || (year % 100 != 0 && (year & 3) == 0);
}

constexpr const int BASE_MOD = 1000000007;  /// caide keep

template<typename T>
inline T& add_mod(T& a, const T b, const T mod = BASE_MOD) {
	if ((a += b) >= mod) {
		a -= mod;
	}
	return a;
}

template<typename T, T MOD = BASE_MOD>
class ModInt {
public:

	/// caide keep
	constexpr ModInt() : ModInt(0) {}

private:
	T value_;

};

using namespace std;

int __;

struct Date {
	int year;
	int month;
	int day;
	int weekday;

	Date() : year(1970), month(0), day(0), weekday(3) {}

	bool is_leap() const {
		return is_leap_year(year);
	}

};

inline int get_days_in_month(const int month, const int year) {
	const int days = kDayMonth[month] + (month == 1 && is_leap_year(year) ? 1 : 0);
	return days;
}

struct Moment {
	Date date;
	int hour;
	int minute;
	int second;

	Moment() : date(), hour(0), minute(0), second(0) {}

	friend istream& operator >>(istream& in, Moment& rhs) {
		in >> rhs.second >> rhs.minute >> rhs.hour >> rhs.date.weekday >> rhs.date.day >> rhs.date.month;
		if (rhs.date.weekday != -1) {
			--rhs.date.weekday;
		}
		if (rhs.date.day != -1) {
			--rhs.date.day;
		}
		if (rhs.date.month != -1) {
			--rhs.date.month;
		}
		return in;
	}

	void next_month() {
		const int days_rem = get_days_in_month(date.month, date.year);
		add_mod(date.weekday, days_rem % 7, 7);
		++date.month;
		if (date.month == 12) {
			date.month = 0;
			++date.year;
		}
		date.day = 0;
		hour = 0;
		minute = 0;
		second = 0;
	}

	void next_day() {
		const int days = get_days_in_month(date.month, date.year);
		++date.day;
		add_mod(date.weekday, 1, 7);
		hour = 0;
		minute = 0;
		second = 0;
		if (date.day == days) {
			next_month();
		}
	}

	void next_hour() {
		++hour;
		minute = 0;
		second = 0;
		if (hour == 24) {
			next_day();
		}
	}

	void next_minute() {
		++minute;
		second = 0;
		if (minute == 60) {
			next_hour();
		}
	}

	void next_second() {
		++second;
		if (second == 60) {
			next_minute();
		}
	}

	void set_month(const int month) {
		bool first = true;
		while (date.month != month || first) {
			first = false;
			next_month();
		}
	}

};

namespace parser {

inline void parse_year(Date& date, ll& time_passed) {
	while (true) {
		const int days = 365 + (date.is_leap() ? 1 : 0);
		const int seconds = days * 86400;
		if (time_passed < seconds) {
			break;
		}
		time_passed -= seconds;
		++date.year;
		add_mod(date.weekday, days % 7, 7);
	}
}

inline void parse_month(Date& date, ll& time_passed) {
	for (size_t month = 0; month < 12; ++month) {
		const int days = get_days_in_month(month, date.year);// kDayMonth[i] + (i == 1 && date.is_leap() ? 1 : 0);
		const int seconds = days * 86400;
		if (time_passed < seconds) {
			break;
		}
		time_passed -= seconds;
		++date.month;
		add_mod(date.weekday, days % 7, 7);
	}
}

inline void parse_day(Date& date, ll& time_passed) {
	const int seconds = 86400;
	date.day = time_passed / seconds;
	add_mod(date.weekday, date.day % 7, 7);
	time_passed %= seconds;
}

inline Date parse_date(ll& time_passed) {
	Date date;
	parse_year(date, time_passed);
	parse_month(date, time_passed);
	parse_day(date, time_passed);
	return date;
}

inline int parse_hour(ll& time_passed) {
	const int seconds = 3600;
	const int hour = time_passed / seconds;
	time_passed %= seconds;
	return hour;
}

inline int parse_minute(ll& time_passed) {
	const int seconds = 60;
	const int minute = time_passed / seconds;
	time_passed %= seconds;
	return minute;
}

inline int parse_second(ll& time_passed) {
	const int second = time_passed;
	time_passed = 0;
	return second;
}

inline Moment parse_moment(ll time_passed) {
	Moment moment;
	moment.date = parse_date(time_passed);
	moment.hour = parse_hour(time_passed);
	moment.minute = parse_minute(time_passed);
	moment.second = parse_second(time_passed);
	return moment;
}

}  // namespace parser

namespace encoder {

inline ll encode_year(const Date& date) {
	ll time = 0;
	for (int year = 1970; year < date.year; ++year) {
		const int days = 365 + (is_leap_year(year) ? 1 : 0);
		const int seconds = days * 86400;
		time += seconds;
	}
	return time;
}

inline ll encode_month(const Date& date) {
	ll time = 0;
	for (int month = 0; month < date.month; ++month) {
		const int days = get_days_in_month(month, date.year); // kDayMonth[month] + (month == 1 && date.is_leap() ? 1 : 0);
		const int seconds = days * 86400;
		time += seconds;
	}
	return time;
}

inline ll encode_day(const Date& date) {
	const int seconds = 86400;
	return date.day * seconds;
}

inline ll encode_date(const Date& date) {
	ll time = 0;
	time += encode_year(date);
	time += encode_month(date);
	time += encode_day(date);
	return time;
}

inline ll encode_hour(const int hour) {
	const int seconds = 3600;
	return hour * seconds;
}

inline ll encode_minute(const int minute) {
	const int seconds = 60;
	return minute * seconds;
}

inline ll encode_second(const int second) {
	return second;
}

inline ll encode_moment(const Moment& moment) {
	ll time = 0;
	time += encode_date(moment.date);
	time += encode_hour(moment.hour);
	time += encode_minute(moment.minute);
	time += encode_second(moment.second);
	return time;
}

}  // namespace encoder

bool test_minute(Moment& good_moment, const Moment& config) {
	Moment cur = good_moment;
	for (int second = cur.second; second < 60; ++second) {
		bool ok = (config.second == -1 || config.second == cur.second);
		if (ok) {
			good_moment = cur;
			return true;
		}
		cur.next_second();
	}
	return false;
}

bool test_hour(Moment& good_moment, const Moment& config) {
	Moment cur = good_moment;
	for (int minute = cur.minute; minute < 60; ++minute) {
		bool ok = (config.minute == -1 || config.minute == cur.minute);
		if (ok && test_minute(cur, config)) {
			good_moment = cur;
			return true;
		}
		cur.next_minute();
	}
	return false;
}

bool test_day(Moment& good_moment, const Moment& config) {
	Moment cur = good_moment;
	for (int hour = cur.hour; hour < 24; ++hour) {
		bool ok = (config.hour == -1 || config.hour == cur.hour);
		if (ok && test_hour(cur, config)) {
			good_moment = cur;
			return true;
		}
		cur.next_hour();
	}
	return false;
}

bool test_month(Moment& good_moment, const Moment& config) {
	const int days = get_days_in_month(good_moment.date.month, good_moment.date.year);
	Moment cur = good_moment;
	for (int day = good_moment.date.day; day < days; ++day) {
		bool ok = false;
		if (config.date.day == -1) {
			ok |= (config.date.weekday == -1 || config.date.weekday == cur.date.weekday);
		} else {
			if (config.date.weekday != -1) {
				ok |= (config.date.day == cur.date.day || config.date.weekday == cur.date.weekday);
			} else {
				ok |= (config.date.day == cur.date.day);
			}
		}
		if (ok && test_day(cur, config)) {
			good_moment = cur;
			return true;
		}
		cur.next_day();
	}
	return false;
}

void solve(std::istream& in, std::ostream& out) {
	Moment config;
	in >> config >> __;
	for (int _ : range(__)) {
		ll time_passed;
		in >> time_passed;
		const Moment current_moment = parser::parse_moment(time_passed);
		// const ll encoded = encoder::encode_moment(current_moment);
		// out << boolalpha << current_moment << " " << encoded << " " << (encoded == time_passed) << endl;
		Moment good_moment = current_moment;
		good_moment.next_second();

		if (config.date.month != -1 && config.date.month < good_moment.date.month) {
			good_moment.set_month(config.date.month);
		}

		while (!test_month(good_moment, config)) {
			if (config.date.month != -1) {
				good_moment.set_month(config.date.month);
			}
			else {
				good_moment.next_month();
			}
		}

		const ll ans = encoder::encode_moment(good_moment);
		out << ans << endl;
	}
}

#include <fstream>

int main() {
    srand(time(NULL));

    ios_base::sync_with_stdio(0);
    cin.tie(0);

    istream& in = cin;

    ostream& out = cout;

    out << fixed << setprecision(16);
    solve(in, out);
    return 0;
}


#include <algorithm>
#include <iostream>
#include <cstdio>
#include <vector>
#define pb push_back
using namespace std;
typedef long long ll;
int month_day[][12] =
{
	{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
	{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
#ifdef ONLINE_JUDGE
#define read_ll(x) do {scanf("%I64d", &x); } while (false)
#define print_ll(x)  do {printf("%I64d\n", x); } while (false)
#else
#define read_ll(x) do {scanf("%lld", &x); } while (false)
#define print_ll(x) do {printf("%lld\n", x); } while (false)
#endif
vector<ll> ok1, ok2;
int main()
{
	freopen("698E.in", "r", stdin);
	int s, m, h, day, date, month;
	scanf("%d%d%d%d%d%d", &s, &m, &h, &day, &date, &month);
	if (day != -1)
		day--;
	if (date != -1)
		date--;
	if (month != -1)
		month--;
	ll x1 = 0;
	for (int h2 = 0; h2 < 24; h2++)
	{
		for (int m2 = 0; m2 < 60; m2++)
		{
			for (int s2 = 0; s2 < 60; s2++, x1++)
			{
				if ((s == -1 || s == s2) && (m == -1 || m == m2) && (h == -1 || h == h2))
					ok1.pb(x1);
			}
		}
	}
	// cout << "ok" << endl;
	int day2 = 3;
	ll x2 = 0;
	for (int year = 1970; year <= 2500; year++)
	{
		bool leap = !(year % 400) || ((year % 100) && !(year % 4));
		for (int month2 = 0; month2 < 12; month2++)
		{
			for (int date2 = 0; date2 < month_day[leap][month2]; date2++, x2++)
			{
				if (-1 == month || month == month2)
				{
					if ((date != -1 && date == date2) || (day != -1 && day == day2) || (date == -1 && day == -1))
						ok2.pb(x2);
				}
				day2 = (day2 + 1) % 7;
			}
		}
	}
	// cout << "ok" << endl;
	int n;
	scanf("%d", &n);
	while (n--)
	{
		ll x;
		read_ll(x);
		ll whole_year = x / x1 / x2;
		ll remain_time = x % (x1 * x2);
		ll whole_day = remain_time / x1;
		remain_time %= x1;
		// print_ll(remain_time);
		// printf("%d\n", binary_search(ok2.begin(), ok2.end(), whole_day));
		// print_ll(ok1.back());
		if (whole_day > ok2.back() || (whole_day == ok2.back() && remain_time >= ok1.back()))
		{
			cout << "case 1" << endl;
			print_ll((whole_year + 1) * x1 * x2 + ok2[0] * x1 + ok1[0]);
		}
		else if (remain_time >= ok1.back() || !binary_search(ok2.begin(), ok2.end(), whole_day))
		{
			cout << "case 2" << endl;
			print_ll(whole_year * x1 * x2 + *upper_bound(ok2.begin(), ok2.end(), whole_day) * x1 + ok1[0]);
		}
		else
		{
			cout << "case 3" << endl;
			print_ll(whole_year * x1 * x2 + whole_day * x1 + *upper_bound(ok1.begin(), ok1.end(), remain_time));
		}
	}
	return 0;
}


#include <algorithm>
#include <iostream>
#include <cstdio>
#include <vector>
#define pb push_back
using namespace std;
typedef long long ll;
int month_day[][12] =
{
	{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
	{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
#ifdef ONLINE_JUDGE
#define read_ll(x) do {scanf("%I64d", &x); } while (false)
#define print_ll(x)  do {printf("%I64d\n", x); } while (false)
#else
#define read_ll(x) do {scanf("%lld", &x); } while (false)
#define print_ll(x) do {printf("%lld\n", x); } while (false)
#endif
vector<ll> ok1, ok2;
int main()
{
	// freopen("698E.in", "r", stdin);
	int s, m, h, day, date, month;
	scanf("%d%d%d%d%d%d", &s, &m, &h, &day, &date, &month);
	if (day != -1)
		day--;
	if (date != -1)
		date--;
	if (month != -1)
		month--;
	ll x1 = 0;
	for (int h2 = 0; h2 < 24; h2++)
	{
		for (int m2 = 0; m2 < 60; m2++)
		{
			for (int s2 = 0; s2 < 60; s2++, x1++)
			{
				if ((s == -1 || s == s2) && (m == -1 || m == m2) && (h == -1 || h == h2))
					ok1.pb(x1);
			}
		}
	}
	// cout << "ok" << endl;
	int day2 = 3;
	ll x2 = 0;
	for (int year = 1970; year <= 3000; year++)
	{
		bool leap = !(year % 400) || ((year % 100) && !(year % 4));
		for (int month2 = 0; month2 < 12; month2++)
		{
			for (int date2 = 0; date2 < month_day[leap][month2]; date2++, x2++)
			{
				if (-1 == month || month == month2)
				{
					if ((date != -1 && date == date2) || (day != -1 && day == day2) || (date == -1 && day == -1))
						ok2.pb(x2);
				}
				day2 = (day2 + 1) % 7;
			}
		}
	}
	// cout << "ok" << endl;
	int n;
	scanf("%d", &n);
	while (n--)
	{
		ll x;
		read_ll(x);
		ll whole_year = x / x1 / x2;
		ll remain_time = x % (x1 * x2);
		ll whole_day = remain_time / x1;
		remain_time %= x1;
		// print_ll(remain_time);
		// printf("%d\n", binary_search(ok2.begin(), ok2.end(), whole_day));
		// print_ll(ok1.back());
		if (whole_day > ok2.back() || (whole_day == ok2.back() && remain_time >= ok1.back()))
			print_ll((whole_year + 1) * x1 * x2 + ok2[0] * x1 + ok1[0]);
		else if (remain_time >= ok1.back() || !binary_search(ok2.begin(), ok2.end(), whole_day))
			print_ll(whole_year * x1 * x2 + *upper_bound(ok2.begin(), ok2.end(), whole_day) * x1 + ok1[0]);
		else
			print_ll(whole_year * x1 * x2 + whole_day * x1 + *upper_bound(ok1.begin(), ok1.end(), remain_time));
	}
	return 0;
}