#include <bits/stdc++.h>
using namespace std;
struct date {
  long long int s, m, h, d, mon, y;
  date() {}
  date(long long int s, long long int m, long long int h, long long int d,
       long long int mon, long long int y)
      : s(s), m(m), h(h), d(d), mon(mon), y(y) {}
};
void escribe(date dat) {
  cout << "(" << dat.s << "," << dat.m << "," << dat.h << "," << dat.d << ","
       << dat.mon << "," << dat.y << ")";
  cout << endl;
}
long long int leapsbefore(long long int y) {
  y--;
  return y / 4 - y / 100 + y / 400;
}
long long int leapssince1970(long long int y) {
  return leapsbefore(y) - leapsbefore(1969);
}
bool isleap(long long int y) {
  return y % 400 == 0 or (y % 100 != 0 and y % 4 == 0);
}
long long int daysbefore(long long int y) {
  return 365 * (y - 1970) + leapssince1970(y);
}
const long long int secondsperday = 86400;
long long int ll2y(long long int x) {
  long long int d = x / secondsperday;
  long long int inf = 1970;
  long long int sup = 10000000000LL;
  while (inf + 1 < sup) {
    long long int med = (inf + sup) / 2;
    if (daysbefore(med) <= d)
      inf = med;
    else
      sup = med;
  }
  return inf;
}
int m2d[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
long long int daysthismonth(long long int mon, long long int y) {
  if (mon == 1 and isleap(y)) return 29;
  return m2d[mon];
}
long long int daysthismonth(date dat) { return daysthismonth(dat.mon, dat.y); }
long long int daysbefore(long long int mon, long long int y) {
  long long int d = 0;
  for (int i = 0; i < mon; i++) d += daysthismonth(i, y);
  return d;
}
long long int daysbefore(date dat) {
  return daysbefore(dat.y) + daysbefore(dat.mon, dat.y) + dat.d;
}
long long int dayofweek(date dat) { return (3 + daysbefore(dat)) % 7; }
date ll2date(long long int x) {
  long long int y = ll2y(x);
  x -= daysbefore(y) * secondsperday;
  long long int mon = 0;
  while (mon < 12 and daysbefore(mon + 1, y) * secondsperday <= x) mon++;
  x -= daysbefore(mon, y) * secondsperday;
  return date(x % 60, x / 60 % 60, x / 3600 % 24, x / 86400, mon, y);
}
long long int date2ll(date dat) {
  long long int d = daysbefore(dat.y) + daysbefore(dat.mon, dat.y) + dat.d;
  return d * secondsperday + dat.s + dat.m * 60 + dat.h * 3600;
}
date nextyear(date dat) {
  dat.y++;
  dat.s = dat.m = dat.h = dat.d = dat.mon = 0;
  return dat;
}
date nextmonth(date dat) {
  if (dat.mon == 11) return nextyear(dat);
  dat.mon++;
  dat.s = dat.m = dat.h = dat.d = 0;
  return dat;
}
date nextday(date dat) {
  if (dat.d + 1 == daysthismonth(dat)) return nextmonth(dat);
  dat.d++;
  dat.s = dat.m = dat.h = 0;
  return dat;
}
date nexthour(date dat) {
  if (dat.h == 23) return nextday(dat);
  dat.h++;
  dat.s = dat.m = 0;
  return dat;
}
date nextminute(date dat) {
  if (dat.m == 59) return nexthour(dat);
  dat.m++;
  dat.s = 0;
  return dat;
}
date nextsecond(date dat) {
  if (dat.s == 59) return nextminute(dat);
  dat.s++;
  return dat;
}
long long int rs, rm, rh, rdw, rd, rmon;
void solve(long long int x) {
  x++;
  date dat = ll2date(x);
  for (;;) {
    if (rmon != -1 and rmon != dat.mon)
      dat = nextmonth(dat);
    else if ((rdw != -1 and rd != -1 and rdw != dayofweek(dat) and
              rd != dat.d) or
             (rdw != -1 and rd == -1 and rdw != dayofweek(dat)) or
             (rdw == -1 and rd != -1 and rd != dat.d))
      dat = nextday(dat);
    else if (rh != -1 and rh != dat.h)
      dat = nexthour(dat);
    else if (rm != -1 and rm != dat.m)
      dat = nextminute(dat);
    else if (rs != -1 and rs != dat.s)
      dat = nextsecond(dat);
    else
      break;
  }
  cout << date2ll(dat) << endl;
}
int main() {
  cin >> rs >> rm >> rh >> rdw >> rd >> rmon;
  if (rdw != -1) rdw--;
  if (rd != -1) rd--;
  if (rmon != -1) rmon--;
  int n;
  cin >> n;
  for (int cas = 0; cas < n; cas++) {
    long long int x;
    cin >> x;
    solve(x);
  }
}
