#include <iostream>

using namespace std;

inline int getSecsInYear(int year) {
    if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
        return 60 * 60 * 24 * 366;
    else
        return 60 * 60 * 24 * 365;
}

int lm[12] = {31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

inline int getSecsInMonth(int year, int month) {
    if(month == 1) {
        if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
            return 60 * 60 * 24 * 29;
        else
            return 60 * 60 * 24 * 28;
    }
    else
        return 60 * 60 * 24 * lm[month];
}

inline void incrementDay(int &day, int &date, int &month, int &year, long long &t) {
    day = (day + 1) % 7;
    t += 60 * 60 * 24;
    if(month == 1) {
        if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) {
            if(date == 28) {
                date = 0;
                month++;
            }
            else
                date++;
        }
        else {
            if(date == 27) {
                date = 0;
                month++;
            }
            else
                date++;
        }
    }
    else {
        if(date + 1 == lm[month]) {
            date = 0;
            if(month == 11) {
                month = 0;
                year++;
            }
            else
                month++;
        }
        else
            date++;
    }
}

inline bool incrementTime(int &s, int &m, int &h, long long &t) {
    t++;
    if(s == 59) {
        s = 0;
        if(m == 59) {
            m = 0;
            if(h == 23)
                return false;
            else
                h++;
        }
        else
            m++;
    }
    else
        s++;
    return true;
}

inline bool check(int x, int y) {
    if(y == -1)
        return true;
    return x == y;
}

inline bool checkDay(int cday, int cdate, int day, int date) {
    if(day == -1)
        return check(cdate, date);
    if(date == -1)
        return check(cday, day);
    return check(cdate, date) || check(cday, day);
}

