Sometime the classic solution are not powerful enough and we have to design our own. For the purpose of this problem you have to implement the part of the system of task scheduling.

Each task should be executed at some particular moments of time. In our system you may set the exact value for the second, minute, hour, day of the week, day and month, when the task should be executed. Moreover, one can set a special value -1 that means any value of this parameter is valid.

For example, if the parameter string is -1 59 23 -1 -1 -1, the problem will be executed every day at 23:59:00, 23:59:01, 23:59:02, ..., 23:59:59 (60 times in total).

Seconds, minutes and hours are numbered starting from zero, while day, months and days of the week are numbered starting from one. The first day of the week is Monday.

There is one special case that is treated separately. If both day of the week and day are given (i.e. differ from -1) to execute the task only one of these two (at least one, if both match this is fine too) parameters should match the current time (of course, all other parameters should match too). For example, the string of parameters 0 0 12 6 3 7 means that the task will be executed both on Saturday, July 2nd, 2016 and on Sunday, July 3rd, 2016 at noon.

One should not forget about the existence of the leap years. The year is leap if it's number is divisible by 400, or is not divisible by 100, but is divisible by 4. Each leap year has 366 days instead of usual 365, by extending February to 29 days rather than the common 28.

The current time is represented as the number of seconds passed after 00:00:00 January 1st, 1970 (Thursday).

You are given the string of six parameters, describing the moments of time the task should be executed. You are also given a number of moments of time. For each of them you have to find the first moment of time strictly greater than the current when the task will be executed.

Input

The first line of the input contains six integers s, m, h, day, date and month (0 ≤ s, m ≤ 59, 0 ≤ h ≤ 23, 1 ≤ day ≤ 7, 1 ≤ date ≤ 31, 1 ≤ month ≤ 12). Each of the number can also be equal to  - 1. It's guaranteed, that there are infinitely many moments of time when this task should be executed.

Next line contains the only integer n (1 ≤ n ≤ 1000) — the number of moments of time you have to solve the problem for. Each of the next n lines contains a single integer ti (0 ≤ ti ≤ 1012).

Output

Print n lines, the i-th of them should contain the first moment of time strictly greater than ti, when the task should be executed.

Examples

Input

-1 59 23 -1 -1 -1
6
1467372658
1467417540
1467417541
1467417598
1467417599
1467417600


Output

1467417540
1467417541
1467417542
1467417599
1467503940
1467503940


Input

0 0 12 6 3 7
3
1467372658
1467460810
1467547200


Output

1467460800
1467547200
1468065600

Note

The moment of time 1467372658 after the midnight of January 1st, 1970 is 11:30:58 July 1st, 2016.
#include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
int IT_MAX = 1 << 17;
const long long MOD = 1000000007;
const int INF = 1034567890;
const long long LL_INF = 1234567890123456789ll;
const double PI = acos(-1);
const long double ERR = 1E-10;
long long par[6];
long long M[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline long long leapYearCount(long long x) {
  return x / 4 - x / 100 + x / 400;
}
long long countDays(long long x) {
  long long t = (x - 1970) * 365 + leapYearCount(x - 1) - leapYearCount(1969);
  return t;
}
bool isLeap(long long y) {
  return (y % 4 == 0 && y % 100 != 0) || y % 400 == 0;
}
long long whichDay(long long y, long long m, long long d) {
  long long u = countDays(y);
  if (isLeap(y))
    M[2] = 29;
  else
    M[2] = 28;
  for (long long i = 1; i < m; i++) u += M[i];
  u += d - 1;
  return (u + 3) % 7 + 1;
}
long long* ch(long long X) {
  long long* RV = new long long[6];
  long long st = 1971, en = INF, mi, rv = 1970;
  while (st <= en) {
    mi = (st + en) / 2;
    if (countDays(mi) * 86400 <= X) {
      rv = mi;
      st = mi + 1;
    } else
      en = mi - 1;
  }
  RV[5] = rv;
  X -= countDays(rv) * 86400;
  if (isLeap(RV[5]))
    M[2] = 29;
  else
    M[2] = 28;
  long long n = X / 86400;
  for (RV[4] = 1; RV[4] <= 12; RV[4]++) {
    if (n < M[RV[4]]) {
      RV[3] = n + 1;
      break;
    }
    n -= M[RV[4]];
  }
  long long t = X % 86400;
  RV[2] = t / 3600;
  RV[1] = (t % 3600) / 60;
  RV[0] = t % 60;
  return RV;
}
long long rch(long long* u) {
  long long rv = 0;
  rv += countDays(u[5]) * 86400;
  if (isLeap(u[5]))
    M[2] = 29;
  else
    M[2] = 28;
  long long i;
  for (i = 1; i < u[4]; i++) rv += M[i] * 86400;
  rv += (u[3] - 1) * 86400;
  rv += u[2] * 3600;
  rv += u[1] * 60;
  rv += u[0];
  return rv;
}
bool isValid(long long* u) {
  if (u[0] != -1 && par[0] != -1 && u[0] != par[0]) return false;
  if (u[1] != -1 && par[1] != -1 && u[1] != par[1]) return false;
  if (u[2] != -1 && par[2] != -1 && u[2] != par[2]) return false;
  if (u[4] != -1 && par[5] != -1 && u[4] != par[5]) return false;
  if (!isLeap(u[5]) && par[5] == 2 && par[4] == 29 && par[3] == -1)
    return false;
  if (isLeap(u[5]))
    M[2] = 29;
  else
    M[2] = 28;
  if (u[4] == -1) return true;
  long long st, en;
  if (u[3] != -1)
    st = en = u[3];
  else
    st = 1, en = M[u[4]];
  for (long long i = st; i <= en; i++) {
    if (par[3] == -1 && par[4] == -1) return true;
    if (par[3] != -1) {
      if (whichDay(u[5], u[4], i) == par[3]) return true;
    }
    if (par[4] != -1) {
      if (par[4] == i) return true;
    }
  }
  return false;
}
int main() {
  for (int i = 0; i < 6; i++) scanf("%lld", &par[i]);
  int T;
  scanf("%d", &T);
  while (T--) {
    long long X, i, j;
    scanf("%lld", &X);
    long long* in = ch(X);
    long long* ans = new long long[6];
    for (i = 0; i < 6; i++) ans[i] = in[i];
    for (i = 0; i < 6; i++) {
      long long st, en;
      if (i == 0 || i == 1)
        st = in[i] + 1, en = 59;
      else if (i == 2)
        st = in[i] + 1, en = 23;
      else if (i == 3) {
        if (isLeap(ans[5]))
          M[2] = 29;
        else
          M[2] = 28;
        st = in[i] + 1, en = M[ans[4]];
      } else if (i == 4)
        st = in[i] + 1, en = 12;
      else if (i == 5)
        st = in[i] + 1, en = LL_INF;
      for (j = st; j <= en; j++) {
        ans[i] = j;
        if (isValid(ans)) break;
      }
      if (j <= en) break;
      ans[i] = -1;
    }
    for (i = i - 1; i >= 0; i--) {
      long long st = 0;
      if (i >= 3) st = 1;
      for (j = st;; j++) {
        ans[i] = j;
        if (isValid(ans)) break;
      }
    }
    printf("%lld\n", rch(ans));
  }
  return 0;
}
