#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() {
  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;
}
