#include <bits/stdc++.h>
using namespace std;
using LL = long long;
int Year[31713 * 366], Yn;
int Time[24 * 60 * 60 + 10], Tn;
int D[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 30, 31, 30};
int main() {
  int S, M, H, Day, Date, Month;
  int s, m, h, day, ads, month;
  cin >> S >> M >> H >> Day >> Date >> Month;
  auto SameDay = [&]() {
    if (-1 == Month || Month == month) {
      if (-1 == Date) {
        if (-1 == Day)
          return true;
        else
          return Day == day;
      } else {
        if (-1 == Day) {
          if (Date == ads) return true;
        } else
          return Date == ads || Day == day;
      }
    }
    return false;
  };
  auto Leap = [&](int d) {
    if (d % 4) return 0;
    if (d % 100) return 1;
    if (d % 400) return 0;
    return 1;
  };
  int r = -1;
  day = 3;
  for (int year = 1970, End = 31711 + 1970, l; year < End; year++) {
    for (month = 1; month <= 12; month++) {
      l = D[month];
      if (month == 2) l += Leap(year);
      for (ads = 1; ads <= l; ads++) {
        r++;
        day++;
        if (day > 7) day = 1;
        if (SameDay()) {
          Year[Yn++] = r;
        }
      }
    }
  }
  auto SameTime = [&]() {
    return (-1 == H || H == h) && (-1 == M || M == m) && (-1 == S || S == s);
  };
  r = 0;
  for (h = 0; h < 24; h++)
    for (m = 0; m < 60; m++)
      for (s = 0; s < 60; s++, r++)
        if (SameTime()) Time[Tn++] = r;
  int test;
  cin >> test;
  LL T, Yea, Tim;
  int a, b;
  while (test--) {
    cin >> T;
    T++;
    Yea = T / 86400;
    Tim = T % 86400;
    a = lower_bound(Year, Year + Yn, Yea) - Year;
    if (Year[a] > Yea)
      cout << (LL)Year[a] * 86400 + Time[0] << endl;
    else {
      if (Time[Tn - 1] < Tim)
        cout << (LL)Year[a + 1] * 86400 + Time[0] << endl;
      else {
        cout << (LL)Year[a] * 86400 +
                    Time[lower_bound(Time, Time + Tn, Tim) - Time]
             << endl;
      }
    }
  }
  return 0;
}
