#include <bits/stdc++.h>
using namespace std;
int mtot[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool is_leap(int y) {
  if (y % 4 != 0) return false;
  if (y % 100 == 0) return y % 400 == 0;
  return true;
}
struct Time {
  int s, m, h;
  int dw, dt, mn, y;
  long long ts;
  int c;
  void setPattern(int s_, int m_, int h_, int dw_, int dt_, int mn_) {
    s = s_, m = m_, h = h_;
    dw = dw_, dt = dt_, mn = mn_;
  }
  void increase_day(bool upd = true) {
    if (upd) {
      dw++;
      if (dw == 8) dw = 1;
    }
    if (is_leap(y)) mtot[2]++;
    if (dt == mtot[mn]) {
      dt = 1, mn++;
    } else
      dt++;
    if (is_leap(y)) mtot[2]--;
    if (mn > 12) mn = 1, y++;
    ts += 24 * 60 * 60;
  }
  void increase_sec() {
    s++;
    if (s == 60) s = 0, m++;
    if (m == 60) m = 0, h++;
    if (h == 24) h = 0, increase_day(), ts -= 24 * 60 * 60;
    ts++;
  }
  bool match_aux(const Time &pat) {
    if (pat.mn != -1 && pat.mn != mn) return false;
    if (pat.dw != -1 && pat.dt != -1) {
      return (pat.dw == dw || pat.dt == dt);
    }
    bool m1 = (pat.dw == -1 || pat.dw == dw);
    bool m2 = (pat.dt == -1 || pat.dt == dt);
    return m1 && m2;
  }
  bool match_time_aux(const Time &pat) {
    if (pat.h != -1 && pat.h != h) return false;
    if (pat.m != -1 && pat.m != m) return false;
    if (pat.s != -1 && pat.s != s) return false;
    return true;
  }
  bool match(const Time &pat) {
    while (!match_time_aux(pat)) increase_sec(), c++;
    while (!match_aux(pat)) increase_day(), c++;
    if (c == 0) {
      increase_sec();
      while (!match_time_aux(pat)) increase_sec(), c++;
      while (!match_aux(pat)) increase_day(), c++;
    }
    return true;
  }
  void parse(long long unixTS) {
    c = 0;
    long long temp = unixTS;
    s = unixTS % 60;
    unixTS /= 60;
    m = unixTS % 60;
    unixTS /= 60;
    h = unixTS % 24;
    unixTS /= 24;
    dw = (unixTS + 3) % 7 + 1;
    if (dw >= 8) dw -= 7;
    y = 1970;
    while (unixTS > 366) {
      if (is_leap(y)) unixTS--;
      unixTS -= 365;
      y++;
    }
    dt = 1;
    mn = 1;
    while (unixTS--) increase_day(false);
    ts = temp;
  }
  long long invert() { return ts; }
  void print() {
    return;
    fprintf(stderr, "> %02d-%02d-%4d (%d) %02d:%02d:%02d\n", dt, mn, y, dw, h,
            m, s);
  }
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int s, m, h, dw, dt, mn;
  cin >> s >> m >> h >> dw >> dt >> mn;
  Time pat;
  pat.setPattern(s, m, h, dw, dt, mn);
  int n;
  cin >> n;
  while (n--) {
    long long val;
    cin >> val;
    Time ans;
    ans.parse(val);
    ans.print();
    ans.match(pat);
    long long ret = ans.invert();
    ans.print();
    ans.parse(val);
    ans.increase_day();
    ans.ts -= (3600 * ans.h + 60 * ans.m + ans.s);
    ans.h = ans.s = ans.m = 0;
    ans.c++;
    ans.match(pat);
    ret = min(ret, ans.invert());
    cout << ret << '\n';
  }
  return 0;
}