inline long long solveToday(int cs, int cm, int ch,
                            int cday, int cdate, int cmonth, int cyear, long long ct,
                            int s, int m, int h, int day, int date, int month) {
    if(!checkDay(cday, cdate, day, date) || !check(cmonth, month))
        return -1;
    while(true) {
        if(check(cs, s) && check(cm, m) && check(ch, h))
            return ct;
        if(!incrementTime(cs, cm, ch, ct))
            break;
    }
    return -1;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int s, m, h, day, date, month;
    cin >> s >> m >> h >> day >> date >> month;
    if(day != -1)
        day--;
    if(date != -1)
        date--;
    if(month != -1)
        month--;
    int n;
    cin >> n;
    for(int q = 0; q < n; q++) {
        long long t;
        cin >> t;
        t++;
        int cs = 0, cm = 0, ch = 0, cday = 3, cdate = 0, cmonth = 0, cyear = 1970;
        long long ct = t;
        while(true) {
            int sy = getSecsInYear(cyear);
            if(t >= sy) {
                t -= sy;
                cday = (cday + sy / (60 * 60 * 24)) % 7;
                cyear++;
            }
            else
                break;
        }
        while(true) {
            int sm = getSecsInMonth(cyear, cmonth);
            if(t >= sm) {
                t -= sm;
                cday = (cday + sm / (60 * 60 * 24)) % 7;
                cmonth++;
            }
            else
                break;
        }
        while(t >= 60 * 60 * 24) {
            t -= 60 * 60 * 24;
            cday = (cday + 1) % 7;
            cdate++;
        }
        while(t >= 60 * 60) {
            t -= 60 * 60;
            ch++;
        }
        while(t >= 60) {
            t -= 60;
            cm++;
        }
        cs += t;
        long long ans = solveToday(cs, cm, ch, cday, cdate, cmonth, cyear, ct,
                                   s, m, h, day, date, month);
        if(ans != -1) {
            cout << ans << '\n';
            continue;
        }
        ct -= cs + 60 * cm + 60 * 60 * ch;
        while(true) {
            incrementDay(cday, cdate, cmonth, cyear, ct);
            ans = solveToday(0, 0, 0, cday, cdate, cmonth, cyear, ct,
                             s, m, h, day, date, month);
            if(ans != -1)
                break;
        }
        cout << ans << '\n';
    }
    return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define ub upper_bound
#define bs binary_search
typedef long long LL;
using namespace std;
int md[][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;
int n;
LL x, tmp1 = 0, tmp2 = 0, today = 3;
vector<LL> a, b;

int match(int h, int m, int s) {
    if ((second != -1) && (second != s)) return 0;
    if ((minute != -1) && (minute != m)) return 0;
    if ((hour != -1) && (hour != h)) return 0;
    return 1;
}

int lp(int x) {
    return ((x % 400 == 0) || (x % 100 != 0)) && (x % 4 == 0);
}

void prepare() {
    for(int h = 0; h < 24; h++)
        for(int m = 0; m < 60; m++)
            for(int s = 0; s < 60; s++, tmp1++)
                if (match(h, m, s)) a.pb(tmp1);
    for(int y = 1970; y < 2400; y++)
        for(int m = 0; m < 12; m++)
            for(int d = 0; d < md[lp(y)][m]; d++, tmp2++) {
                if ((month == -1) || (month == m))
                    if (((date != -1) && (date == d)) || ((day != -1) && (day == today)) || ((date == -1) && (day == -1))) b.pb(tmp2);
                today = (today + 1) % 7;
            }
}

int main() {
    cin >> second >> minute >> hour >> day >> date >> month;
    day -= (day != -1), date -= (date != -1), month -= (month != -1);
    prepare();
    cin >> n;
    while (n--) {
        cin >> x;
        LL y = x / tmp1 / tmp2, r = x % (tmp1 * tmp2), d = r / tmp1, ans;
        r %= tmp1;
        if ((d > b.back()) || ((d == b.back()) && (r > a.back())))
            ans = (y + 1) * tmp1 * tmp2 + b[0] * tmp1 + a[0];
        else if ((r >= a.back()) || (!bs(b.begin(), b.end(), d)))
            ans = y * tmp1 * tmp2 + *ub(b.begin(), b.end(), d) * tmp1 + a[0];
        else ans = y * tmp1 * tmp2 + d * tmp1 + *ub(a.begin(), a.end(), r);
        cout << ans << endl;
    }
}


#include<bits/stdc++.h>
#define pb push_back
#define ub upper_bound
#define bs binary_search
typedef long long LL;
using namespace std;
int md[][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;
int n;
LL x, tmp1 = 0, tmp2 = 0, today = 3;
vector<LL> a, b;

int match(int h, int m, int s) {
    if ((second != -1) && (second != s)) return 0;
    if ((minute != -1) && (minute != m)) return 0;
    if ((hour != -1) && (hour != h)) return 0;
    return 1;
}

int lp(int x) {
    return ((x % 400 == 0) || (x % 100 != 0)) && (x % 4 == 0);
}

void prepare() {
    for(int h = 0; h < 24; h++)
        for(int m = 0; m < 60; m++)
            for(int s = 0; s < 60; s++, tmp1++)
                if (match(h, m, s)) a.pb(tmp1);
    for(int y = 1970; y < 2400; y++)
        for(int m = 0; m < 12; m++)
            for(int d = 0; d < md[lp(y)][m]; d++, tmp2++) {
                if ((month == -1) || (month = m))
                    if (((date != -1) && (date == d)) || ((day != -1) && (day == today)) || ((date == -1) && (day == -1))) b.pb(tmp2);
                today = (today + 1) % 7;
            }
}

int main() {
    cin >> second >> minute >> hour >> day >> date >> month;
    day -= (day != -1), date -= (date != -1), month -= (month != -1);
    prepare();
    cin >> n;
    while (n--) {
        cin >> x;
        LL y = x / tmp1 / tmp2, r = x % (tmp1 * tmp2), d = r / tmp1, ans;
        r %= tmp1;
        if ((d > b.back()) || ((d == b.back()) && (r > a.back())))
            ans = (y + 1) * tmp1 * tmp2 + b[0] * tmp1 + a[0];
        else if ((r >= a.back()) || (!bs(b.begin(), b.end(), d)))
            ans = y * tmp1 * tmp2 + *ub(b.begin(), b.end(), d) * tmp1 + a[0];
        else ans = y * tmp1 * tmp2 + d * tmp1 + *ub(a.begin(), a.end(), r);
        cout << ans << endl;
    }
}


#include<bits/stdc++.h>
#define pb push_back
#define ub upper_bound
#define bs binary_search
typedef long long LL;
using namespace std;
int md[][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;
int n;
LL x, tmp1 = 0, tmp2 = 0, today = 3;
vector<LL> a, b;

int match(int h, int m, int s) {
    if ((second != -1) && (second != s)) return 0;
    if ((minute != -1) && (minute != m)) return 0;
    if ((hour != -1) && (hour != h)) return 0;
    return 1;
}

int lp(int x) {
    return ((x % 400 == 0) || (x % 100 != 0)) && (x % 4 == 0);
}

void prepare() {
    for(int h = 0; h < 24; h++)
        for(int m = 0; m < 60; m++)
            for(int s = 0; s < 60; s++, tmp1++)
                if (match(h, m, s)) a.pb(tmp1);
    for(int y = 1970; y < 1970 + 400; y++)
        for(int m = 0; m < 12; m++)
            for(int d = 0; d < md[lp(y)][m]; d++, tmp2++) {
                if ((month == -1) || (month == m))
                    if (((date != -1) && (date == d)) || ((day != -1) && (day == today)) || ((date == -1) && (day == -1))) b.pb(tmp2);
                today = (today + 1) % 7;
            }
}

int main() {
    cin >> second >> minute >> hour >> day >> date >> month;
    day -= (day != -1), date -= (date != -1), month -= (month != -1);
    prepare();
    cin >> n;
    while (n--) {
        cin >> x;
        LL y = x / tmp1 / tmp2, r = x % (tmp1 * tmp2), d = r / tmp1, ans;
        r %= tmp1;
        if ((d > b.back()) || ((d == b.back()) && (r > a.back())))
            ans = (y + 1) * tmp1 * tmp2 + b[0] * tmp1 + a[0];
        else if ((r >= a.back()) || (!bs(b.begin(), b.end(), d)))
            ans = y * tmp1 * tmp2 + *ub(b.begin(), b.end(), d) * tmp1 + a[0];
        else ans = y * tmp1 * tmp2 + d * tmp1 + *ub(a.begin(), a.end(), r);
        cout << ans << endl;
    }
}


#include <bits/stdc++.h>
using namespace std;

#define ms(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORd(i, a, b) for (int i = (a) - 1; i >= (b); i--)
#define FORall(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
#define sz(a) int((a).size())
#define present(t, x) (t.find(x) != t.end())
#define all(a) (a).begin(), (a).end()
#define uni(a) (a).erase(unique(all(a)), (a).end())
#define pb push_back
#define pf push_front
#define mp make_pair
#define fi first
#define se second
#define prec(n) fixed<<setprecision(n)
#define bit(n, i) (((n) >> (i)) & 1)
#define bitcount(n) __builtin_popcountll(n)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vii;
const int MOD = (int) 1e9 + 7;
const int INF = (int) 1e9;
const ll LINF = (ll) 1e18;
const ld PI = acos((ld) -1);
const ld EPS = 1e-9;
inline ll gcd(ll a, ll b) {ll r; while (b) {r = a % b; a = b; b = r;} return a;}
inline ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
inline ll fpow(ll n, ll k, int p = MOD) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n % p; n = n * n % p;} return r;}
template<class T> inline int chkmin(T& a, const T& val) {return a > val ? a = val, 1 : 0;}
template<class T> inline int chkmax(T& a, const T& val) {return a < val ? a = val, 1 : 0;}
inline ll isqrt(ll k) {ll r = sqrt(k) + 1; while (r * r > k) r--; return r;}
inline ll icbrt(ll k) {ll r = cbrt(k) + 1; while (r * r * r > k) r--; return r;}
inline void addmod(int& a, int val, int p = MOD) {if ((a = (a + val)) >= p) a -= p;}
inline void submod(int& a, int val, int p = MOD) {if ((a = (a - val)) < 0) a += p;}
inline int mult(int a, int b, int p = MOD) {return (ll) a * b % p;}
inline int inv(int a, int p = MOD) {return fpow(a, p - 2, p);}

string dayOfWeek[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
//converts Gregorian date to integer (Julian day number)
int dateToInt(int m, int d, int y) {
	return
	    1461 * (y + 4800 + (m - 14) / 12) / 4 +
	    367 * (m - 2 - (m - 14) / 12 * 12) / 12 -
	    3 * ((y + 4900 + (m - 14) / 12) / 100) / 4 +
	    d - 32075;
}
//converts integer (Julian day number) to Gregorian date: month/day/year
void intToDate(long long jd, long long &m, long long &d, long long &y) {
	long long x, n, i, j;
	x = jd + 68569;
	n = 4 * x / 146097;
	x -= (146097 * n + 3) / 4;
	i = (4000 * (x + 1)) / 1461001;
	x -= 1461 * i / 4 - 31;
	j = 80 * x / 2447;
	d = x - 2447 * j / 80;
	x = j / 11;
	m = j + 2 - 12 * x;
	y = 100 * (n - 49) + i + x;
}
//converts integer (Julian day number) to day of week
string intToDay(long long jd) {
	return dayOfWeek[jd % 7];
}

int s, m, h, day, date, month;

int checkdate(long long t) {
	long long m, d, y;
	t += dateToInt(1, 1, 1970);
	intToDate(t, m, d, y);
	if (month != -1 && month != m) {
		return 0;
	}
	if (day != -1 && date != -1) {
		if (day == t % 7 + 1 || date == d) {
			return 1;
		}
		else {
			return 0;
		}
	}
	else if (day != -1) {
		return day == t % 7 + 1;
	}
	else if (date != -1) {
		return date == d;
	}
}

int checktime(int smh) {
	int ss = smh % 60; smh /= 60;
	int mm = smh % 60; smh /= 60;
	int hh = smh;
	if (s != -1 && s != ss) return 0;
	if (m != -1 && m != mm) return 0;
	if (h != -1 && h != hh) return 0;
	return 1;
}

void solve() {
	cin >> s >> m >> h >> day >> date >> month;
	int q; cin >> q;
	while (q--) {
		long long t; cin >> t;
		int smh = t % (60 * 60 * 24); t /= 60 * 60 * 24;
		if (checkdate(t)) {
			int found = 0;
			for (smh++; smh < 60 * 60 * 24; smh++) {
				if (checktime(smh)) {
					found = 1;
					break;
				}
			}
			if (found) {
				cout << t * 60 * 60 * 24 + smh << "\n";
				continue;
			}
		}
		t++;
		while (!checkdate(t)) t++;
		FOR(i, 0, 60 * 60 * 24) {
			if (checktime(i)) {
				cout << t * 60 * 60 * 24 + i << "\n";
				break;
			}
		}
	}
}

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef _LOCAL_
	freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout);
#endif
	solve();
#ifdef _LOCAL_
	//printf("\nTime elapsed: %dms", 1000 * clock() / CLOCKS_PER_SEC);
#endif
	return 0;
}


#include <iostream>

using namespace std;

typedef long long int ll;

struct date {
  ll s,m,h,d,mon,y;
  date() {}
  date(ll s,ll m,ll h,ll d,ll mon,ll y):s(s),m(m),h(h),d(d),mon(mon),y(y) {}
};

void escribe(date dat)
{
  cout<<"("<<dat.s<<","<<dat.m<<","<<dat.h<<","<<dat.d<<","<<dat.mon<<","<<dat.y<<")";
  cout<<endl;
}

ll leapsbefore(ll y)
{
  y--;
  return y/4-y/100+y/400;
}

ll leapssince1970(ll y)
{
  return leapsbefore(y)-leapsbefore(1969);
}

bool isleap(ll y)
{
  return y%400==0 or (y%100!=0 and y%4==0);
}

ll daysbefore(ll y)
{
  return 365*(y-1970)+leapssince1970(y);
}

const ll secondsperday=86400;

ll ll2y(ll x)
{
  ll d=x/secondsperday;
  ll inf=1970;
  ll sup=10000000000LL;
  while (inf+1<sup) {
    ll med=(inf+sup)/2;
    if (daysbefore(med)<=d) inf=med;
    else sup=med;
  }
  return inf;
}

int m2d[12]={31,28,31,30,31,30,31,31,30,31,30,31};

ll daysthismonth(ll mon,ll y)
{
  if (mon==1 and isleap(y)) return 29;
  return m2d[mon];
}

ll daysthismonth(date dat)
{
  return daysthismonth(dat.mon,dat.y);
}

ll daysbefore(ll mon,ll y)
{
  ll d=0;
  for (int i=0;i<mon;i++)
    d+=daysthismonth(i,y);
  return d;
}

ll daysbefore(date dat)
{
  return daysbefore(dat.y)+daysbefore(dat.mon,dat.y)+dat.d;
}

ll dayofweek(date dat)
{
  return (3+daysbefore(dat))%7;
}

date ll2date(ll x)
{
  ll y=ll2y(x);
  x-=daysbefore(y)*secondsperday;
  ll mon=0;
  while (mon<12 and daysbefore(mon+1,y)*secondsperday<=x) mon++;
  x-=daysbefore(mon,y)*secondsperday;
  return date(x%60,x/60%60,x/3600%24,x/86400,mon,y);
}

ll date2ll(date dat)
{
  ll d=daysbefore(dat.y)+daysbefore(dat.mon,dat.y)+dat.d;
  return d*secondsperday+dat.s+dat.m*60+dat.h*3600;
}

date nextyear(date dat)
{
  dat.y++;
  dat.s=dat.m=dat.h=dat.d=dat.mon=0;
  return dat;
}

date nextmonth(date dat)
{
  if (dat.mon==11) return nextyear(dat);
  dat.mon++;
  dat.s=dat.m=dat.h=dat.d=0;
  return dat;
}

date nextday(date dat)
{
  if (dat.d+1==daysthismonth(dat)) return nextmonth(dat);
  dat.d++;
  dat.s=dat.m=dat.h=0;
  return dat;
}

date nexthour(date dat)
{
  if (dat.h==23) return nextday(dat);
  dat.h++;
  dat.s=dat.m=0;
  return dat;
}

date nextminute(date dat)
{
  if (dat.m==59) return nexthour(dat);
  dat.m++;
  dat.s=0;
  return dat;
}

date nextsecond(date dat)
{
  if (dat.s==59) return nextminute(dat);
  dat.s++;
  return dat;
}

ll rs,rm,rh,rdw,rd,rmon;

void solve(ll x)
{
  //cout<<endl;
  //cout<<"tratar "<<x<<endl;
  x++;
  date dat=ll2date(x);
  //escribe(dat);
  for (;;) {
    if (rmon!=-1 and rmon!=dat.mon) dat=nextmonth(dat);
    else if ((rdw!=-1 and rd!=-1 and rdw!=dayofweek(dat) and rd!=dat.d) or
	     (rdw!=-1 and rd==-1 and rdw!=dayofweek(dat)) or
	     (rdw==-1 and rd!=-1 and rd!=dat.d)) dat=nextday(dat);
    else if (rh!=-1 and rh!=dat.h) dat=nexthour(dat);
    else if (rm!=-1 and rm!=dat.m) dat=nextminute(dat);
    else if (rs!=-1 and rs!=dat.s) dat=nextsecond(dat);
    else break;
    //escribe(dat);
  }
  cout<<date2ll(dat)<<endl;
}

int main()
{
  cin>>rs>>rm>>rh>>rdw>>rd>>rmon;
  if (rdw!=-1) rdw--;
  if (rd!=-1) rd--;
  if (rmon!=-1) rmon--;
  int n;
  cin>>n;
  for (int cas=0;cas<n;cas++) {
    ll x;
    cin>>x;
    solve(x);
  }
}


#include <bits/stdc++.h>
using namespace std;
namespace fastIO{
	#define BUF_SIZE 100000
	#define OUT_SIZE 100000
	//fread->read
	bool IOerror=0;
//	inline char nc(){char ch=getchar();if(ch==-1)IOerror=1;return ch;}
	inline char nc(){
		static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
		if(p1==pend){
			p1=buf;pend=buf+fread(buf,1,BUF_SIZE,stdin);
			if(pend==p1){IOerror=1;return -1;}
		}
		return *p1++;
	}
	inline bool blank(char ch){return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';}
	template<class T> inline bool read(T &x){
		bool sign=0;char ch=nc();x=0;
		for(;blank(ch);ch=nc());
		if(IOerror)return false;
		if(ch=='-')sign=1,ch=nc();
		for(;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0';
		if(sign)x=-x;
		return true;
	}
	inline bool read(double &x){
		bool sign=0;char ch=nc();x=0;
		for(;blank(ch);ch=nc());
		if(IOerror)return false;
		if(ch=='-')sign=1,ch=nc();
		for(;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0';
		if(ch=='.'){
			double tmp=1; ch=nc();
			for(;ch>='0'&&ch<='9';ch=nc())tmp/=10.0,x+=tmp*(ch-'0');
		}
		if(sign)x=-x;
		return true;
	}
	inline bool read(char *s){
		char ch=nc();
		for(;blank(ch);ch=nc());
		if(IOerror)return false;
		for(;!blank(ch)&&!IOerror;ch=nc())*s++=ch;
		*s=0;
		return true;
	}
	inline bool read(char &c){
		for(c=nc();blank(c);c=nc());
		if(IOerror){c=-1;return false;}
		return true;
	}
	template<class T,class... U>bool read(T& h,U&... t){return read(h)&&read(t...);}
	#undef OUT_SIZE
	#undef BUF_SIZE
};
using namespace fastIO;
/************* debug begin *************/
string to_string(string s){return '"'+s+'"';}
string to_string(const char* s){return to_string((string)s);}
string to_string(const bool& b){return(b?"true":"false");}
template<class T>string to_string(T x){ostringstream sout;sout<<x;return sout.str();}
template<class A,class B>string to_string(pair<A,B> p){return "("+to_string(p.first)+", "+to_string(p.second)+")";}
template<class A>string to_string(const vector<A> v){
	int f=1;string res="{";for(const auto x:v){if(!f)res+= ", ";f=0;res+=to_string(x);}res+="}";
	return res;
}
void debug_out(){puts("");}
template<class T,class... U>void debug_out(const T& h,const U&... t){cout<<" "<<to_string(h);debug_out(t...);}
#ifdef tokitsukaze
#define debug(...) cout<<"["<<#__VA_ARGS__<<"]:",debug_out(__VA_ARGS__);
#else
#define debug(...) 233;
#endif
/*************  debug end  *************/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define mem(a,b) memset((a),(b),sizeof(a))
#define MP make_pair
#define pb push_back
#define fi first
#define se second
#define sz(x) (int)x.size()
#define all(x) x.begin(),x.end()
using namespace __gnu_cxx;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef vector<int> VI;
typedef vector<ll> VL;
void go();
int main(){
	#ifdef tokitsukaze
		freopen("TEST.txt","r",stdin);
	#endif
	go();return 0;
}
const int INF=0x3f3f3f3f;
const ll LLINF=0x3f3f3f3f3f3f3f3f;
const double PI=acos(-1.0);
const double eps=1e-8;
const int MAX=2e5+10;
const ll mod=1e9+7;
/*********************************head*********************************/
const ll loop=86400;
int sec[86500],tot,day[150000],ds,cur;
int s,m,h,d,dt,mt;
ll perioid,dd,ss,x,ans;
int md[][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 leap(int x){return ((x%400==0)||(x%100!=0))&&(x%4==0);}
int check(int _mt,int _dt,int _d)
{
	if(mt!=-1&&mt!=_mt) return 0;
	if((dt==-1&&d==-1)||dt==_dt||d==_d) return 1;
	return 0;
}
void init()
{
	int i,j,k;
	tot=0;
	for(i=0;i<24;i++)
	{
		if(!(h==-1||h==i)) continue;
		for(j=0;j<60;j++)
		{
			if(!(m==-1||m==j)) continue;
			for(k=0;k<60;k++)
			{
				if(!(s==-1||s==k)) continue;
				sec[tot++]=i*3600+j*60+k;
			}
		}
	}
	for(i=0;i<400;i++)
	{
		for(j=0;j<12;j++)
		{
			for(k=0;k<md[leap(i+1970)][j];k++)
			{
				if(check(j,k,(cur+3)%7)) day[ds++]=cur;
				cur++;
			}
		}
	}
	perioid=cur*loop;
}
void go()
{
	int q,id,pos;
	while(read(s,m,h,d,dt,mt))
	{
		if(d>0) d--;
		if(dt>0) dt--;
		if(mt>0) mt--;
		init();
		read(q);
		while(q--)
		{
			read(x);
			ans=x-x%perioid;
			x=x-ans;
			dd=x/loop;
			ss=x%loop;
			pos=lower_bound(day,day+ds,dd)-day;
			if(pos==ds) ans+=perioid+day[0]*loop+sec[0];
			else if(day[pos]>dd)ans+=day[pos]*loop+sec[0];
			else
			{
				id=upper_bound(sec,sec+tot,ss)-sec;
				if(id<tot) ans+=day[pos]*loop+sec[id];
				else
				{
					pos++;
					if(pos==ds)ans+=perioid+day[0]*loop+sec[0];
					else ans+=day[pos]*loop+sec[0];
				}
			}
			printf("%lld\n",ans);
		}
	}
}


#include<bits/stdc++.h>
#define date ads
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() {
#ifndef ONLINE_JUDGE
  freopen("in.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif

  int S, M, H, Day, Date, Month;
  int s, m, h, day, date, 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 == date) return true;
        }else return Date == date || 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(date=1; date<=l; date++) {
        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;
}


#include<bits/stdc++.h>
#define date ads
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() {
#ifndef ONLINE_JUDGE
  freopen("in.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif

  int S, M, H, Day, Date, Month;
  int s, m, h, day, date, month;
  cin>>S>>M>>H>>Day>>Date>>Month;
  auto SameDay=[&]() { return ((-1 == Day || Day == day) || (-1 == Date || Date == date)) && (-1 == Month || Month == month); };
  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(date=1; date<=l; date++) {
        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;
}


#include<bits/stdc++.h>
#define date ads
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() {
#ifndef ONLINE_JUDGE
  freopen("in.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif

  int S, M, H, Day, Date, Month;
  int s, m, h, day, date, 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 == date) return true;
        }else return Date == date || 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(date=1; date<=l; date++) {
        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, ans;
  int a, b;
  while(test--) {
    scanf("%I64d", &T); T++;
    Yea=T/86400;
    Tim=T%86400;
    a=lower_bound(Year, Year+Yn, Yea)-Year;
    if(Year[a]>Yea) ans=(LL)Year[a]*86400+Time[0];
    else {
      if(Time[Tn-1]<Tim) ans=(LL)Year[a+1]*86400+Time[0];
      else {
        ans=(LL)Year[a]*86400+Time[lower_bound(Time, Time+Tn, Tim)-Time];
      }
    }
    printf("%I64d\n", ans);
  }
  return 0;
}


#include<bits/stdc++.h>

typedef long long ll;
const int N = 410;
inline bool leap(int year){return year % 400 == 0 || year % 4 == 0 && year % 100 != 0;}

int years[N];
int month[N] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int months[N];

struct Time{
    int year;
    int month;
    int day;
    int hour;
    int minute;
    int second;

    Time():year(0), month(0), day(0), hour(0), minute(0), second(0){}
    Time(int year, int month, int day, int hour, int minute, int second)
        :year(year), month(month), day(day), hour(hour), minute(minute), second(second){}

    int toDay(){
        ll ret = day - 1 + months[month - 1];
        ret += month >= 3 && leap(year);
        ret += (year - 1970) / 400 * years[400] + years[(year - 1970) % 400];
        return ret;
    }

    int toWeek(){
        int ret = (4 + toDay()) % 7;
        return ret ? ret : 7;
    }

    ll toSecond(){
        ll ret = 1ll * toDay() * 86400;
        ret += hour * 3600;
        ret += minute * 60 + second;
        return ret;
    }

    void addDay(){
        second = minute = hour = 0;
        ++ day;
        int limit = ::month[month] + (month == 2 && leap(year));
        if (day > limit){
            ++ month;
            day -= limit;
        }
        if (month > 12){
            month = 1;
            ++ year;
        }
    }
};

Time toTime(ll timeSecond){
    Time nowTime(1970, 1, 1, 0, 0, 0);
    int left = 1970, right = 100000;
    while (left < right){
        int mid = left + right + 1 >> 1;
        nowTime.year = mid;
        if (nowTime.toSecond() > timeSecond){
            right = mid - 1;
        }
        else{
            left = mid;
        }
    }
    nowTime.year = left;
    left = 1, right = 12;
    while (left < right){
        int mid = left + right + 1 >> 1;
        nowTime.month = mid;
        if (nowTime.toSecond() > timeSecond){
            right = mid - 1;
        }
        else{
            left = mid;
        }
    }
    nowTime.month = left;
    nowTime.day += (timeSecond - nowTime.toSecond()) / 86400;
    nowTime.hour = (timeSecond - nowTime.toSecond()) / 3600;
    nowTime.minute = (timeSecond - nowTime.toSecond()) / 60;
    nowTime.second = timeSecond - nowTime.toSecond();
    return nowTime;
}

int main(){
    for (int i = 1970; i < 2370; ++ i){
        years[i - 1970 + 1] = years[i - 1970] + (365 + leap(i));
    }
    for (int i = 1; i <= 12; ++ i){
        months[i] = month[i] + months[i - 1];
    }
    int s, m, h, day, date, month;
    scanf("%d%d%d%d%d%d", &s, &m, &h, &day, &date, &month);
    int test;
    scanf("%d", &test);
    while (test --){
        ll second;
        scanf("%lld", &second);
        ++ second;
        Time nowTime = toTime(second);
        if (~h && nowTime.hour > h){
            nowTime.addDay();
        }
        else if (~h && nowTime.hour == h){
           if (~m && nowTime.minute > m){
               nowTime.addDay();
           }
           else if (~s && nowTime.second > s){
               nowTime.addDay();
           }
        }

        while (~month && nowTime.month != month ||
                ~day && nowTime.toWeek() != day &&
                ~date && nowTime.day != date){
            nowTime.addDay();
        }
        while (~h && h > nowTime.hour){
            ++ nowTime.hour;
            nowTime.minute = nowTime.second = 0;
        }
        while (~m && m > nowTime.minute){
            ++ nowTime.minute;
            nowTime.second = 0;
        }
        while (~s && s > nowTime.second){
            ++ nowTime.second;
        }
        printf("%lld\n", nowTime.toSecond());
    }
    return 0;
}


#include<bits/stdc++.h>

typedef long long ll;
const int N = 410;
inline bool leap(int year){return year % 400 == 0 || year % 4 == 0 && year % 100 != 0;}

int years[N];
int month[N] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int months[N];

struct Time{
    int year;
    int month;
    int day;
    int hour;
    int minute;
    int second;

    Time():year(0), month(0), day(0), hour(0), minute(0), second(0){}
    Time(int year, int month, int day, int hour, int minute, int second)
        :year(year), month(month), day(day), hour(hour), minute(minute), second(second){}

    int toDay(){
        ll ret = day - 1 + months[month - 1];
        ret += month >= 3 && leap(year);
        ret += (year - 1970) / 400 * years[400] + years[(year - 1970) % 400];
        return ret;
    }

    int toWeek(){
        int ret = (4 + toDay()) % 7;
        return ret ? ret : 7;
    }

    ll toSecond(){
        ll ret = 1ll * toDay() * 86400;
        ret += hour * 3600;
        ret += minute * 60 + second;
        return ret;
    }

    void addDay(){
        second = minute = hour = 0;
        ++ day;
        int limit = ::month[month] + (month == 2 && leap(year));
        if (day > limit){
            ++ month;
            day -= limit;
        }
        if (month > 12){
            month = 1;
            ++ year;
        }
    }
};

Time toTime(ll timeSecond){
    Time nowTime(1970, 1, 1, 0, 0, 0);
    int left = 1970, right = 100000;
    while (left < right){
        int mid = left + right + 1 >> 1;
        nowTime.year = mid;
        if (nowTime.toSecond() > timeSecond){
            right = mid - 1;
        }
        else{
            left = mid;
        }
    }
    nowTime.year = left;
    left = 1, right = 12;
    while (left < right){
        int mid = left + right + 1 >> 1;
        nowTime.month = mid;
        if (nowTime.toSecond() > timeSecond){
            right = mid - 1;
        }
        else{
            left = mid;
        }
    }
    nowTime.month = left;
    nowTime.day += (timeSecond - nowTime.toSecond()) / 86400;
    nowTime.hour = (timeSecond - nowTime.toSecond()) / 3600;
    nowTime.minute = (timeSecond - nowTime.toSecond()) / 60;
    nowTime.second = timeSecond - nowTime.toSecond();
    return nowTime;
}

int main(){
    for (int i = 1970; i < 2370; ++ i){
        years[i - 1970 + 1] = years[i - 1970] + (365 + leap(i));
    }
    for (int i = 1; i <= 12; ++ i){
        months[i] = month[i] + months[i - 1];
    }
    int s, m, h, day, date, month;
    scanf("%d%d%d%d%d%d", &s, &m, &h, &day, &date, &month);
    int test;
    scanf("%d", &test);
    while (test --){
        ll second;
        scanf("%lld", &second);
        ++ second;
        Time nowTime = toTime(second);
        //printf("%d %d %d %d %d %d\n", nowTime.year, nowTime.month, nowTime.day, nowTime.hour, nowTime.minute, nowTime.second);
        if (~h && nowTime.hour > h){
            nowTime.addDay();
        }
        else if (~h && nowTime.hour == h){
           if (~m && nowTime.minute > m){
               nowTime.addDay();
           }
           else if (~s && nowTime.second > s){
               nowTime.addDay();
           }
        }

        while (~month && nowTime.month != month ||
                ~day && ~date && nowTime.toWeek() != day && nowTime.day != date ||
                ~day && nowTime.toWeek() != day || ~date && nowTime.day != date){
            nowTime.addDay();
        }
        while (~h && h > nowTime.hour){
            ++ nowTime.hour;
            nowTime.minute = nowTime.second = 0;
        }
        while (~m && m > nowTime.minute){
            ++ nowTime.minute;
            nowTime.second = 0;
        }
        while (~s && s > nowTime.second){
            ++ nowTime.second;
        }
        //printf("%d %d %d %d %d %d\n", nowTime.year, nowTime.month, nowTime.day, nowTime.hour, nowTime.minute, nowTime.second);
        printf("%lld\n", nowTime.toSecond());
    }
    return 0;
}


#include<bits/stdc++.h>

typedef long long ll;
const int N = 410;
inline bool leap(int year){return year % 400 == 0 || year % 4 == 0 && year % 100 != 0;}

int years[N];
int month[N] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int months[N];

struct Time{
    int year;
    int month;
    int day;
    int hour;
    int minute;
    int second;

    Time():year(0), month(0), day(0), hour(0), minute(0), second(0){}
    Time(int year, int month, int day, int hour, int minute, int second)
        :year(year), month(month), day(day), hour(hour), minute(minute), second(second){}

    int toDay(){
        ll ret = day - 1 + months[month - 1];
        ret += month >= 3 && leap(year);
        ret += (year - 1970) / 400 * years[400] + years[(year - 1970) % 400];
        return ret;
    }

    int toWeek(){
        int ret = (4 + toDay()) % 7;
        return ret ? ret : 7;
    }

    ll toSecond(){
        ll ret = 1ll * toDay() * 86400;
        ret += hour * 3600;
        ret += minute * 60 + second;
        return ret;
    }

    void addDay(){
        second = minute = hour = 0;
        ++ day;
        int limit = ::month[month] + (month == 2 && leap(year));
        if (day > limit){
            ++ month;
            day -= limit;
        }
        if (month > 12){
            month = 1;
            ++ year;
        }
    }
};

Time toTime(ll timeSecond){
    Time nowTime(1970, 1, 1, 0, 0, 0);
    int left = 1970, right = 100000;
    while (left < right){
        int mid = left + right + 1 >> 1;
        nowTime.year = mid;
        if (nowTime.toSecond() > timeSecond){
            right = mid - 1;
        }
        else{
            left = mid;
        }
    }
    nowTime.year = left;
    left = 1, right = 12;
    while (left < right){
        int mid = left + right + 1 >> 1;
        nowTime.month = mid;
        if (nowTime.toSecond() > timeSecond){
            right = mid - 1;
        }
        else{
            left = mid;
        }
    }
    nowTime.month = left;
    nowTime.day += (timeSecond - nowTime.toSecond()) / 86400;
    nowTime.hour = (timeSecond - nowTime.toSecond()) / 3600;
    nowTime.minute = (timeSecond - nowTime.toSecond()) / 60;
    nowTime.second = timeSecond - nowTime.toSecond();
    return nowTime;
}

int main(){
    for (int i = 1970; i < 2370; ++ i){
        years[i - 1970 + 1] = years[i - 1970] + (365 + leap(i));
    }
    for (int i = 1; i <= 12; ++ i){
        months[i] = month[i] + months[i - 1];
    }
    int s, m, h, day, date, month;
    scanf("%d%d%d%d%d%d", &s, &m, &h, &day, &date, &month);
    int test;
    scanf("%d", &test);
    while (test --){
        ll second;
        scanf("%lld", &second);
        ++ second;
        Time nowTime = toTime(second);
        //printf("%d %d %d %d %d %d\n", nowTime.year, nowTime.month, nowTime.day, nowTime.hour, nowTime.minute, nowTime.second);
        if (~h && nowTime.hour > h){
            nowTime.addDay();
        }
        else if (~h && nowTime.hour == h){
           if (~m && nowTime.minute > m){
               nowTime.addDay();
           }
           else if (~s && nowTime.second > s){
               nowTime.addDay();
           }
        }

        while (~month && nowTime.month != month ||
                ~day && ~date && nowTime.toWeek() != day && nowTime.day != date ||
                ~day && !~date && nowTime.toWeek() != day || ~date && !~day && nowTime.day != date){
            nowTime.addDay();
        }
        while (~h && h > nowTime.hour){
            ++ nowTime.hour;
            nowTime.minute = nowTime.second = 0;
        }
        while (~m && m > nowTime.minute){
            ++ nowTime.minute;
            nowTime.second = 0;
        }
        while (~s && s > nowTime.second){
            ++ nowTime.second;
        }
        //printf("%d %d %d %d %d %d\n", nowTime.year, nowTime.month, nowTime.day, nowTime.hour, nowTime.minute, nowTime.second);
        printf("%lld\n", nowTime.toSecond());
    }
    return 0;
}


#include<bits/stdc++.h>

typedef long long ll;
const int N = 410;
inline bool leap(int year){return year % 400 == 0 || year % 4 == 0 && year % 100 != 0;}

int years[N];
int month[N] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int months[N];

struct Time{
    int year;
    int month;
    int day;
    int hour;
    int minute;
    int second;

    Time():year(0), month(0), day(0), hour(0), minute(0), second(0){}
    Time(int year, int month, int day, int hour, int minute, int second)
        :year(year), month(month), day(day), hour(hour), minute(minute), second(second){}

    int toDay(){
        ll ret = day - 1 + months[month - 1];
        ret += month >= 3 && leap(year);
        ret += (year - 1970) / 400 * years[400] + years[(year - 1970) % 400];
        return ret;
    }

    int toWeek(){
        int ret = (4 + toDay()) % 7;
        return ret ? ret : 7;
    }

    ll toSecond(){
        ll ret = 1ll * toDay() * 86400;
        ret += hour * 3600;
        ret += minute * 60 + second;
        return ret;
    }

    void addDay(){
        second = minute = hour = 0;
        ++ day;
        int limit = ::month[month] + (month == 2 && leap(year));
        if (day > limit){
            ++ month;
            day -= limit;
        }
        if (month > 12){
            month = 1;
            ++ year;
        }
    }

    void addSecond(){
        ++ second;
        if (second >= 60){
            second -= 60;
            ++ minute;
        }
        if (minute >= 60){
            minute -= 60;
            ++ hour;
        }
        if (hour >= 24){
            hour -= 24;
            addDay();
        }
    }
};

Time toTime(ll timeSecond){
    Time nowTime(1970, 1, 1, 0, 0, 0);
    int left = 1970, right = 100000;
    while (left < right){
        int mid = left + right + 1 >> 1;
        nowTime.year = mid;
        if (nowTime.toSecond() > timeSecond){
            right = mid - 1;
        }
        else{
            left = mid;
        }
    }
    nowTime.year = left;
    left = 1, right = 12;
    while (left < right){
        int mid = left + right + 1 >> 1;
        nowTime.month = mid;
        if (nowTime.toSecond() > timeSecond){
            right = mid - 1;
        }
        else{
            left = mid;
        }
    }
    nowTime.month = left;
    nowTime.day += (timeSecond - nowTime.toSecond()) / 86400;
    nowTime.hour = (timeSecond - nowTime.toSecond()) / 3600;
    nowTime.minute = (timeSecond - nowTime.toSecond()) / 60;
    nowTime.second = timeSecond - nowTime.toSecond();
    return nowTime;
}

int main(){
    //freopen("input.txt", "r", stdin);
    for (int i = 1970; i < 2370; ++ i){
        years[i - 1970 + 1] = years[i - 1970] + (365 + leap(i));
    }
    for (int i = 1; i <= 12; ++ i){
        months[i] = month[i] + months[i - 1];
    }
    int s, m, h, day, date, month;
    scanf("%d%d%d%d%d%d", &s, &m, &h, &day, &date, &month);
    int test;
    scanf("%d", &test);
    while (test --){
        ll second;
        scanf("%lld", &second);
        ++ second;
        Time nowTime = toTime(second);
        //printf("%d %d %d %d %d %d %d\n", nowTime.year, nowTime.month, nowTime.day, nowTime.hour, nowTime.minute, nowTime.second, nowTime.toWeek());
        while (~h && h != nowTime.hour ||
                ~m && m != nowTime.minute ||
                ~s && s != nowTime.second){
            nowTime.addSecond();
        }
        while (~month && nowTime.month != month ||
                ~day && ~date && nowTime.toWeek() != day && nowTime.day != date ||
                ~day && !~date && nowTime.toWeek() != day || ~date && !~day && nowTime.day != date){
            nowTime.addDay();
        }
        while (~h && h != nowTime.hour ||
                ~m && m != nowTime.minute ||
                ~s && s != nowTime.second){
            nowTime.addSecond();
        }
        //printf("%d %d %d %d %d %d %d\n", nowTime.year, nowTime.month, nowTime.day, nowTime.hour, nowTime.minute, nowTime.second, nowTime.toWeek());
        printf("%lld\n", nowTime.toSecond());
    }
    return 0;
}


#include<bits/stdc++.h>

typedef long long ll;
const int N = 410;
inline bool leap(int year){return year % 400 == 0 || year % 4 == 0 && year % 100 != 0;}

int years[N];
int month[N] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int months[N];

struct Time{
    int year;
    int month;
    int day;
    int hour;
    int minute;
    int second;

    Time():year(0), month(0), day(0), hour(0), minute(0), second(0){}
    Time(int year, int month, int day, int hour, int minute, int second)
        :year(year), month(month), day(day), hour(hour), minute(minute), second(second){}

    int toDay(){
        ll ret = day - 1 + months[month - 1];
        ret += month >= 3 && leap(year);
        ret += (year - 1970) / 400 * years[400] + years[(year - 1970) % 400];
        return ret;
    }

    int toWeek(){
        int ret = (4 + toDay()) % 7;
        return ret ? ret : 7;
    }

    ll toSecond(){
        ll ret = 1ll * toDay() * 86400;
        ret += hour * 3600;
        ret += minute * 60 + second;
        return ret;
    }

    void addDay(){
        second = minute = hour = 0;
        ++ day;
        int limit = ::month[month] + (month == 2 && leap(year));
        if (day > limit){
            ++ month;
            day -= limit;
        }
        if (month > 12){
            month = 1;
            ++ year;
        }
    }
};

Time toTime(ll timeSecond){
    Time nowTime(1970, 1, 1, 0, 0, 0);
    int left = 1970, right = 100000;
    while (left < right){
        int mid = left + right + 1 >> 1;
        nowTime.year = mid;
        if (nowTime.toSecond() > timeSecond){
            right = mid - 1;
        }
        else{
            left = mid;
        }
    }
    nowTime.year = left;
    left = 1, right = 12;
    while (left < right){
        int mid = left + right + 1 >> 1;
        nowTime.month = mid;
        if (nowTime.toSecond() > timeSecond){
            right = mid - 1;
        }
        else{
            left = mid;
        }
    }
    nowTime.month = left;
    nowTime.day += (timeSecond - nowTime.toSecond()) / 86400;
    nowTime.hour = (timeSecond - nowTime.toSecond()) / 3600;
    nowTime.minute = (timeSecond - nowTime.toSecond()) / 60;
    nowTime.second = timeSecond - nowTime.toSecond();
    return nowTime;
}

int main(){
    for (int i = 1970; i < 2370; ++ i){
        years[i - 1970 + 1] = years[i - 1970] + (365 + leap(i));
    }
    for (int i = 1; i <= 12; ++ i){
        months[i] = month[i] + months[i - 1];
    }
    int s, m, h, day, date, month;
    scanf("%d%d%d%d%d%d", &s, &m, &h, &day, &date, &month);
    int test;
    scanf("%d", &test);
    while (test --){
        ll second;
        scanf("%lld", &second);
        ++ second;
        Time nowTime = toTime(second);
        //printf("%d %d %d %d %d %d\n", nowTime.year, nowTime.month, nowTime.day, nowTime.hour, nowTime.minute, nowTime.second);
        if (~h && nowTime.hour > h){
            nowTime.addDay();
        }
        else if (nowTime.hour == h){
           if (~m && nowTime.minute > m){
               nowTime.addDay();
           }
           else if (nowTime.minute == m){
               if (~s && nowTime.second > s){
                    nowTime.addDay();
               }
           }
        }

        while (~month && nowTime.month != month ||
                ~day && ~date && nowTime.toWeek() != day && nowTime.day != date ||
                ~day && !~date && nowTime.toWeek() != day || ~date && !~day && nowTime.day != date){
            nowTime.addDay();
        }
        if (~m && nowTime.minute > m){
            ++ nowTime.hour;
            nowTime.minute = nowTime.second = 0;
        }
        else if (nowTime.minute == m){
            if (~s && nowTime.second > s){
                ++ nowTime.hour;
                nowTime.minute = nowTime.second = 0;
            }
        }
        while (~h && h > nowTime.hour){
            ++ nowTime.hour;
            nowTime.minute = nowTime.second = 0;
        }
        if (~s && nowTime.second > s){
            ++ nowTime.minute;
            nowTime.second = 0;
        }
        while (~m && m > nowTime.minute){
            ++ nowTime.minute;
            nowTime.second = 0;
        }
        while (~s && s > nowTime.second){
            ++ nowTime.second;
        }
        //printf("%d %d %d %d %d %d\n", nowTime.year, nowTime.month, nowTime.day, nowTime.hour, nowTime.minute, nowTime.second);
        printf("%lld\n", nowTime.toSecond());
    }
    return 0;
}


#include <algorithm>
#include <iostream>
#include <cstdio>
#include <vector>
#define pb push_back
using namespace std;
typedef long long ll;
int month_day[][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}
};
#ifdef ONLINE_JUDGE
#define read_ll(x) do {scanf("%I64d", &x); } while (false)
#define print_ll(x)  do {printf("%I64d\n", x); } while (false)
#else
#define read_ll(x) do {scanf("%lld", &x); } while (false)
#define print_ll(x) do {printf("%lld\n", x); } while (false)
#endif
vector<ll> ok1, ok2;
int main()
{
	// freopen("698E.in", "r", stdin);
	int s, m, h, day, date, month;
	scanf("%d%d%d%d%d%d", &s, &m, &h, &day, &date, &month);
	if (day != -1)
		day--;
	if (date != -1)
		date--;
	if (month != -1)
		month--;
	ll x1 = 0;
	for (int h2 = 0; h2 < 24; h2++)
	{
		for (int m2 = 0; m2 < 60; m2++)
		{
			for (int s2 = 0; s2 < 60; s2++, x1++)
			{
				if ((s == -1 || s == s2) && (m == -1 || m == m2) && (h == -1 || h == h2))
					ok1.pb(x1);
			}
		}
	}
	// cout << "ok" << endl;
	int day2 = 3;
	ll x2 = 0;
	for (int year = 1970; year < 2370; year++)
	{
		bool leap = !(year % 400) || ((year % 100) && !(year % 4));
		for (int month2 = 0; month2 < 12; month2++)
		{
			for (int date2 = 0; date2 < month_day[leap][month2]; date2++, x2++)
			{
				if (-1 == month || month == month2)
				{
					if ((date != -1 && date == date2) || (day != -1 && day == day2) || (date == -1 && day == -1))
						ok2.pb(x2);
				}
				day2 = (day2 + 1) % 7;
			}
		}
	}
	// cout << "ok" << endl;
	int n;
	scanf("%d", &n);
	while (n--)
	{
		ll x;
		read_ll(x);
		ll whole_year = x / x1 / x2;
		ll remain_time = x % (x1 * x2);
		ll whole_day = remain_time / x1;
		remain_time %= x1;
		// print_ll(remain_time);
		// printf("%d\n", binary_search(ok2.begin(), ok2.end(), whole_day));
		// print_ll(ok1.back());
		if (whole_day > ok2.back() || (whole_day == ok2.back() && remain_time >= ok1.back()))
		{
			// cout << "case 1" << endl;
			print_ll((whole_year + 1) * x1 * x2 + ok2[0] * x1 + ok1[0]);
		}
		else if (remain_time >= ok1.back() || !binary_search(ok2.begin(), ok2.end(), whole_day))
		{
			// cout << "case 2" << endl;
			print_ll(whole_year * x1 * x2 + *upper_bound(ok2.begin(), ok2.end(), whole_day) * x1 + ok1[0]);
		}
		else
		{
			// cout << "case 3" << endl;
			print_ll(whole_year * x1 * x2 + whole_day * x1 + *upper_bound(ok1.begin(), ok1.end(), remain_time));
		}
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

int mtot[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool is_leap(int y) {
  if (y % 4 != 0) return false;
  if (y % 100 == 0) return y % 400 == 0;
  return true;
}

struct Time {
  int s, m, h;
  int dw, dt, mn, y;
  LL ts;
  int c;
  void setPattern(int s_, int m_, int h_,
		  int dw_, int dt_, int mn_) {
    s = s_, m = m_, h = h_;
    dw = dw_, dt = dt_, mn = mn_;
  }

  void increase_day(bool upd = true) {
    if (upd) {
      dw++; if (dw == 8) dw = 1;
    }
    if (is_leap(y)) mtot[2]++;

    if (dt == mtot[mn]) {
      dt = 1, mn++;
    } else dt++;
    if (is_leap(y)) mtot[2]--;

    if (mn > 12) mn = 1, y++;
    ts += 24 * 60 * 60;
  }
  void increase_sec() {
    s++;
    if (s == 60) s = 0, m++;
    if (m == 60) m = 0, h++;
    if (h == 24) h = 0, increase_day(), ts -= 24 * 60 * 60;
    ts++;
  }
  bool match_aux(const Time &pat) {
    if (pat.mn != -1 && pat.mn != mn) return false;
    if (pat.dw != -1 && pat.dt != -1) {
      return (pat.dw == dw || pat.dt == dt);
    }
    bool m1 = (pat.dw == -1 || pat.dw == dw);
    bool m2 = (pat.dt == -1 || pat.dt == dt);
    return m1 && m2;
  }
  bool match_time_aux(const Time &pat) {
    if (pat.h != -1 && pat.h != h) return false;
    if (pat.m != -1 && pat.m != m) return false;
    if (pat.s != -1 && pat.s != s) return false;
    return true;
  }
  bool match(const Time &pat) {
    while (!match_time_aux(pat)) increase_sec(), c++;
    while (!match_aux(pat)) increase_day(), c++;

    if (c == 0) {
      increase_sec();
      while (!match_time_aux(pat)) increase_sec(), c++;
      while (!match_aux(pat)) increase_day(), c++;
    }
    return true;
  }

  void parse(LL unixTS) {
    c = 0;
    LL temp = unixTS;
    s = unixTS % 60; unixTS /= 60;
    m = unixTS % 60; unixTS /= 60;
    h = unixTS % 24; unixTS /= 24;
    dw = (unixTS + 3) % 7 + 1;
    if (dw >= 8) dw -= 7;

    y = 1970;
    while (unixTS > 366) {
      if (is_leap(y)) unixTS--;
      unixTS -= 365;
      y++;
    }
    dt = 1; mn = 1;
    while (unixTS--) increase_day(false);
    ts = temp;
  }

  LL invert() { return ts; }
  void print() {
    return;
    fprintf(stderr, "> %02d-%02d-%4d (%d) %02d:%02d:%02d\n",
	    dt, mn, y, dw, h, m, s);
  }
};

int main() {
  ios_base::sync_with_stdio(false); cin.tie(NULL);

  int s, m, h, dw, dt, mn;
  cin >> s >> m >> h >> dw >> dt >> mn;
  Time pat;
  pat.setPattern(s, m, h, dw, dt, mn);

  int n; cin >> n;
  while (n--) {
    LL val; cin >> val;
    Time ans;
    ans.parse(val);
    ans.print();
    ans.match(pat);
    LL ret = ans.invert();
    ans.print();

    ans.parse(val);
    ans.increase_day();
    ans.ts -= (3600 * ans.h + 60 * ans.m + ans.s);
    ans.h = ans.s = ans.m = 0;
    ans.c++;
    ans.match(pat);
    ret = min(ret, ans.invert());
    cout << ret << '\n';
  }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

int mtot[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool is_leap(int y) {
  if (y % 4 != 0) return false;
  if (y % 100 == 0) return y % 400 == 0;
  return true;
}

struct Time {
  int s, m, h;
  int dw, dt, mn, y;
  LL ts;
  void setPattern(int s_, int m_, int h_,
		  int dw_, int dt_, int mn_) {
    s = s_, m = m_, h = h_;
    dw = dw_, dt = dt_, mn = mn_;
  }

  void increase_day(bool upd = true) {
    if (upd) {
      dw++; if (dw == 8) dw = 1;
    }
    if (is_leap(y)) mtot[2]++;

    if (dt == mtot[mn]) {
      dt = 1, mn++;
    } else dt++;
    if (is_leap(y)) mtot[2]--;

    if (mn > 12) mn = 1, y++;
    ts += 24 * 60 * 60;
  }
  void increase_sec() {
    s++;
    if (s == 60) s = 0, m++;
    if (m == 60) m = 0, h++;
    if (h == 24) h = 0, increase_day(), ts -= 24 * 60 * 60;
    ts++;
  }
  bool match_aux(const Time &pat) {
    if (pat.m != -1 && pat.m != m) return false;
    bool m1 = (pat.dw == -1 || pat.dw == dw);
    bool m2 = (pat.dt == -1 || pat.dt == dt);
    return m1 || m2;
  }
  bool match_time_aux(const Time &pat) {
    if (pat.h != -1 && pat.h != h) return false;
    if (pat.m != -1 && pat.m != m) return false;
    if (pat.s != -1 && pat.s != s) return false;
    return true;
  }
  bool match(const Time &pat) {
    int c = 0;
    while (!match_time_aux(pat)) increase_sec(), c++;
    // cerr << "INC_SEC: " << c << endl;
    c = 0;
    while (!match_aux(pat)) increase_day(), c++;
    // cerr << "INC_DAY: " << c << endl;

    return true;
  }

  void parse(LL unixTS) {
    LL temp = unixTS;
    s = unixTS % 60; unixTS /= 60;
    m = unixTS % 60; unixTS /= 60;
    h = unixTS % 24; unixTS /= 24;
    dw = (unixTS + 3) % 7 + 1;
    if (dw >= 8) dw -= 7;

    y = 1970;
    while (unixTS > 366) {
      if (is_leap(y)) unixTS--;
      unixTS -= 365;
      y++;
    }
    dt = 1; mn = 1;
    while (unixTS--) increase_day(false);
    ts = temp;
  }

  LL invert() { return ts; }
  void print() {
    return;
    fprintf(stderr, "> %02d-%02d-%4d (%d) %02d:%02d:%02d\n",
	    dt, mn, y, dw, h, m, s);
  }
};

int main() {
  ios_base::sync_with_stdio(false); cin.tie(NULL);

  int s, m, h, dw, dt, mn;
  cin >> s >> m >> h >> dw >> dt >> mn;
  Time pat;
  pat.setPattern(s, m, h, dw, dt, mn);

  int n; cin >> n;
  while (n--) {
    LL val; cin >> val;
    Time ans;
    ans.parse(val);
    ans.print();
    // assert(ans.ts == val);
    ans.increase_sec();
    ans.match(pat);
    cout << ans.invert() << '\n';
    ans.print();
  }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

int mtot[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool is_leap(int y) {
  if (y % 4 != 0) return false;
  if (y % 100 == 0) return y % 400 == 0;
  return true;
}

struct Time {
  int s, m, h;
  int dw, dt, mn, y;
  LL ts;
  void setPattern(int s_, int m_, int h_,
		  int dw_, int dt_, int mn_) {
    s = s_, m = m_, h = h_;
    dw = dw_, dt = dt_, mn = mn_;
  }

  void increase_day(bool upd = true) {
    if (upd) {
      dw++; if (dw == 8) dw = 1;
    }
    if (is_leap(y)) mtot[2]++;

    if (dt == mtot[mn]) {
      dt = 1, mn++;
    } else dt++;
    if (is_leap(y)) mtot[2]--;

    if (mn > 12) mn = 1, y++;
    ts += 24 * 60 * 60;
  }
  void increase_sec() {
    s++;
    if (s == 60) s = 0, m++;
    if (m == 60) m = 0, h++;
    if (h == 24) h = 0, increase_day(), ts -= 24 * 60 * 60;
    ts++;
  }
  bool match_aux(const Time &pat) {
    if (pat.mn != -1 && pat.mn != mn) return false;
    if (pat.dw != -1 && pat.dt != -1) {
      return (pat.dw == dw || pat.dt == dt);
    }
    bool m1 = (pat.dw == -1 || pat.dw == dw);
    bool m2 = (pat.dt == -1 || pat.dt == dt);
    return m1 && m2;
  }
  bool match_time_aux(const Time &pat) {
    if (pat.h != -1 && pat.h != h) return false;
    if (pat.m != -1 && pat.m != m) return false;
    if (pat.s != -1 && pat.s != s) return false;
    return true;
  }
  bool match(const Time &pat) {
    int c = 0;
    while (!match_time_aux(pat)) increase_sec(), c++;
    // cerr << "INC_SEC: " << c << endl;
    c = 0;
    while (!match_aux(pat)) increase_day(), c++;
    // cerr << "INC_DAY: " << c << endl;

    return true;
  }

  void parse(LL unixTS) {
    LL temp = unixTS;
    s = unixTS % 60; unixTS /= 60;
    m = unixTS % 60; unixTS /= 60;
    h = unixTS % 24; unixTS /= 24;
    dw = (unixTS + 3) % 7 + 1;
    if (dw >= 8) dw -= 7;

    y = 1970;
    while (unixTS > 366) {
      if (is_leap(y)) unixTS--;
      unixTS -= 365;
      y++;
    }
    dt = 1; mn = 1;
    while (unixTS--) increase_day(false);
    ts = temp;
  }

  LL invert() { return ts; }
  void print() {
    return;
    fprintf(stderr, "> %02d-%02d-%4d (%d) %02d:%02d:%02d\n",
	    dt, mn, y, dw, h, m, s);
  }
};

int main() {
  ios_base::sync_with_stdio(false); cin.tie(NULL);

  int s, m, h, dw, dt, mn;
  cin >> s >> m >> h >> dw >> dt >> mn;
  Time pat;
  pat.setPattern(s, m, h, dw, dt, mn);

  int n; cin >> n;
  while (n--) {
    LL val; cin >> val;
    Time ans;
    ans.parse(val);
    ans.print();
    // assert(ans.ts == val);
    ans.increase_sec();
    ans.match(pat);
    cout << ans.invert() << '\n';
    ans.print();
  }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

int mtot[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool is_leap(int y) {
  if (y % 4 != 0) return false;
  if (y % 100 == 0) return y % 400 == 0;
  return true;
}

struct Time {
  int s, m, h;
  int dw, dt, mn, y;
  LL ts;
  void parse(LL unixTS) {
    char *buff = new char[201];
    time_t raw = unixTS;
    struct tm * ctm;
    ctm = gmtime(&raw);
    s = ctm->tm_sec; m = ctm->tm_min; h = ctm->tm_hour;
    dw = ctm->tm_wday; if (dw == 0) dw = 7;
    dt = ctm->tm_mday; mn = ctm->tm_mon + 1;
    ts = unixTS;
  }
  void setPattern(int s_, int m_, int h_,
		  int dw_, int dt_, int mn_) {
    s = s_, m = m_, h = h_;
    dw = dw_, dt = dt_, mn = mn_;
  }

  void increase_day() {
    dw++; if (dw == 8) dw = 1;
    if (is_leap(y)) mtot[2]++;

    if (dt == mtot[mn]) {
      dt = 1, mn++;
    } else dt++;
    if (is_leap(y)) mtot[2]--;

    if (mn > 12) mn = 1, y++;
    ts += 24 * 60 * 60;
  }
  void increase_sec() {
    s++;
    if (s == 60) s = 0, m++;
    if (m == 60) m = 0, h++;
    if (h == 24) h = 0, increase_day(), ts -= 24 * 60 * 60;
    ts++;
  }
  bool match_aux(const Time &pat) {
    if (pat.m != -1 && pat.m != m) return false;
    bool m1 = (pat.dw == -1 || pat.dw == dw);
    bool m2 = (pat.dt == -1 || pat.dt == dt);
    return m1 || m2;
  }
  bool match_time_aux(const Time &pat) {
    if (pat.h != -1 && pat.h != h) return false;
    if (pat.m != -1 && pat.m != m) return false;
    if (pat.s != -1 && pat.s != s) return false;
    return true;
  }
  bool match(const Time &pat) {
    int c = 0;
    while (!match_time_aux(pat)) increase_sec(), c++;
    // cerr << "INC_SEC: " << c << endl;
    c = 0;
    while (!match_aux(pat)) increase_day(), c++;
    // cerr << "INC_DAY: " << c << endl;

    return true;
  }

  LL invert() { return ts; }
};

int main() {
  ios_base::sync_with_stdio(false); cin.tie(NULL);

  int s, m, h, dw, dt, mn;
  cin >> s >> m >> h >> dw >> dt >> mn;
  Time pat;
  pat.setPattern(s, m, h, dw, dt, mn);

  int n; cin >> n;
  while (n--) {
    LL val; cin >> val;
    Time ans;
    ans.parse(val);
    assert(ans.ts == val);
    ans.increase_sec();
    ans.match(pat);
    cout << ans.invert() << '\n';
  }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

int mtot[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool is_leap(int y) {
  if (y % 4 != 0) return false;
  if (y % 100 == 0) return y % 400 == 0;
  return true;
}

struct Time {
  int s, m, h;
  int dw, dt, mn, y;
  LL ts;
  void setPattern(int s_, int m_, int h_,
		  int dw_, int dt_, int mn_) {
    s = s_, m = m_, h = h_;
    dw = dw_, dt = dt_, mn = mn_;
  }

  void increase_day(bool upd = true) {
    if (upd) {
      dw++; if (dw == 8) dw = 1;
    }
    if (is_leap(y)) mtot[2]++;

    if (dt == mtot[mn]) {
      dt = 1, mn++;
    } else dt++;
    if (is_leap(y)) mtot[2]--;

    if (mn > 12) mn = 1, y++;
    ts += 24 * 60 * 60;
  }
  void increase_sec() {
    s++;
    if (s == 60) s = 0, m++;
    if (m == 60) m = 0, h++;
    if (h == 24) h = 0, increase_day(), ts -= 24 * 60 * 60;
    ts++;
  }
  bool match_aux(const Time &pat) {
    if (pat.mn != -1 && pat.mn != mn) return false;
    if (pat.dw != -1 && pat.dt != -1) {
      return (pat.dw == dw || pat.dt == dt);
    }
    bool m1 = (pat.dw == -1 || pat.dw == dw);
    bool m2 = (pat.dt == -1 || pat.dt == dt);
    return m1 && m2;
  }
  bool match_time_aux(const Time &pat) {
    if (pat.h != -1 && pat.h != h) return false;
    if (pat.m != -1 && pat.m != m) return false;
    if (pat.s != -1 && pat.s != s) return false;
    return true;
  }
  bool match(const Time &pat) {
    int c = 0;
    while (!match_time_aux(pat)) increase_sec(), c++;
    while (!match_aux(pat)) increase_day(), c++;

    if (c == 0) {
      increase_sec();
      while (!match_time_aux(pat)) increase_sec(), c++;
      while (!match_aux(pat)) increase_day(), c++;
    }
    return true;
  }

  void parse(LL unixTS) {
    LL temp = unixTS;
    s = unixTS % 60; unixTS /= 60;
    m = unixTS % 60; unixTS /= 60;
    h = unixTS % 24; unixTS /= 24;
    dw = (unixTS + 3) % 7 + 1;
    if (dw >= 8) dw -= 7;

    y = 1970;
    while (unixTS > 366) {
      if (is_leap(y)) unixTS--;
      unixTS -= 365;
      y++;
    }
    dt = 1; mn = 1;
    while (unixTS--) increase_day(false);
    ts = temp;
  }

  LL invert() { return ts; }
  void print() {
    return;
    fprintf(stderr, "> %02d-%02d-%4d (%d) %02d:%02d:%02d\n",
	    dt, mn, y, dw, h, m, s);
  }
};

int main() {
  ios_base::sync_with_stdio(false); cin.tie(NULL);

  int s, m, h, dw, dt, mn;
  cin >> s >> m >> h >> dw >> dt >> mn;
  Time pat;
  pat.setPattern(s, m, h, dw, dt, mn);

  int n; cin >> n;
  while (n--) {
    LL val; cin >> val;
    Time ans;
    ans.parse(val);
    ans.print();
    ans.match(pat);
    cout << ans.invert() << '\n';
    ans.print();
  }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

int mtot[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool is_leap(int y) {
  if (y % 4 != 0) return false;
  if (y % 100 == 0) return y % 400 == 0;
  return true;
}

struct Time {
  int s, m, h;
  int dw, dt, mn, y;
  LL ts;
  void parse(LL unixTS) {
    char *buff = new char[201];
    time_t raw = unixTS;
    struct tm * ctm;
    ctm = gmtime(&raw);
    s = ctm->tm_sec; m = ctm->tm_min; h = ctm->tm_hour;
    dw = ctm->tm_wday; if (dw == 0) dw = 7;
    dt = ctm->tm_mday; mn = ctm->tm_mon + 1;
    ts = unixTS;
  }
  void setPattern(int s_, int m_, int h_,
		  int dw_, int dt_, int mn_) {
    s = s_, m = m_, h = h_;
    dw = dw_, dt = dt_, mn = mn_;
  }

  void increase_day() {
    dw++; if (dw == 8) dw = 1;
    if (is_leap(y)) mtot[2]++;

    if (dt == mtot[mn]) {
      dt = 1, mn++;
    } else dt++;
    if (is_leap(y)) mtot[2]--;

    if (mn > 12) mn = 1, y++;
    ts += 24 * 60 * 60;
  }
  void increase_sec() {
    s++;
    if (s == 60) s = 0, m++;
    if (m == 60) m = 0, h++;
    if (h == 24) h = 0, increase_day(), ts -= 24 * 60 * 60;
    ts++;
  }
  bool match_aux(const Time &pat) {
    if (pat.m != -1 && pat.m != m) return false;
    bool m1 = (pat.dw == -1 || pat.dw == dw);
    bool m2 = (pat.dt == -1 || pat.dt == dt);
    return m1 || m2;
  }
  bool match_time_aux(const Time &pat) {
    if (pat.h != -1 && pat.h != h) return false;
    if (pat.m != -1 && pat.m != m) return false;
    if (pat.s != -1 && pat.s != s) return false;
    return true;
  }
  bool match(const Time &pat) {
    int c = 0;
    while (!match_time_aux(pat)) increase_sec(), c++;
    // cerr << "INC_SEC: " << c << endl;
    c = 0;
    while (!match_aux(pat)) increase_day(), c++;
    // cerr << "INC_DAY: " << c << endl;

    return true;
  }

  LL invert() { return ts; }
};

int main() {
  ios_base::sync_with_stdio(false); cin.tie(NULL);

  cout << sizeof(time_t) << endl;

  int s, m, h, dw, dt, mn;
  cin >> s >> m >> h >> dw >> dt >> mn;
  Time pat;
  pat.setPattern(s, m, h, dw, dt, mn);

  int n; cin >> n;
  while (n--) {
    LL val; cin >> val;
    Time ans;
    ans.parse(val);
    // assert(ans.ts == val);
    ans.increase_sec();
    ans.match(pat);
    cout << ans.invert() << '\n';
  }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

int mtot[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool is_leap(int y) {
  if (y % 4 != 0) return false;
  if (y % 100 == 0) return y % 400 == 0;
  return true;
}

struct Time {
  int s, m, h;
  int dw, dt, mn, y;
  LL ts;
  void parse(LL unixTS) {
    char *buff = new char[201];
    time_t raw = unixTS;
    struct tm * ctm;
    ctm = gmtime(&raw);
    s = ctm->tm_sec; m = ctm->tm_min; h = ctm->tm_hour;
    dw = ctm->tm_wday; if (dw == 0) dw = 7;
    dt = ctm->tm_mday; mn = ctm->tm_mon + 1;
    ts = unixTS;
  }
  void setPattern(int s_, int m_, int h_,
		  int dw_, int dt_, int mn_) {
    s = s_, m = m_, h = h_;
    dw = dw_, dt = dt_, mn = mn_;
  }

  void increase_day() {
    dw++; if (dw == 8) dw = 1;
    if (is_leap(y)) mtot[2]++;

    if (dt == mtot[mn]) {
      dt = 1, mn++;
    } else dt++;
    if (is_leap(y)) mtot[2]--;

    if (mn > 12) mn = 1, y++;
    ts += 24 * 60 * 60;
  }
  void increase_sec() {
    s++;
    if (s == 60) s = 0, m++;
    if (m == 60) m = 0, h++;
    if (h == 24) h = 0, increase_day(), ts -= 24 * 60 * 60;
    ts++;
  }
  bool match_aux(const Time &pat) {
    if (pat.m != -1 && pat.m != m) return false;
    bool m1 = (pat.dw == -1 || pat.dw == dw);
    bool m2 = (pat.dt == -1 || pat.dt == dt);
    return m1 || m2;
  }
  bool match_time_aux(const Time &pat) {
    if (pat.h != -1 && pat.h != h) return false;
    if (pat.m != -1 && pat.m != m) return false;
    if (pat.s != -1 && pat.s != s) return false;
    return true;
  }
  bool match(const Time &pat) {
    int c = 0;
    while (!match_time_aux(pat)) increase_sec(), c++;
    // cerr << "INC_SEC: " << c << endl;
    c = 0;
    while (!match_aux(pat)) increase_day(), c++;
    // cerr << "INC_DAY: " << c << endl;

    return true;
  }

  LL invert() { return ts; }
};

int main() {
  ios_base::sync_with_stdio(false); cin.tie(NULL);

  int s, m, h, dw, dt, mn;
  cin >> s >> m >> h >> dw >> dt >> mn;
  Time pat;
  pat.setPattern(s, m, h, dw, dt, mn);

  int n; cin >> n;
  while (n--) {
    LL val; cin >> val;
    Time ans;
    ans.parse(val);
    // assert(ans.ts == val);
    ans.increase_sec();
    ans.match(pat);
    cout << ans.invert() << '\n';
  }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

int mtot[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool is_leap(int y) {
  if (y % 4 != 0) return false;
  if (y % 100 == 0) return y % 400 == 0;
  return true;
}

struct Time {
  int s, m, h;
  int dw, dt, mn, y;
  LL ts;
  void parse(LL unixTS) {
    char *buff = new char[201];
    time_t raw = unixTS;
    struct tm * ctm;
    ctm = gmtime(&raw);
    s = ctm->tm_sec; m = ctm->tm_min; h = ctm->tm_hour;
    dw = ctm->tm_wday; if (dw == 0) dw = 7;
    dt = ctm->tm_mday; mn = ctm->tm_mon + 1;
    ts = unixTS;
  }
  void setPattern(int s_, int m_, int h_,
		  int dw_, int dt_, int mn_) {
    s = s_, m = m_, h = h_;
    dw = dw_, dt = dt_, mn = mn_;
  }

  void increase_day() {
    dw++; if (dw == 8) dw = 1;
    if (is_leap(y)) mtot[2]++;

    if (dt == mtot[mn]) {
      dt = 1, mn++;
    } else dt++;
    if (is_leap(y)) mtot[2]--;

    if (mn > 12) mn = 1, y++;
    ts += 24 * 60 * 60;
  }
  void increase_sec() {
    s++;
    if (s == 60) s = 0, m++;
    if (m == 60) m = 0, h++;
    if (h == 24) h = 0, increase_day(), ts -= 24 * 60 * 60;
    ts++;
  }
  bool match_aux(const Time &pat) {
    if (pat.m != -1 && pat.m != m) return false;
    bool m1 = (pat.dw == -1 || pat.dw == dw);
    bool m2 = (pat.dt == -1 || pat.dt == dt);
    return m1 || m2;
  }
  bool match_time_aux(const Time &pat) {
    if (pat.h != -1 && pat.h != h) return false;
    if (pat.m != -1 && pat.m != m) return false;
    if (pat.s != -1 && pat.s != s) return false;
    return true;
  }
  bool match(const Time &pat) {
    int c = 0;
    while (!match_time_aux(pat)) increase_sec(), c++;
    // cerr << "INC_SEC: " << c << endl;
    c = 0;
    while (!match_aux(pat)) increase_day(), c++;
    // cerr << "INC_DAY: " << c << endl;

    return true;
  }

  LL invert() { return ts; }
};

int main() {
  ios_base::sync_with_stdio(false); cin.tie(NULL);

  cerr << sizeof(time_t) << endl;

  int s, m, h, dw, dt, mn;
  cin >> s >> m >> h >> dw >> dt >> mn;
  Time pat;
  pat.setPattern(s, m, h, dw, dt, mn);

  int n; cin >> n;
  while (n--) {
    LL val; cin >> val;
    Time ans;
    ans.parse(val);
    // assert(ans.ts == val);
    ans.increase_sec();
    ans.match(pat);
    cout << ans.invert() << '\n';
  }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

int mtot[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool is_leap(int y) {
  if (y % 4 != 0) return false;
  if (y % 100 == 0) return y % 400 == 0;
  return true;
}

struct Time {
  int s, m, h;
  int dw, dt, mn, y;
  LL ts;
  void setPattern(int s_, int m_, int h_,
		  int dw_, int dt_, int mn_) {
    s = s_, m = m_, h = h_;
    dw = dw_, dt = dt_, mn = mn_;
  }

  void increase_day() {
    dw++; if (dw == 8) dw = 1;
    if (is_leap(y)) mtot[2]++;

    if (dt == mtot[mn]) {
      dt = 1, mn++;
    } else dt++;
    if (is_leap(y)) mtot[2]--;

    if (mn > 12) mn = 1, y++;
    ts += 24 * 60 * 60;
  }
  void increase_sec() {
    s++;
    if (s == 60) s = 0, m++;
    if (m == 60) m = 0, h++;
    if (h == 24) h = 0, increase_day(), ts -= 24 * 60 * 60;
    ts++;
  }
  bool match_aux(const Time &pat) {
    if (pat.m != -1 && pat.m != m) return false;
    bool m1 = (pat.dw == -1 || pat.dw == dw);
    bool m2 = (pat.dt == -1 || pat.dt == dt);
    return m1 || m2;
  }
  bool match_time_aux(const Time &pat) {
    if (pat.h != -1 && pat.h != h) return false;
    if (pat.m != -1 && pat.m != m) return false;
    if (pat.s != -1 && pat.s != s) return false;
    return true;
  }
  bool match(const Time &pat) {
    int c = 0;
    while (!match_time_aux(pat)) increase_sec(), c++;
    // cerr << "INC_SEC: " << c << endl;
    c = 0;
    while (!match_aux(pat)) increase_day(), c++;
    // cerr << "INC_DAY: " << c << endl;

    return true;
  }

  void parse(LL unixTS) {
    LL temp = unixTS;
    s = unixTS % 60; unixTS /= 60;
    m = unixTS % 60; unixTS /= 60;
    h = unixTS % 24; unixTS /= 24;
    dw = unixTS % 7 + 4;
    if (dw >= 8) dw -= 7;

    y = 1970;
    while (unixTS > 366) {
      if (is_leap(y)) unixTS--;
      unixTS -= 365;
      y++;
    }
    dt = 1; mn = 1;
    while (unixTS--) increase_day();
    ts = temp;
  }

  LL invert() { return ts; }
};

int main() {
  ios_base::sync_with_stdio(false); cin.tie(NULL);

  int s, m, h, dw, dt, mn;
  cin >> s >> m >> h >> dw >> dt >> mn;
  Time pat;
  pat.setPattern(s, m, h, dw, dt, mn);

  int n; cin >> n;
  while (n--) {
    LL val; cin >> val;
    Time ans;
    ans.parse(val);
    // assert(ans.ts == val);
    ans.increase_sec();
    ans.match(pat);
    cout << ans.invert() << '\n';
  }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for(int i = (a); i < int(b); ++i)
#define rrep(i, a, b) for(int i = (a) - 1; i >= int(b); --i)
#define trav(it, v) for(typeof((v).begin()) it = (v).begin(); it != (v).end(); ++it)
#define all(v) (v).begin(), (v).end()
#define what_is(x) cerr << #x << " is " << x << endl;

typedef double fl;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;

int numLeapYears(int to){
	int ret = (to-1)/4-(to-1)/100+(to-1)/400;
	return ret;
}

int numLeapYears(int from, int to){
	return numLeapYears(to)-numLeapYears(from);
}

ll year2Time(ll year){
	return (year*365+numLeapYears(year))*86400;
}

ll month2Time(int month, int year){
	int ret=0;
	if(month > 1)ret += 31;
	if(month > 2){
		if(numLeapYears(year+1)-numLeapYears(year) == 1)
			ret += 29;
		else
			ret += 28;
	}
	if(month > 3)ret += 31;
	if(month > 4)ret += 30;
	if(month > 5)ret += 31;
	if(month > 6)ret += 30;
	if(month > 7)ret += 31;
	if(month > 8)ret += 31;
	if(month > 9)ret += 30;
	if(month > 10)ret += 31;
	if(month > 11)ret += 30;
	if(month > 12)ret += 31;
	return ret*86400;
}

struct Date{
	int s, m, h, D, M, Y;

	Date(){
		s=0;
		m=0;
		h=0;
		D=1;
		M=1;
		Y=1970;
	}

	ll toTime(){
		ll ret=year2Time(Y);
		ret += month2Time(M, Y);
		ret += (D-1)*86400;
		ret += h*3600;
		ret += m*60;
		ret += s;
		return ret;
	}

	int getWeekday(){
		ll t=toTime();
		ll days=t/86400;
		return ((days+6)%7)+1;
	}

	void removeD(){
	}

	void removeHour(){
		--h;
		if(h < 0){
			h += 60;
			removeD();
		}
	}

	void removeMinute(){
		--m;
		if(m < 0){
			m += 60;
			removeHour();
		}
	}

	void removeSecond(){
		--s;
		if(s < 0){
			s += 60;
			removeMinute();
		}
	}
};

int s, m, h, day, date, month;

bool matches(Date d){
	if(s != -1 && s != d.s)
		return 0;
	if(m != -1 && m != d.m)
		return 0;
	if(h != -1 && h != d.h)
		return 0;
	if(month != -1 && month != d.M)
		return 0;
	bool dayMatches=(date == d.D);
	bool dateMatches=(day == d.getWeekday());
	if(dayMatches || dateMatches || (day == -1 && date == -1))
		return 1;
	return 0;
}

ll solve(ll t){
	Date cur;
	t += cur.toTime();
	cur.Y=t/(366*86400);
	while(cur.toTime()+86400*367 < t)
		++cur.Y;
	if(s == -1)
		cur.s=59;
	else
		cur.s=s;
	if(m == -1)
		cur.m=59;
	else
		cur.m=m;
	if(h == -1)
		cur.h=23;
	else
		cur.h=h;
	while(true){
		ll curT=cur.toTime();
		if(curT > t && matches(cur)){
			cur.s=0;
			cur.m=0;
			cur.h=0;
			while(cur.toTime()+4000 < t)
				++cur.h;
			while(true){
				curT=cur.toTime();
				if(curT > t && matches(cur)){
					return curT;
				}
				++cur.s;
				if(cur.s == 60){
					cur.s=0;
					++cur.m;
				}
				if(cur.m == 60){
					cur.m=0;
					++cur.h;
				}
			}
			assert(0);
		}
		cur.D++;
		Date nextMonth=cur;
		nextMonth.M++;
		nextMonth.D=1;
		if(nextMonth.toTime() == curT+86400){
			cur=nextMonth;
			if(nextMonth.M == 13){
				cur.M=1;
				++cur.Y;
			}
		}
	}
}

int main(){
	Date d;
	/*d.D=19;
	d.M=7;
	d.Y=2016;*/
	int tmp=d.getWeekday();
	ll start=d.toTime();
	scanf("%d%d%d%d%d%d", &s, &m, &h, &day, &date, &month);
	int n;
	scanf("%d", &n);
	while(n--){
		ll t;
		scanf("%I64d", &t);
		printf("%I64d\n", (solve(t)-start));
	}
}


#include <bits/stdc++.h>

using namespace std;

#define F first
#define S second
#define pb push_back
#define mp make_pair

int main() {
    ios_base::sync_with_stdio(0);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif // LOCAL

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define F first
#define S second
#define pb push_back
#define mp make_pair

namespace watch{

    const int SECONDS_IN_DAY = 24 * 60 * 60;
    const int DAYS[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int DAYS_LEAP[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    bool isLeapYear(int year) {
        return (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0));
    }

    int getDaysInYear(int year) {
        return 365 + isLeapYear(year);
    }

    int DAYS_400[400];
    int P_DAYS_400[400];

    auto __precompute_days_400 = [](){
        for (int i = 0; i < 400; i++) {
            P_DAYS_400[i] = DAYS_400[i] = getDaysInYear(i);
            if (i) P_DAYS_400[i] += P_DAYS_400[i - 1];
        }
        return 0;
    }();

    unsigned long long daysInYears(int l, int r) {
        assert(l <= r);
        if (l == r) return 0;
        if (l % 400 == 0) {
            int cnt = r - l;
            int k = cnt / 400;
            unsigned long long res = k * 1ll * P_DAYS_400[399];
            cnt %= 400;
            if (cnt--) res += P_DAYS_400[cnt % 400];
            return res;
        }
        unsigned long long res = daysInYears(l - (l % 400), r);
        int pr = l % 400;
        if (pr--) res -= P_DAYS_400[pr];
        return res;
    }

    void parseMonthDay(int days, int &m, int &d, bool isLeap) {
        const int *arr = (isLeap ? DAYS_LEAP : DAYS);
        m = 0;
        while (days > arr[m]) {
            days -= arr[m++];
        }
        m++;
        d = days;
    }

    struct second{
        int h, m, s;

        bool operator<(const second &to) {
            if (h < to.h) return true;
            if (h > to.h) return false;
            if (m < to.m) return true;
            if (m > to.m) return false;
            return s < to.s;
        }

        bool operator==(const second &to) {
            return h == to.h && s == to.s && m == to.m;
        }

        int toInt() {
            return (h * 60 + m) * 60 + s;
        }

        int operator-(second &t) {
            return toInt() - t.toInt();
        }

        second(int h, int m, int s): h(h), s(s), m(m) {}

        second(int t) {
            s = t % 60;
            t /= 60;
            m = t % 60;
            h = t / 60;
        }
    };

    struct day{
        int y, m, d;
        int w;

        bool isLeap() {
            return isLeapYear(y);
        }

        bool operator<(const day &to) {
            if (y < to.y) return true;
            if (y > to.y) return false;
            if (m < to.m) return true;
            if (m > to.m) return false;
            return d < to.d;
        }

        bool operator==(const day &to) {
            return y == to.y && m == to.m && d == to.d;
        }

        int getDay() {
            const int *arr = (isLeapYear(y) ? DAYS_LEAP : DAYS);
            int res = 0;
            for (int i = 1; i < m; i++) res += arr[i - 1];
            res += d;
            return res;
        }

        unsigned long long operator-(day &t) {
            if ((*this) < t) return -(t - (*this));
            if (y == t.y) return getDay() - t.getDay();
            return daysInYears(t.y + 1, y) + (t.isLeap() ? 366 : 365) - t.getDay() + getDay();
        }

        day(unsigned long long days) {
            w = 3 + (days % 7);
            w %= 7;
            w++;
            days += P_DAYS_400[1970 - 1 - 1600];
            y = 1600;
            y += 400 * (days / P_DAYS_400[399]);
            days %= P_DAYS_400[399];
            int l = 0;
            int r = 399;
            int ans = 0;
            while (l <= r) {
                int mid = (l + r) / 2;
                if (P_DAYS_400[mid] <= days) {
                    ans = mid + 1;
                    l = mid + 1;
                } else {
                    r = mid - 1;
                }
            }
            y += ans;
            if (ans) days -= P_DAYS_400[ans - 1];
            parseMonthDay((int)days + 1, m, d, isLeapYear(y));
        }

        void next() {
            if (++w == 8) w = 1;
            if (m == 12 && d == 31) {
                d = 1;
                m = 1;
                y++;
                return;
            }
            const int *arr = (isLeapYear(y) ? DAYS_LEAP : DAYS);
            if (arr[m - 1] <= d) {
                d = 1;
                m++;
            } else d++;
        }
    };

    struct instant{
        day d;
        second s;

        instant(unsigned long long utc): d(utc / SECONDS_IN_DAY), s(utc % SECONDS_IN_DAY) {}

        unsigned long long operator-(instant &in) {
            day &d2 = d;
            day &d1 = in.d;
            int days = d2 - d1;
            if (days == 0) return s - in.s;
            return SECONDS_IN_DAY * (days - 1ll) + SECONDS_IN_DAY + (s - in.s);
        }
    };

    ostream& operator<<(ostream &out, const day &d) {
        out << d.d << "." << d.m << "." << d.y;
        return out;
    }

    ostream& operator<<(ostream &out, const second &s) {
        out << s.h << ":" << s.m << ":" << s.s;
        return out;
    }

    ostream& operator<<(ostream &out, const instant &ins) {
        out << ins.d << " " << ins.s;
        return out;
    }

}

bool match(watch::day &d, int day, int date, int month) {
    if (month != -1 && d.m != month) return false;
    if (day != -1 && date != -1) return d.w == day || d.d == date;
    if (day != -1 && day != d.w) return false;
    if (date != -1 && date != d.d) return false;
    return true;
}

bool match(watch::second &t, int s, int m, int h) {
    if (s != -1 && t.s != s) return false;
    if (m != -1 && t.m != m) return false;
    if (h != -1 && t.h != h) return false;
    return true;
}

int main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif // LOCAL
    int s, m, h, day, date, month;
    cin >> s >> m >> h >> day >> date >> month;
    vector<watch::second> sc;
    int n;
    cin >> n;
    for (int i = 0; i < watch::SECONDS_IN_DAY; i++) {
        watch::second sec(i);
        if (match(sec, s, m, h)) sc.pb(sec);
    }
    watch::instant start(0);
    while (n--) {
        long long x;
        cin >> x;
        x++;
        watch::instant ins(x);
        while (true) {
            if (match(ins.d, day, date, month)) {
                auto it = lower_bound(sc.begin(), sc.end(), ins.s);
                if (it != sc.end()) {
                    ins.s = *it;
                    break;
                }
            }
            ins.d.next();
            ins.s = watch::second(0);
        }
        cout << (ins - start) << endl;
    }
    return 0;
}


// 11:50
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

#define sz(a) static_cast<int>(a.size())
#define all(a) a.begin(), a.end()
#define rep(i, a, b) for(int i=int(a); i<=int(b); ++i)
#define irep(i, a, b) for(int i=int(a); i>=int(b); --i)
#define repn(i, n) rep(i, 0, (n)-1)
#define irepn(i, n) irep(i, (n)-1, 0)
#define fillchar(a, x) memset(a, x, sizeof(a))
#define fi first
#define se second
#define pb push_back
#define mp make_pair
typedef long long LL;
typedef pair<LL,LL> PLL;
typedef pair<int,int> PII;
typedef pair<double,double> PDD;
typedef vector<LL> VL;
typedef vector<int> VI;
typedef vector<PII> VPI;
typedef vector<string> VS;
template<class T> bool setmax(T &_a, T _b) { if(_b>_a) { _a=_b; return true; } return false; }
template<class T> bool setmin(T &_a, T _b) { if(_b<_a) { _a=_b; return true; } return false; }
template<class T> T gcd(T _a, T _b) { return _b==0?_a:gcd(_b,_a%_b); }

const int seconds_in_day=60*60*24;

bool is_leap_year(int y) { return y%4==0 && (y%100!=0 || y%400==0); }

int days_in_month(int y, int m) {
    if(m==1 || m==3 || m==5 || m==7 || m==8 || m==10 || m==12) return 31;
    if(m==4 || m==6 || m==9 || m==11) return 30;
    if(m==2) return is_leap_year(y)?29:28;
    return 0;
}

int days_in_year(int y) {
    return is_leap_year(y)?366:365;
}

struct Time {
    int s=0, m=0, h=0;
    int day=4, date=1, month=1, year=1970;
    void read() { cin>>s>>m>>h>>day>>date>>month; }
};

Time from_unix(LL ut) {
    Time t;
    while(1) {
        int days=days_in_year(t.year);
        LL seconds=seconds_in_day*(LL)days;
        if(seconds>ut) break;
        ut-=seconds;
        t.day=(t.day-1+days)%7+1;
        t.year++;
    }
    while(1) {
        int days=days_in_month(t.year, t.month);
        LL seconds=seconds_in_day*(LL)days;
        if(seconds>ut) break;
        ut-=seconds;
        t.day=(t.day-1+days)%7+1;
        t.month++;
    }
    int days=int(ut/seconds_in_day);
    ut-=seconds_in_day*(LL)days;
    t.day=(t.day-1+days)%7+1;
    t.date+=days;
    t.h=int(ut/3600);
    ut-=t.h*3600;
    t.m=int(ut/60);
    ut-=t.m*60;
    t.s=int(ut);
    return t;
}

LL to_unix(Time t) {
    LL ut=0;
    rep(y, 1970, t.year-1) {
        ut+=seconds_in_day*(LL)days_in_year(y);
    }
    rep(m, 1, t.month-1) {
        ut+=seconds_in_day*(LL)days_in_month(t.year, m);
    }
    ut+=seconds_in_day*(LL)(t.date-1)+3600*t.h+60*t.m+t.s;
    return ut;
}

bool match(const Time& pattern, const Time& t) {
    if(pattern.s>=0 && pattern.s!=t.s) return false;
    if(pattern.m>=0 && pattern.m!=t.m) return false;
    if(pattern.h>=0 && pattern.h!=t.h) return false;
    if(pattern.month>=0 && pattern.month!=t.month) return false;
    if(pattern.day>=0 || pattern.date>=0) {
        bool ok=false;
        if(pattern.day>=0 && pattern.day==t.day) ok=true;
        if(pattern.date>=0 && pattern.date==t.date) ok=true;
        if(!ok) return false;
    }
    return true;
}

Time solve(Time pattern, Time t) {
    bool first=true;
    do {
        if(first || (pattern.s>=0 && pattern.s!=t.s) || (pattern.s<0 && t.s!=0)) {
            ++t.s;
        } else if((pattern.m>=0 && pattern.m!=t.m) || (pattern.m<0 && t.m!=0)) {
            ++t.m;
        } else if((pattern.h>=0 && pattern.h!=t.h) || (pattern.h<0 && t.h!=0)) {
            ++t.h;
        } else {
            t.day=t.day%7+1, ++t.date;
        }
        if(t.s==60) { t.s=0, ++t.m; }
        if(t.m==60) { t.m=0, ++t.h; }
        if(t.h==24) { t.h=0, t.day=t.day%7+1, ++t.date; }
        if(t.date>days_in_month(t.year, t.month)) {
            t.date=1, ++t.month;
        }
        if(t.month>12) { t.month=1, ++t.year; }
        first=false;
    } while(!match(pattern, t));
    return t;
}

int main() {
    Time pattern; pattern.read();
    int n; cin>>n;
    while(n--) {
        LL ut; cin>>ut;
        Time r=solve(pattern, from_unix(ut));
        cout<<to_unix(r)<<endl;
    }
    return 0;
}


// 11:50
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

#define sz(a) static_cast<int>(a.size())
#define all(a) a.begin(), a.end()
#define rep(i, a, b) for(int i=int(a); i<=int(b); ++i)
#define irep(i, a, b) for(int i=int(a); i>=int(b); --i)
#define repn(i, n) rep(i, 0, (n)-1)
#define irepn(i, n) irep(i, (n)-1, 0)
#define fillchar(a, x) memset(a, x, sizeof(a))
#define fi first
#define se second
#define pb push_back
#define mp make_pair
typedef long long LL;
typedef pair<LL,LL> PLL;
typedef pair<int,int> PII;
typedef pair<double,double> PDD;
typedef vector<LL> VL;
typedef vector<int> VI;
typedef vector<PII> VPI;
typedef vector<string> VS;
template<class T> bool setmax(T &_a, T _b) { if(_b>_a) { _a=_b; return true; } return false; }
template<class T> bool setmin(T &_a, T _b) { if(_b<_a) { _a=_b; return true; } return false; }
template<class T> T gcd(T _a, T _b) { return _b==0?_a:gcd(_b,_a%_b); }

const int seconds_in_day=60*60*24;

bool is_leap_year(int y) { return y%4==0 && (y%100!=0 || y%400==0); }

int days_in_month(int y, int m) {
    if(m==1 || m==3 || m==5 || m==7 || m==8 || m==10 || m==12) return 31;
    if(m==4 || m==6 || m==9 || m==11) return 30;
    if(m==2) return is_leap_year(y)?29:28;
    return 0;
}

int days_in_year(int y) {
    return is_leap_year(y)?366:365;
}

struct Time {
    int s=0, m=0, h=0;
    int day=4, date=1, month=1, year=1970;
    void read() { cin>>s>>m>>h>>day>>date>>month; }

    void advance_year() {
        day=(day-1+days_in_year(year))%7+1;
        ++year;
    }
};

Time from_unix(LL ut) {
    Time t;
    while(1) {
        int days=days_in_year(t.year);
        LL seconds=seconds_in_day*(LL)days;
        if(seconds>ut) break;
        ut-=seconds;
        t.day=(t.day-1+days)%7+1;
        t.year++;
    }
    while(1) {
        int days=days_in_month(t.year, t.month);
        LL seconds=seconds_in_day*(LL)days;
        if(seconds>ut) break;
        ut-=seconds;
        t.day=(t.day-1+days)%7+1;
        t.month++;
    }
    int days=int(ut/seconds_in_day);
    ut-=seconds_in_day*(LL)days;
    t.day=(t.day-1+days)%7+1;
    t.date+=days;
    t.h=int(ut/3600);
    ut-=t.h*3600;
    t.m=int(ut/60);
    ut-=t.m*60;
    t.s=int(ut);
    return t;
}

LL to_unix(Time t) {
    LL ut=0;
    rep(y, 1970, t.year-1) {
        ut+=seconds_in_day*(LL)days_in_year(y);
    }
    rep(m, 1, t.month-1) {
        ut+=seconds_in_day*(LL)days_in_month(t.year, m);
    }
    ut+=seconds_in_day*(LL)(t.date-1)+3600*t.h+60*t.m+t.s;
    return ut;
}

bool match(const Time& pattern, const Time& t) {
    if(pattern.s>=0 && pattern.s!=t.s) return false;
    if(pattern.m>=0 && pattern.m!=t.m) return false;
    if(pattern.h>=0 && pattern.h!=t.h) return false;
    if(pattern.month>=0 && pattern.month!=t.month) return false;
    if(pattern.day>=0 && pattern.date>=0) {
        bool ok=false;
        if(pattern.day>=0 && pattern.day==t.day) ok=true;
        if(pattern.date>=0 && pattern.date==t.date) ok=true;
        if(!ok) return false;
    }
    return true;
}

Time solve(Time pattern, Time t) {
    bool first=true;
    do {
        if(first || (pattern.s>=0 && pattern.s!=t.s) || (pattern.s<0 && t.s!=0)) {
            ++t.s;
        } else if((pattern.m>=0 && pattern.m!=t.m) || (pattern.m<0 && t.m!=0)) {
            ++t.m;
        } else if((pattern.h>=0 && pattern.h!=t.h) || (pattern.h<0 && t.h!=0)) {
            ++t.h;
        } else {
            t.day=t.day%7+1, ++t.date;
        }
        if(t.s==60) { t.s=0, ++t.m; }
        if(t.m==60) { t.m=0, ++t.h; }
        if(t.h==24) { t.h=0, t.day=t.day%7+1, ++t.date; }
        if(t.date==days_in_month(t.year, t.month)) {
            t.date=1, ++t.month;
        }
        if(t.month==13) { t.month=1, ++t.year; }
        first=false;
    } while(!match(pattern, t));
    return t;
}

int main() {
    Time pattern; pattern.read();
    int n; cin>>n;
    while(n--) {
        LL ut; cin>>ut;
        Time r=solve(pattern, from_unix(ut));
        cout<<to_unix(r)<<endl;
    }
    return 0;
}


// eddy1021
#include <bits/stdc++.h>
using namespace std;
typedef double D;
typedef long double LD;
typedef long long ll;
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef pair<LD,LD> Pt;
typedef tuple<int,int,int> tiii;
typedef tuple<LL,LL,LL> tlll;
#define mod9 1000000009ll
#define mod7 1000000007ll
#define INF  1023456789ll
#define INF16 10000000000000000ll
#define FI first
#define SE second
#define X FI
#define Y SE
#define PB push_back
#define MP make_pair
#define MT make_tuple
#define eps 1e-9
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(), (x).end()
#ifndef ONLINE_JUDGE
#define debug(...) printf(__VA_ARGS__)
#else
#define debug(...)
#endif
inline ll getint(){
  ll _x=0,_tmp=1; char _tc=getchar();
  while( (_tc<'0'||_tc>'9')&&_tc!='-' ) _tc=getchar();
  if( _tc == '-' ) _tc=getchar() , _tmp = -1;
  while(_tc>='0'&&_tc<='9') _x*=10,_x+=(_tc-'0'),_tc=getchar();
  return _x*_tmp;
}
inline ll add( ll _x , ll _y , ll _mod = mod7 ){
  ll _ = _x + _y;
  if( _ >= _mod ) _ -= _mod;
  return _;
}
inline ll sub( ll _x , ll _y , ll _mod = mod7 ){
  ll _ = _x - _y;
  if( _ < 0 ) _ += _mod;
  return _;
}
inline ll mul( ll _x , ll _y , ll _mod = mod7 ){
  ll _ = _x * _y;
  if( _ >= _mod ) _ %= _mod;
  return _;
}
ll mypow( ll _a , ll _x , ll _mod ){
  if( _x == 0 ) return 1ll;
  ll _tmp = mypow( _a , _x / 2 , _mod );
  _tmp = mul( _tmp , _tmp , _mod );
  if( _x & 1 ) _tmp = mul( _tmp , _a , _mod );
  return _tmp;
}
ll mymul( ll _a , ll _x , ll _mod ){
  if( _x == 0 ) return 0ll;
  ll _tmp = mymul( _a , _x / 2 , _mod );
  _tmp = add( _tmp , _tmp , _mod );
  if( _x & 1 ) _tmp = add( _tmp , _a , _mod );
  return _tmp;
}
inline bool equal( D _x ,  D _y ){
  return _x > _y - eps && _x < _y + eps;
}
int __ = 1 , _cs;
/*********default*********/
void build(){

}
int sec , mnt , hr , day , dat , mon;
vector< int > sat;
bool isleap( int yy ){
  if( yy % 400 == 0 ) return true;
  if( yy % 100 == 0 ) return false;
  return yy % 4 == 0;
}
int days( int yy ){
  return isleap( yy ) ? 366 : 365;
}
int dyn[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int dyl[]={0,31,29,31,30,31,30,31,31,30,31,30,31};
void pre_build(){
  for( int hh = 0 ; hh < 24 ; hh ++ )
    for( int mm = 0 ; mm < 60 ; mm ++ )
      for( int ss = 0 ; ss < 60 ; ss ++ )
        if( ( sec == -1 || sec == ss ) &&
            ( mnt == -1 || mnt == mm ) &&
            ( hr  == -1 || hr  == hh ) )
          sat.push_back( hh * 3600 + mm * 60 + ss );
}
void init(){
  sec = getint();
  mnt = getint();
  hr  = getint();
  day = getint();
  if( day != -1 ) day --;
  dat = getint();
  mon = getint();
}
void solve(){
  pre_build();
  int q = getint(); while( q -- ){
    LL qt = getint();
    LL ny = 1970 , at = 0 , w = 3;
    while( at + 86400ll * days( ny ) <= qt ){
      w = ( w + days( ny ) ) % 7;
      at += 86400ll * days( ny ); ny ++;
    }
    bool got = false;
    while( !got ){
      bool isl = isleap( ny );
      for( int i = 1 ; i <= 12 && !got ; i ++ )
        if( ( mon == -1 || mon == i ) ){
          if( isl ){
            for( int j = 1 ; j <= dyl[ i ] ; j ++ , w = ( w + 1 ) % 7 ){
              if( ( w == day || j == dat || ( day == -1 && dat == -1 ) ) ){
                if( at + sat.back() > qt ){
                  got = true;
                  int bst = upper_bound( ALL( sat ) , qt - at ) - sat.begin();
                  at += sat[ bst ]; got = true;
                  break;
                }
              }
              at += 86400ll;
            }
          }else{
            for( int j = 1 ; j <= dyn[ i ] ; j ++ , w = ( w + 1 ) % 7 ){
              if( ( w == day || j == dat || ( day == -1 && dat == -1 ) ) ){
                if( at + sat.back() > qt ){
                  got = true;
                  int bst = upper_bound( ALL( sat ) , qt - at ) - sat.begin();
                  at += sat[ bst ]; got = true;
                  break;
                }
              }
              at += 86400ll;
            }
          }
        }else{
          if( isl ){
            at += 86400ll * dyl[ i ];
            w = ( w + dyl[ i ] ) % 7;
          }else{
            at += 86400ll * dyn[ i ];
            w = ( w + dyn[ i ] ) % 7;
          }
        }
      ny ++;
    }
    printf( "%lld\n" , at );
  }
}
int main(){
  build();
  //__ = getint();
  while( __ -- ){
    init();
    solve();
  }
}