#include <bits/stdc++.h>
using namespace std;
const int months[2][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}};
const int day_in_seconds = 24 * 60 * 60, days = 365, day0 = 3, year0 = 1970,
          seconds = 60, minutes = 60, hours = 24, week = 7;
bool leap(int year) {
  return (year % 400 == 0 || (year % 100 > 0 && year % 4 == 0));
}
int s, m, h, day, date, month;
int good_second(int after) {
  for (int i = after; i < seconds; i++)
    if (s == -1 || i == s) return i;
  return -1;
}
int good_minute_second(int after) {
  int m2 = after / seconds, s2 = after % seconds;
  for (int i = m2, fl = 1; i < minutes; i++, fl = 0)
    if (m == -1 || i == m) {
      int num = good_second(s2 * fl);
      if (num != -1) return i * seconds + num;
    }
  return -1;
}
int good_hour_minute_second(int after) {
  int h2 = after / (seconds * minutes), ms2 = after % (seconds * minutes);
  for (int i = h2, fl = 1; i < hours; i++, fl = 0)
    if (h == -1 || i == h) {
      int num = good_minute_second(ms2 * fl);
      if (num != -1) return i * seconds * minutes + num;
    }
  return -1;
}
bool good_day(int d, int w) {
  if (day == w || date == d || (day == -1 && date == -1)) return true;
  return false;
}
long long good_time_in_a_year(int year, long long after, int first_day) {
  int mm = 0, dd = 0, ord = after / (long long)day_in_seconds;
  for (int d = 0, fl = 1; d < days + leap(year); d++, dd++) {
    if (dd == months[leap(year)][mm]) mm++, dd = 0;
    if (d < ord) continue;
    if ((month == -1 || month == mm) && good_day(dd, (d + first_day) % week)) {
      long long num =
          good_hour_minute_second((after % (long long)day_in_seconds) * fl);
      if (num != -1) return d * 1ll * day_in_seconds + num;
    }
    fl = 0;
  }
  return -1;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> s >> m >> h >> day >> date >> month;
  if (day > -1) day--;
  if (date > -1) date--;
  if (month > -1) month--;
  int q;
  cin >> q;
  while (q--) {
    long long t;
    cin >> t;
    t++;
    long long sum = 0;
    for (int year = year0, w = day0;; year++) {
      long long my_seconds = (days + leap(year)) * 1ll * day_in_seconds;
      if (t <= sum + my_seconds) {
        long long num = good_time_in_a_year(year, max(0ll, t - sum), w);
        if (num != -1) {
          cout << sum + num << "\n";
          break;
        }
      }
      sum += my_seconds, w = (w + days + leap(year)) % (long long)week;
    }
  }
  return 0;
}
