#include <bits/stdc++.h>
using namespace std;
const long long Time = 12622780800ll;
int i, j, k, n, ch, s, m, h, day, date, month, Fg;
long long t;
struct cc {
  int s, m, h, day, date, month, year;
  int Js() {
    if (month == 4 || month == 6 || month == 9 || month == 11) return 30;
    if (month != 2) return 31;
    if (year % 400 == 0 || (year % 100 && year % 4 == 0)) return 29;
    return 28;
  }
  int count() {
    if (year % 400 == 0 || (year % 100 && year % 4 == 0)) return 366;
    return 365;
  }
  void work() {
    if (s == 60) {
      s = 0;
      m++;
    }
    if (m == 60) {
      h++;
      m = 0;
    }
    if (h == 24) {
      date++;
      day = day % 7 + 1;
      h = 0;
    }
    if (date == Js() + 1) {
      month++;
      date = 1;
    }
    if (month == 13) {
      year++;
      month = 1;
    }
  }
  void operator+=(const long long &ad) {
    long long a = ad;
    while (s && a) {
      a--;
      if (++s == 60) work();
    }
    while (m && a >= 60) {
      a -= 60;
      if (++m == 60) work();
    }
    while (h && a >= 3600) {
      a -= 3600;
      if (++h == 24) work();
    }
    while (date != 1 && a >= 86400) {
      a -= 86400;
      day = day % 7 + 1;
      if (++date == Js() + 1) work();
    }
    while (month != 1 && a >= Js() * 86400) {
      a -= Js() * 86400;
      day = (day + Js() - 1) % 7 + 1;
      if (++month == 13) work();
    }
    while (year % 400 && a >= count() * 86400) {
      a -= count() * 86400;
      day = (day + count() - 1) % 7 + 1;
      year++;
    }
    year += (a / Time) * 400;
    a %= Time;
    while (a >= count() * 86400) {
      a -= count() * 86400;
      day = (day + count() - 1) % 7 + 1;
      year++;
    }
    while (a >= Js() * 86400) {
      a -= Js() * 86400;
      day = (day + Js() - 1) % 7 + 1;
      if (++month == 13) work();
    }
    while (a >= 86400) {
      a -= 86400;
      day = day % 7 + 1;
      if (++date == Js() + 1) work();
    }
    while (a >= 3600) {
      a -= 3600;
      if (++h == 24) work();
    }
    while (a >= 60) {
      a -= 60;
      if (++m == 60) work();
    }
    while (a) {
      a--;
      if (++s == 60) work();
    }
  }
} a;
void R(long long &x) {
  x = 0;
  ch = getchar();
  while (ch < '0' || '9' < ch) ch = getchar();
  while ('0' <= ch && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
}
void W(long long x) {
  if (x >= 10) W(x / 10);
  putchar(x % 10 + '0');
}
bool check() {
  if (month == -1 || month == a.month)
    if ((day == -1 && date == -1) || (day == a.day || date == a.date))
      if (h == -1 || h == a.h)
        if (m == -1 || m == a.m)
          if (s == -1 || s == a.s) return 1;
  return 0;
}
int main() {
  scanf("%d%d%d%d%d%d", &s, &m, &h, &day, &date, &month);
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    R(t);
    t++;
    Fg = 0;
    a.s = a.m = a.h = 0;
    a.day = 4;
    a.date = a.month = 1;
    a.year = 1970;
    a += t;
    while (a.s && !check()) {
      t++;
      if (++a.s == 60) a.work();
    }
    while (s != -1 && a.s != s) {
      t++;
      a.s++;
    }
    while (a.m && !check()) {
      t += 60;
      if (++a.m == 60) a.work();
    }
    while (m != -1 && a.m != m) {
      t += 60;
      a.m++;
    }
    while (a.h && !check()) {
      t += 3600;
      if (++a.h == 24) a.work();
    }
    while (h != -1 && a.h != h) {
      t += 3600;
      a.h++;
    }
    while (!check()) {
      t += 86400;
      a.day = a.day % 7 + 1;
      a.date++;
      a.work();
    }
    W(t);
    puts("");
  }
}
