#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
int n;
const int spy = 365 * 24 * 60 * 60;
const int spyl = 366 * 24 * 60 * 60;
const int dpm[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
struct task {
  int s, m, h, day, date, mon, y;
} a, b;
inline bool isleap(int year) {
  return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
}
inline int calcday(int year, int mon, int date) {
  int x = 0;
  for (int i = 1; i < mon; i++) {
    x += dpm[i];
    if (i == 2 && isleap(year)) x++;
  }
  x += date;
  int day = year - 1 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400 + x;
  day %= 7;
  if (!day) day = 7;
  return day;
}
void conv1(long long t) {
  for (b.y = 1970;; b.y++) {
    if (isleap(b.y)) {
      if (t < spyl) break;
      t -= spyl;
    } else {
      if (t < spy) break;
      t -= spy;
    }
  }
  for (b.mon = 1;; b.mon++) {
    int tmp = dpm[b.mon];
    if (b.mon == 2 && isleap(b.y)) tmp++;
    long long sec = tmp * 24 * 60 * 60;
    if (t < sec) break;
    t -= sec;
  }
  for (b.date = 1;; b.date++) {
    long long sec = 24 * 60 * 60;
    if (t < sec) break;
    t -= sec;
  }
  b.day = calcday(b.y, b.mon, b.date);
  b.h = (int)t / 3600;
  t %= 3600;
  b.m = (int)t / 60;
  t %= 60;
  b.s = (int)t;
}
void checkmonth() {
  if (b.mon == 13) {
    b.y++;
    b.mon = 1;
  }
}
void checkdate() {
  int tmp = dpm[b.mon];
  if (b.mon == 2 && isleap(b.y)) tmp++;
  if (b.date == tmp + 1) {
    b.mon++;
    b.date = 1;
  }
}
void checkhour() {
  if (b.h == 24) {
    b.h = 0;
    b.date++;
  }
}
void checkmin() {
  if (b.m == 60) {
    b.m = 0;
    b.h++;
  }
}
void checksec() {
  if (b.s == 60) {
    b.s = 0;
    b.m++;
  }
}
void calc() {
  if (a.mon != -1 && b.mon != a.mon) {
    b.mon++;
    checkmonth();
    b.date = 1;
    b.day = calcday(b.y, b.mon, b.date);
    b.h = b.m = b.s = 0;
    calc();
  }
  if (a.date != -1 && a.day != -1) {
    if (a.date != b.date && a.day != b.day) {
      b.date++;
      checkdate();
      checkmonth();
      b.day = calcday(b.y, b.mon, b.date);
      b.h = b.m = b.s = 0;
      calc();
    }
  } else if ((a.date != -1 && a.date != b.date) ||
             (a.day != -1 && a.day != b.day)) {
    b.date++;
    checkdate();
    checkmonth();
    b.day = calcday(b.y, b.mon, b.date);
    b.h = b.m = b.s = 0;
    calc();
  }
  if (a.h != -1 && a.h != b.h) {
    b.h++;
    b.m = b.s = 0;
    checkhour();
    checkdate();
    checkmonth();
    b.day = calcday(b.y, b.mon, b.date);
    calc();
  }
  if (a.m != -1 && a.m != b.m) {
    b.m++;
    b.s = 0;
    checkmin();
    checkhour();
    checkdate();
    checkmonth();
    b.day = calcday(b.y, b.mon, b.date);
    calc();
  }
  if (a.s != -1 && a.s != b.s) {
    b.s++;
    checksec();
    checkmin();
    checkhour();
    checkdate();
    checkmonth();
    b.day = calcday(b.y, b.mon, b.date);
    calc();
  }
}
long long conv2() {
  long long ans = 0;
  for (int i = 1970; i < b.y; i++) {
    if (isleap(i))
      ans += spyl;
    else
      ans += spy;
  }
  for (int i = 1; i < b.mon; i++) {
    if (isleap(b.y) && i == 2) ans += 24 * 60 * 60;
    ans += dpm[i] * 24 * 60 * 60;
  }
  ans += (b.date - 1) * 24 * 60 * 60;
  ans += b.h * 60 * 60;
  ans += b.m * 60;
  ans += b.s;
  return ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> a.s >> a.m >> a.h >> a.day >> a.date >> a.mon >> n;
  while (n--) {
    long long t;
    cin >> t;
    conv1(t + 1);
    calc();
    cout << conv2() << endl;
  }
  return 0;
}
