#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<deque>
#include<iterator>
#include<iostream>
#include<list>
#include<map>
#include<memory>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<vector>
#include<utility>
#include<bitset>
#define ll long long
const int Y=32000;
const int Day=86400;
const int Hour=3600;
using namespace std;

int Year[Y];
int m[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int summ[13];

bool ly(int k){
     k+=1970-1;
     if(k%100==0) return (k%400==0);
     return (k&3)==0;
}

void init(){
     int i;
     Year[0]=0;
     for(i=1;i<Y;i++)
        if(ly(i)) Year[i]=Year[i-1]+366;
        else Year[i]=Year[i-1]+365;
     summ[0]=0;
     for(i=1;i<=12;i++)
         summ[i]=summ[i-1]+m[i];
}

struct node{
    int hour,min,sec,month,date,day,year;
    int calc_day(){
        int d=Year[year-1];
        d+=summ[month-1];
        if(month>2 && ly(year)) d++;
        d+=date;
        return (d%7+2)%7+1;
    }
}cur,nxt;

ll getsec(node t){
           int d=Year[t.year-1];
           d+=summ[t.month-1];
           if(t.month>2 && ly(t.year)) d++;
           d+=t.date-1;
           int s=t.hour*60*60+t.min*60+t.sec;
           return (ll)s+1LL*d*Day;
}

void next_days(node &c,int d){
     c.date+=d;
     if(c.month==2 && ly(c.year))
        if(c.date<=29) return;
     if(c.date<=m[c.month]) return;
     c.date-=m[c.month];
     if(c.month==2 && ly(c.year)) c.date--;
     c.month++;
     if(c.month<=12) return;
     c.month=1;
     c.year++;
}

void first_day(node &c,int d){
    int curday=c.calc_day();//cout<<curday<<endl;
    while(curday!=d)
    {  next_days(c,1);
       curday=c.calc_day();
    }
}

void set0(node &ans,node st){
     if(st.hour==-1) ans.hour=0; else ans.hour=st.hour;
     if(st.min==-1) ans.min=0; else ans.min=st.min;
     if(st.sec==-1) ans.sec=0; else ans.sec=st.sec;
}

bool ok(node &a,node b){
     if(a.min>-1 && a.min<b.min) return false;
     if(a.min>-1)
     {  if(a.min>b.min)
        {  if(a.sec==-1) a.sec=0;return true;}
        else
        {  if(a.sec>-1 && a.sec<=b.sec) return false;
           if(a.sec>-1) return true;
           if(b.sec==59) return false;
           else {a.sec=b.sec+1;return true;}
        }
     }
     else
     {  if(a.sec>b.sec)
        { a.min=b.min;return true;}
        if(a.sec==b.sec)
        {  if(b.min==59) return false;
           a.min=b.min+1;
           return true;
        }
        if(a.sec>-1)
        {  if(b.min==59) return false;
           a.min=b.min+1;
           return true;
        }
        if(b.sec==59)
        {  if(b.min==59) return false;
           a.min=b.min+1;
           a.sec=0;
           return true;
        }
        a.sec=b.sec+1;
        a.min=b.min;
        return true;
     }
}

bool checktime(node &a,node b){
     if(b.hour==23 && b.min==59 && b.sec==59) return false;
     if(a.hour>-1 && a.hour<b.hour) return false;
     if(a.hour>b.hour)
     {  if(a.min==-1) a.min=0;
        if(a.sec==-1) a.sec=0;
        return true;
     }
     if(a.hour==b.hour) return ok(a,b);
     if(ok(a,b))
     {  a.hour=b.hour;
        return true;
     }
     if(b.hour==23) return false;
     a.hour=b.hour+1;
     if(a.min==-1) a.min=0;
     if(a.sec==-1) a.sec=0;
     return true;
}

node check_mon(const node &c,const node &st){
        node ans;
        ans.year=c.year;
        ans.month=c.month;
        if(st.day==-1)
        {  if(st.date==-1)
           { ans.date=c.date;
             ans.hour=st.hour;ans.min=st.min;ans.sec=st.sec;
             if(checktime(ans,c)) return ans;
             next_days(ans,1);
             if(ans.month==c.month)
             {  set0(ans,st);return ans;}
             ans.year=c.year+1;
             ans.month=c.month;
             ans.date=1;
             set0(ans,st);return ans;
           }
           else
           {  if(st.date>c.date)
              { ans.date=st.date;
                set0(ans,st);return ans;
              }
              else if(st.date==c.date)
              {  ans.date=c.date;
                 ans.hour=st.hour;ans.min=st.min;ans.sec=st.sec;
                 if(checktime(ans,c)) return ans;
              }
              ans.year=c.year+1;
              ans.month=c.month;
              ans.date=st.date;
              set0(ans,st);
              return ans;
           }
        }
        else
        {   ans.date=c.date;
            first_day(ans,st.day);
            if(ans.month==st.month)
            {  if(ans.date==c.date)
               {  ans.hour=st.hour;ans.min=st.min;ans.sec=st.sec;
                  if(checktime(ans,c)) return ans;
                  next_days(ans,1);
                  first_day(ans,st.day);
                  if(ans.month==st.month)
                  {  set0(ans,st);return ans;}
               }
               if(ans.month==st.month)
               {  set0(ans,st);return ans;}
            }
            ans.year++;
            ans.month=st.month;
            ans.date=1;
            first_day(ans,st.day);
            set0(ans,st);
            return ans;
        }
}

node calc(const node &st,const node &c){
     node ans;
     if(st.month>c.month)
     {  ans.year=c.year;
        ans.month=st.month;
        if(st.day==-1)
        {  if(st.date==-1) ans.date=1;
           else{
                if(st.month==2 && st.date==29 && !ly(c.year) )
                  { while(!ly(ans.year)) ans.year++;}
                ans.date=st.date;
           }
           set0(ans,st);
           return ans;
        }
        else
        {  ans.date=1;
           first_day(ans,st.day);
           set0(ans,st);
           return ans;
        }
     }
     else if(st.month<c.month && st.month>0)
     {  ans.year=c.year+1;
        ans.month=st.month;
        if(st.day==-1)
        {  if(st.date==-1) ans.date=1;
           else{ if(st.month==2 && st.date==29 && !ly(c.year))
                 {  while(!ly(ans.year)) ans.year++;}
                 ans.date=st.date;
           }
           set0(ans,st);
           return ans;
        }
        else
        {  ans.date=1;
           first_day(ans,st.day);
           set0(ans,st);
           return ans;
        }
     }
     else if(st.month==c.month)
     {  return check_mon(c,st);
     }
     else
     {  ans.month=c.month;
        ans=check_mon(c,st);
        if(ans.year==c.year) return ans;
        ans.month=c.month+1;
        ans.year=c.year;
        if(ans.month>12)
        { ans.month-=12;
          ans.year=c.year+1;
        }
        if(st.day==-1)
        {  if(st.date==-1)
           {  ans.date=1;
              set0(ans,st);
              return ans;
           }
           else
           {  while(true)
              {  if(ly(ans.year) && st.date>29) ans.month++;
                 else if(st.date>m[ans.month]) ans.month++;
                 else break;
                 if(ans.month>12) {ans.year++;ans.month=1;}
              }
              ans.date=st.date;
              set0(ans,st);
              return ans;
           }
        }
        else
        {   ans.date=1;
            first_day(ans,st.day);
            set0(ans,st);
            return ans;
        }
     }
}

int main(){
    node st;
    scanf("%d%d%d%d%d%d",&st.sec,&st.min,&st.hour,&st.day,&st.date,&st.month);
    int n,i;
    init();
    scanf("%d",&n);
    ll t;
    while(n--)
    {  scanf("%I64d",&t);
       int d=(int)(t/(ll)Day);
       int q=t-1LL*d*Day;
       ++d;
       int cury=upper_bound(Year,Year+Y,d)-Year;
       int curd,curmon;
       d-=Year[cury-1];
       if(d>0)
       {  for(i=1;i<=12;i++)
          {   if(i!=2)
              {  if(d<=m[i]) break;
                 d-=m[i];
              }
              else
              {   int cur;
                  if(ly(cury)) cur=29; else cur=28;
                  if(d<=cur) break;
                  d-=cur;
              }
          }
          curd=d;curmon=i;
       }
       else
       {  curd=31;curmon=12;cury--;}
       int curhr=q/Hour;
       q=q-curhr*Hour;
       int curmin=q/60;
       int cursec=q-60*curmin;
       cur.year=cury;cur.min=curmin;cur.sec=cursec;cur.hour=curhr;
       cur.month=curmon;cur.date=curd;cur.day=cur.calc_day();
       //cout<<cury+1970-1<<' '<<curmon<<' '<<curd<<' '<<cur.day<<' '<<curhr<<' '<<curmin<<cursec<<endl;
       ll res;
       if(st.date!=-1 && st.day!=-1)
       {  node tmp=st;
          tmp.date=-1;
          nxt=calc(tmp,cur);
          //cout<<nxt.year+1970-1<<' '<<nxt.month<<' '<<nxt.date<<' '<<nxt.day<<' '<<nxt.hour<<' '<<nxt.min<<' '<<nxt.sec<<endl;
          res=getsec(nxt);
          tmp=st;tmp.day=-1;
          nxt=calc(tmp,cur);
          //cout<<nxt.year+1970-1<<' '<<nxt.month<<' '<<nxt.date<<' '<<nxt.day<<' '<<nxt.hour<<nxt.min<<nxt.sec<<endl;
          res=min(res,getsec(nxt));
       }
       else
       {  nxt=calc(st,cur);
          res=getsec(nxt);
       }
       //if(res==654057194400) cout<<cury+1970-1<<curmon<<curd<<cur.day<<curhr<<curmin<<cursec<<endl;
       printf("%I64d\n",res);
    }
    //system("pause");
    return 0;
}


#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<deque>
#include<iterator>
#include<iostream>
#include<list>
#include<map>
#include<memory>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<vector>
#include<utility>
#include<bitset>
#define ll long long
const int Y=32000;
const int Day=86400;
const int Hour=3600;
using namespace std;

int Year[Y];
int m[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int summ[13];

bool ly(int k){
     k+=1970-1;
     if(k%100==0) return (k%400==0);
     return (k&3)==0;
}

void init(){
     int i;
     Year[0]=0;
     for(i=1;i<Y;i++)
        if(ly(i)) Year[i]=Year[i-1]+366;
        else Year[i]=Year[i-1]+365;
     summ[0]=0;
     for(i=1;i<=12;i++)
         summ[i]=summ[i-1]+m[i];
}

struct node{
    int hour,min,sec,month,date,day,year;
    int calc_day(){
        int d=Year[year-1];
        d+=summ[month-1];
        if(month>2 && ly(year)) d++;
        d+=date;
        return (d%7+2)%7+1;
    }
}cur,nxt;

ll getsec(node t){
           int d=Year[t.year-1];
           d+=summ[t.month-1];
           if(t.month>2 && ly(t.year)) d++;
           d+=t.date-1;
           int s=t.hour*60*60+t.min*60+t.sec;
           return (ll)s+1LL*d*Day;
}

void next_days(node &c,int d){
     c.date+=d;
     if(c.month==2 && ly(c.year))
        if(c.date<=29) return;
     if(c.date<=m[c.month]) return;
     c.date-=m[c.month];
     if(c.month==2 && ly(c.year)) c.date--;
     c.month++;
     if(c.month<=12) return;
     c.month=1;
     c.year++;
}

void first_day(node &c,int d){
    int curday=c.calc_day();//cout<<curday<<endl;
    while(curday!=d)
    {  next_days(c,1);
       curday=c.calc_day();
    }
}

void set0(node &ans,node st){
     if(st.hour==-1) ans.hour=0; else ans.hour=st.hour;
     if(st.min==-1) ans.min=0; else ans.min=st.min;
     if(st.sec==-1) ans.sec=0; else ans.sec=st.sec;
}

bool ok(node &a,node b){
     if(a.min>-1 && a.min<b.min) return false;
     if(a.min>-1)
     {  if(a.min>b.min)
        {  if(a.sec==-1) a.sec=0;return true;}
        else
        {  if(a.sec>-1 && a.sec<=b.sec) return false;
           if(a.sec>-1) return true;
           if(b.sec==59) return false;
           else {a.sec=b.sec+1;return true;}
        }
     }
     else
     {  if(a.sec>b.sec)
        { a.min=b.min;return true;}
        if(a.sec==b.sec)
        {  if(b.min==59) return false;
           a.min=b.min+1;
           return true;
        }
        if(a.sec>-1)
        {  if(b.min==59) return false;
           a.min=b.min+1;
           return true;
        }
        if(b.sec==59)
        {  if(b.min==59) return false;
           a.min=b.min+1;
           a.sec=0;
           return true;
        }
        a.sec=b.sec+1;
        a.min=b.min;
        return true;
     }
}

bool checktime(node &a,node b){
     if(b.hour==23 && b.min==59 && b.sec==59) return false;
     if(a.hour>-1 && a.hour<b.hour) return false;
     if(a.hour>b.hour)
     {  if(a.min==-1) a.min=0;
        if(a.sec==-1) a.sec=0;
        return true;
     }
     if(a.hour==b.hour) return ok(a,b);
     if(ok(a,b))
     {  a.hour=b.hour;
        return true;
     }
     if(b.hour==23) return false;
     a.hour=b.hour+1;
     if(a.min==-1) a.min=0;
     if(a.sec==-1) a.sec=0;
     return true;
}

node check_mon(const node &c,const node &st){
        node ans;
        ans.year=c.year;
        ans.month=c.month;
        if(st.day==-1)
        {  if(st.date==-1)
           { ans.date=c.date;
             ans.hour=st.hour;ans.min=st.min;ans.sec=st.sec;
             if(checktime(ans,c)) return ans;
             next_days(ans,1);
             if(ans.month==c.month)
             {  set0(ans,st);return ans;}
             ans.year=c.year+1;
             ans.month=c.month;
             ans.date=1;
             set0(ans,st);return ans;
           }
           else
           {  if(st.date>c.date)
              { ans.date=st.date;
                set0(ans,st);return ans;
              }
              else if(st.date==c.date)
              {  ans.date=c.date;
                 ans.hour=st.hour;ans.min=st.min;ans.sec=st.sec;
                 if(checktime(ans,c)) return ans;
              }
              ans.year=c.year+1;
              ans.month=c.month;
              ans.date=st.date;
              set0(ans,st);
              return ans;
           }
        }
        else
        {   ans.date=c.date;
            while(true)
            {  first_day(ans,st.day);
               if(ans.month!=st.month) break;
               next_days(ans,1);
            }
            if(ans.month==st.month)
            {  if(ans.date==c.date)
               {  ans.hour=st.hour;ans.min=st.min;ans.sec=st.sec;
                  if(checktime(ans,c)) return ans;
                  next_days(ans,1);
                  first_day(ans,st.day);
                  if(ans.month==st.month)
                  {  set0(ans,st);return ans;}
               }
               if(ans.month==st.month)
               {  set0(ans,st);return ans;}
            }
            ans.year++;
            ans.month=st.month;
            ans.date=1;
            first_day(ans,st.day);
            set0(ans,st);
            return ans;
        }
}

node calc(const node &st,const node &c){
     node ans;
     if(st.month>c.month)
     {  ans.year=c.year;
        ans.month=st.month;
        if(st.day==-1)
        {  if(st.date==-1) ans.date=1;
           else{
                if(st.month==2 && st.date==29 && !ly(c.year) )
                  { while(!ly(ans.year)) ans.year++;}
                ans.date=st.date;
           }
           set0(ans,st);
           return ans;
        }
        else
        {  ans.date=1;
           first_day(ans,st.day);
           set0(ans,st);
           return ans;
        }
     }
     else if(st.month<c.month && st.month>0)
     {  ans.year=c.year+1;
        ans.month=st.month;
        if(st.day==-1)
        {  if(st.date==-1) ans.date=1;
           else{ if(st.month==2 && st.date==29 && !ly(c.year))
                 {  while(!ly(ans.year)) ans.year++;}
                 ans.date=st.date;
           }
           set0(ans,st);
           return ans;
        }
        else
        {  ans.date=1;
           first_day(ans,st.day);
           set0(ans,st);
           return ans;
        }
     }
     else if(st.month==c.month)
     {  return check_mon(c,st);
     }
     else
     {  ans.month=c.month;
        ans=check_mon(c,st);
        if(ans.year==c.year) return ans;
        ans.month=c.month+1;
        ans.year=c.year;
        if(ans.month>12)
        { ans.month-=12;
          ans.year=c.year+1;
        }
        if(st.day==-1)
        {  if(st.date==-1)
           {  ans.date=1;
              set0(ans,st);
              return ans;
           }
           else
           {  while(true)
              {  if(ly(ans.year) && st.date>29) ans.month++;
                 else if(st.date>m[ans.month]) ans.month++;
                 else break;
                 if(ans.month>12) {ans.year++;ans.month=1;}
              }
              ans.date=st.date;
              set0(ans,st);
              return ans;
           }
        }
        else
        {   ans.date=1;
            first_day(ans,st.day);
            set0(ans,st);
            return ans;
        }
     }
}

int main(){
    node st;
    scanf("%d%d%d%d%d%d",&st.sec,&st.min,&st.hour,&st.day,&st.date,&st.month);
    int n,i;
    init();
    scanf("%d",&n);
    ll t;
    while(n--)
    {  scanf("%I64d",&t);
       int d=(int)(t/(ll)Day);
       int q=t-1LL*d*Day;
       ++d;
       int cury=upper_bound(Year,Year+Y,d)-Year;
       int curd,curmon;
       d-=Year[cury-1];
       if(d>0)
       {  for(i=1;i<=12;i++)
          {   if(i!=2)
              {  if(d<=m[i]) break;
                 d-=m[i];
              }
              else
              {   int cur;
                  if(ly(cury)) cur=29; else cur=28;
                  if(d<=cur) break;
                  d-=cur;
              }
          }
          curd=d;curmon=i;
       }
       else
       {  curd=31;curmon=12;cury--;}
       int curhr=q/Hour;
       q=q-curhr*Hour;
       int curmin=q/60;
       int cursec=q-60*curmin;
       cur.year=cury;cur.min=curmin;cur.sec=cursec;cur.hour=curhr;
       cur.month=curmon;cur.date=curd;cur.day=cur.calc_day();
       //cout<<cury+1970-1<<' '<<curmon<<' '<<curd<<' '<<cur.day<<' '<<curhr<<' '<<curmin<<cursec<<endl;
       ll res;
       if(st.date!=-1 && st.day!=-1)
       {  node tmp=st;
          tmp.date=-1;
          nxt=calc(tmp,cur);
         // cout<<nxt.year+1970-1<<' '<<nxt.month<<' '<<nxt.date<<' '<<nxt.day<<' '<<nxt.hour<<' '<<nxt.min<<' '<<nxt.sec<<endl;
          res=getsec(nxt);
          tmp=st;tmp.day=-1;
          nxt=calc(tmp,cur);
         // cout<<nxt.year+1970-1<<' '<<nxt.month<<' '<<nxt.date<<' '<<nxt.day<<' '<<nxt.hour<<nxt.min<<nxt.sec<<endl;
          res=min(res,getsec(nxt));
       }
       else
       {  nxt=calc(st,cur);
          res=getsec(nxt);
       }
       //if(res==654057194400) cout<<cury+1970-1<<curmon<<curd<<cur.day<<curhr<<curmin<<cursec<<endl;
       printf("%I64d\n",res);
    }
    //system("pause");
    return 0;
}


#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<deque>
#include<iterator>
#include<iostream>
#include<list>
#include<map>
#include<memory>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<vector>
#include<utility>
#include<bitset>
#define ll long long
const int Y=32000;
const int Day=86400;
const int Hour=3600;
using namespace std;

int Year[Y];
int m[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int summ[13];

bool ly(int k){
     k+=1970-1;
     if(k%100==0) return (k%400==0);
     return (k&3)==0;
}

void init(){
     int i;
     Year[0]=0;
     for(i=1;i<Y;i++)
        if(ly(i)) Year[i]=Year[i-1]+366;
        else Year[i]=Year[i-1]+365;
     summ[0]=0;
     for(i=1;i<=12;i++)
         summ[i]=summ[i-1]+m[i];
}

struct node{
    int hour,min,sec,month,date,day,year;
    int calc_day(){
        int d=Year[year-1];
        d+=summ[month-1];
        if(month>2 && ly(year)) d++;
        d+=date;
        return (d%7+2)%7+1;
    }
}cur,nxt;

ll getsec(node t){
           int d=Year[t.year-1];
           d+=summ[t.month-1];
           if(t.month>2 && ly(t.year)) d++;
           d+=t.date-1;
           int s=t.hour*60*60+t.min*60+t.sec;
           return (ll)s+1LL*d*Day;
}

void next_days(node &c,int d){
     c.date+=d;
     if(c.date<=m[c.month]) return;
     c.date-=m[c.month];
     c.month++;
     if(c.month<=12) return;
     c.month=1;
     c.year++;
}

void first_day(node &c,int d){
    int curday=c.calc_day();//cout<<curday<<endl;
    while(curday!=d)
    {  next_days(c,1);
       curday=c.calc_day();
    }
}

void set0(node &ans,node st){
     if(st.hour==-1) ans.hour=0; else ans.hour=st.hour;
     if(st.min==-1) ans.min=0; else ans.min=st.min;
     if(st.sec==-1) ans.sec=0; else ans.sec=st.sec;
}

bool ok(node &a,node b){
     if(a.min>-1 && a.min<b.min) return false;
     if(a.min>-1)
     {  if(a.min>b.min)
        {  if(a.sec==-1) a.sec=0;return true;}
        else
        {  if(a.sec>-1 && a.sec<=b.sec) return false;
           if(a.sec>-1) return true;
           if(b.sec==59) return false;
           else {a.sec=b.sec+1;return true;}
        }
     }
     else
     {  if(a.sec>b.sec)
        { a.min=b.min;return true;}
        if(a.sec==b.sec)
        {  if(b.min==59) return false;
           a.min=b.min+1;
           return true;
        }
        if(a.sec>-1)
        {  if(b.min==59) return false;
           a.min=b.min+1;
           return true;
        }
        if(b.sec==59)
        {  if(b.min==59) return false;
           a.min=b.min+1;
           a.sec=0;
           return true;
        }
        a.sec=b.sec+1;
        a.min=b.min;
        return true;
     }
}

bool checktime(node &a,node b){
     if(b.hour==23 && b.min==59 && b.sec==59) return false;
     if(a.hour>-1 && a.hour<b.hour) return false;
     if(a.hour>b.hour)
     {  if(a.min==-1) a.min=0;
        if(a.sec==-1) a.sec=0;
        return true;
     }
     if(a.hour==b.hour) return ok(a,b);
     if(ok(a,b))
     {  a.hour=b.hour;
        return true;
     }
     if(b.hour==23) return false;
     a.hour=b.hour+1;
     if(a.min==-1) a.min=0;
     if(a.sec==-1) a.sec=0;
     return true;
}

node check_mon(const node &c,const node &st){
        node ans;
        ans.year=c.year;
        ans.month=c.month;
        if(st.day==-1)
        {  if(st.date==-1)
           { ans.date=c.date;
             ans.hour=st.hour;ans.min=st.min;ans.sec=st.sec;
             if(checktime(ans,c)) return ans;
             next_days(ans,1);
             if(ans.month==c.month)
             {  set0(ans,st);return ans;}
             ans.year=c.year+1;
             ans.month=c.month;
             ans.date=c.date;
             set0(ans,st);return ans;
           }
           else
           {  if(st.date>c.date)
              { ans.date=st.date;
                set0(ans,st);return ans;
              }
              else if(st.date==c.date)
              {  ans.date=c.date;
                 ans.hour=st.hour;ans.min=st.min;ans.sec=st.sec;
                 if(checktime(ans,c)) return ans;
              }
              ans.year=c.year+1;
              ans.month=c.month;
              ans.date=st.date;
              set0(ans,st);
              return ans;
           }
        }
        else
        {   ans.date=1;
            while(true)
            {  first_day(ans,st.day);
               if(ans.month!=st.month) break;
               if(ans.date>=c.date) break;
               next_days(ans,1);
            }
            if(ans.month==st.month)
            {  if(ans.date==c.date)
               {  ans.hour=st.hour;ans.min=st.min;ans.sec=st.sec;
                  if(checktime(ans,c)) return ans;
                  next_days(ans,1);
                  first_day(ans,st.day);
                  if(ans.month==st.month)
                  {  set0(ans,st);return ans;}
               }
               if(ans.month==st.month)
               {  set0(ans,st);return ans;}
            }
            ans.year++;
            ans.month=st.month;
            ans.date=1;
            first_day(ans,st.day);
            set0(ans,st);
            return ans;
        }
}

node calc(const node &st,const node &c){
     node ans;
     if(st.month>c.month)
     {  ans.year=c.year;
        ans.month=st.month;
        if(st.day==-1)
        {  if(st.date==-1) ans.date=1;
           else{
                if(st.month==2 && st.date==29 && !ly(c.year) )
                  { while(!ly(ans.year)) ans.year++;}
                ans.date=st.date;
           }
           set0(ans,st);
           return ans;
        }
        else
        {  ans.date=1;
           first_day(ans,st.day);
           set0(ans,st);
           return ans;
        }
     }
     else if(st.month<c.month && st.month>0)
     {  ans.year=c.year+1;
        ans.month=st.month;
        if(st.day==-1)
        {  if(st.date==-1) ans.date=1;
           else{ if(st.month==2 && st.date==29 && !ly(c.year))
                 {  while(!ly(ans.year)) ans.year++;}
                 ans.date=st.date;
           }
           set0(ans,st);
           return ans;
        }
        else
        {  ans.date=1;
           first_day(ans,st.day);
           set0(ans,st);
           return ans;
        }
     }
     else if(st.month==c.month)
     {  return check_mon(c,st);
     }
     else
     {  ans.month=c.month;
        ans=check_mon(c,st);
        if(ans.year==c.year) return ans;
        ans.month=c.month+1;
        if(ans.month>12)
        { ans.month-=12;
          ans.year=c.year+1;
        }
        if(st.day==-1)
        {  if(st.date==-1)
           {  ans.date=1;
              set0(ans,st);
              return ans;
           }
           else
           {  while(true)
              {  if(ly(ans.year) && st.date>29) ans.month++;
                 else if(st.date>m[ans.month]) ans.month++;
                 else break;
                 if(ans.month>12) {ans.year++;ans.month=1;}
              }
              ans.date=st.date;
              set0(ans,st);
              return ans;
           }
        }
        else
        {   ans.date=1;
            first_day(ans,st.day);
            set0(ans,st);
            return ans;
        }
     }
}

int main(){
    node st;
    scanf("%d%d%d%d%d%d",&st.sec,&st.min,&st.hour,&st.day,&st.date,&st.month);
    int n,i;
    init();
    scanf("%d",&n);
    ll t;
    while(n--)
    {  scanf("%I64d",&t);
       int d=(int)(t/(ll)Day);
       int q=t-1LL*d*Day;
       ++d;
       int cury=upper_bound(Year,Year+Y,d)-Year;
       int curd,curmon;
       d-=Year[cury-1];
       if(d>0)
       {  for(i=1;i<=12;i++)
          {   if(i!=2)
              {  if(d<=m[i]) break;
                 d-=m[i];
              }
              else
              {   int cur;
                  if(ly(cury)) cur=29; else cur=28;
                  if(d<=cur) break;
                  d-=cur;
              }
          }
          curd=d;curmon=i;
       }
       else
       {  curd=31;curmon=12;cury--;}
       int curhr=q/Hour;
       q=q-curhr*Hour;
       int curmin=q/60;
       int cursec=q-60*curmin;
       cur.year=cury;cur.min=curmin;cur.sec=cursec;cur.hour=curhr;
       cur.month=curmon;cur.date=curd;cur.day=cur.calc_day();
       //cout<<cury+1970-1<<' '<<curmon<<' '<<curd<<' '<<curhr<<curmin<<cursec<<endl;
       ll res;
       if(st.date!=-1 && st.day!=-1)
       {  node tmp=st;
          tmp.date=-1;
          nxt=calc(tmp,cur);
         // cout<<nxt.year+1970-1<<' '<<nxt.month<<' '<<nxt.date<<' '<<nxt.day<<' '<<nxt.hour<<' '<<nxt.min<<' '<<nxt.sec<<endl;
          res=getsec(nxt);
          tmp=st;tmp.day=-1;
          nxt=calc(tmp,cur);
         // cout<<nxt.year+1970-1<<' '<<nxt.month<<' '<<nxt.date<<' '<<nxt.day<<' '<<nxt.hour<<nxt.min<<nxt.sec<<endl;
          res=min(res,getsec(nxt));
       }
       else
       {  nxt=calc(st,cur);
          res=getsec(nxt);
       }
       printf("%I64d\n",res);
    }
    //system("pause");
    return 0;
}


#include <cstdio>
#include <algorithm>

using namespace std;

int second, minute, hour, day, date, month;
long long t;
int a[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int f[50000];
int sum[50001];
long long sum2[50001];

int get_max_date(int year, int month) {
    if (month == 1 && f[year] == 1) {
        return a[month] + 1;
    } else {
        return a[month];
    }
}

long long get(int year, int month, int date, int hour, int minute, int second) {
    int i;
    long long ans = sum2[year];

    for (i = 0; i < month; i++) ans += get_max_date(year, i) * 24 * 60 * 60;

    ans += date * 24 * 60 * 60;
    ans += hour * 60 * 60;
    ans += minute * 60;
    ans += second;

    return ans;
}

long long calc3(int year, int month, int date) {
    long long x = t - get(year, month, date, 0, 0, 0);

    if (x <= 0) {
        int h = max(hour, 0);
        int m = max(minute, 0);
        int s = max(second, 0);

        return get(year, month, date, h, m, s);
    } else if (x >= 24 * 60 * 60) {
        return -1;
    } else {
        int h, m, s;

        s = x % 60;
        x /= 60;
        m = x % 60;
        h = x / 60;

        if (second >= 0) {
            if (second >= s) {
                s = second;
            } else {
                s = second;
                m++;

                if (m >= 60) {
                    m -= 60;
                    h++;

                    if (h >= 24) return -1;
                }
            }
        }

        if (minute >= 0) {
            if (minute > m) {
                m = minute;

                if (second == -1) s = 0;
            } else if (minute < m) {
                m = minute;
                h++;

                if (second == -1) s = 0;

                if (h >= 24) return -1;
            }
        }

        if (hour >= 0) {
            if (hour > h) {
                h = hour;

                if (minute == -1) m = 0;
                if (second == -1) s = 0;
            } else if (hour < h) {
                return -1;
            }
        }

        return get(year, month, date, h, m, s);
    }
}

long long calc2(int year, int month) {
    int i;

    if (date == -1 && day == -1) {
        for (i = 0; i < get_max_date(year, month); i++) {
            long long ans = calc3(year, month, i);

            if (ans >= 0) return ans;
        }
    } else {
        long long ans = 1e18;

        if (date != -1) {
            if (date < get_max_date(year, month)) {
                long long tmp = calc3(year, month, date);

                if (tmp >= 0) ans = min(ans, tmp);
            }
        }

        if (day != -1) {
            int x = sum[year];

            for (i = 0; i < month; i++) x += get_max_date(year, i);

            x %= 7;

            for (i = (day + 7 - x) % 7; i < get_max_date(year, month); i += 7) {
                long long tmp = calc3(year, month, i);

                if (tmp >= 0) ans = min(ans, tmp);
            }
        }

        if (ans != 1e18) return ans;
    }

    return -1;
}

long long calc() {
    int year = upper_bound(sum2, sum2 + 50000, t) - sum2 - 1, i;

    for (; ; year++) {
        if (month >= 0) {
            long long ans = calc2(year, month);

            if (ans >= 0) return ans;
        } else {
            for (i = 0; i < 12; i++) {
                long long ans = calc2(year, i);

                if (ans >= 0) return ans;
            }
        }
    }
}

int main() {
    int n, i;

    scanf("%d %d %d %d %d %d", &second, &minute, &hour, &day, &date, &month);
    scanf("%d", &n);

    if (day >= 0) day--;
    if (date >= 0) date--;
    if (month >= 0) month--;

    sum[0] = 3;

    for (i = 0; i < 50000; i++) {
        int x = 1970 + i;

        if (x % 400 == 0 || (x % 100 != 0 && x % 4 == 0)) f[i] = 1;

        sum[i + 1] = (sum[i] + 365 + f[i]) % 7;
        sum2[i + 1] = sum2[i] + (365 + f[i]) * 24 * 60 * 60;
    }

    for (i = 0; i < n; i++) {
        scanf("%I64d", &t);

        t++;

        printf("%I64d\n", calc());
    }

    return 0;
}


#include <cstdio>
#include <algorithm>

using namespace std;

int s, m, h, day, date, month;
long long t;
int a[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int f[50000];
int sum[50000];
long long sum2[50000];

long long get(int year, int month, int date, int h, int m, int s) {
    int i;
    long long ans = 0;

    ans += sum2[year];

    for (i = 0; i < month; i++) {
        ans += a[i] * 24 * 60 * 60;
        if (i == 1 && f[year + 1] == 1) ans += 24 * 60 * 60;
    }

    ans += date * 24 * 60 * 60;
    ans += h * 60 * 60;
    ans += m * 60;
    ans += s;

    return ans;
}

long long calc3(int year, int month2, int date2) {
    long long x = t - get(year, month2, date2, 0, 0, 0);

    if (x <= 0) {
        int h2 = h, m2 = m, s2 = s;

        if (h2 < 0) h2 = 0;
        if (m2 < 0) m2 = 0;
        if (s2 < 0) s2 = 0;

        return get(year, month2, date2, h2, m2, s2);
    } else if (x >= 24 * 60 * 60) {
        return -1;
    } else {
        int h2, m2, s2;

        h2 = x / (60 * 60);
        x -= h2 * 60 * 60;
        m2 = x / 60;
        s2 = x % 60;

        if (s >= 0) {
            if (s2 > s) {
                s2 = s;
                m2++;
                if (m2 >= 60) {
                    m2 -= 60;
                    h2++;
                    if (h2 >= 24) return -1;
                }
            } else {
                s2 = s;
            }
        }

        if (m >= 0) {
            if (m2 > m) {
                m2 = m;
                h2++;
                if (s == -1) s2 = 0;
                if (h2 >= 24) return -1;
            } else if (m2 < m) {
                m2 = m;
                if (s == -1) s2 = 0;
            }
        }

        if (h >= 0) {
            if (h2 > h) return -1;

            if (h2 < h) {
                h2 = h;
                if (m == -1) m2 = 0;
                if (s == -1) s2 = 0;
            }
        }

        return get(year, month2, date2, h2, m2, s2);
    }
}

long long calc2(int year, int month2) {
    int i;

    if (date == -1 && day == -1) {
        for (i = 0; i < a[month2]; i++) {
            long long ans = calc3(year, month2, i);

            if (ans >= 0) return ans;
        }

        if (month2 == 1 && f[year + 1] == 1) {
            long long ans = calc3(year, month2, i);

            if (ans >= 0) return ans;
        }
    } else {
        long long ans = 1e18;

        if (date != -1) {
            if (date <= a[month2] || (date == 29 && month2 == 1 && f[year + 1] == 1)) {
                long long tmp = calc3(year, month2, date - 1);

                if (tmp >= 0) ans = min(ans, tmp);
            }
        }

        if (day != -1) {
            int x = sum[year];

            for (i = 0; i < month2; i++) {
                x += a[i];

                if (i == 1 && f[year + 1] == 1) x++;
            }

            x %= 7;

            for (i = (day - 1 + 7 - x) % 7; i < a[month2]; i += 7) {
                long long tmp = calc3(year, month2, i);

                if (tmp >= 0) ans = min(ans, tmp);
            }

            if (i == a[month2] && month2 == 1 && f[year + 1] == 1) {
                long long tmp = calc3(year, month2, i);

                if (tmp >= 0) ans = min(ans, tmp);
            }
        }

        if (ans != 1e18) return ans;
    }

    return -1;
}

long long calc() {
    int year = upper_bound(sum2, sum2 + 50000, t) - sum2 - 1, i;

    for (; ; year++) {
        if (month >= 0) {
            long long ans = calc2(year, month - 1);

            if (ans >= 0) return ans;
        } else {
            for (i = 0; i < 12; i++) {
                long long ans = calc2(year, i);

                if (ans >= 0) return ans;
            }
        }
    }
}

int main() {
    int n, i, j, k;

    scanf("%d %d %d %d %d %d", &s, &m, &h, &day, &date, &month);
    scanf("%d", &n);

    sum[0] = 3;

    for (i = 0; i < 50000; i++) {
        int x = 1970 + i;

        if (x % 400 == 0 || (x % 100 != 0 && x % 4 == 0)) f[i + 1] = 1;

        sum[i + 1] = (sum[i] + 365 + f[i + 1]) % 7;
        sum2[i + 1] = sum2[i] + (365 + f[i + 1]) * 24 * 60 * 60;
    }

    for (i = 0; i < n; i++) {
        scanf("%I64d", &t);

        t++;

        printf("%I64d\n", calc());
    }

    return 0;
}


#include <cstdio>
#include <algorithm>

using namespace std;

int s, m, h, day, date, month;
long long t;
int a[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int f[50000];
int sum[50000];
long long sum2[50000];

long long get(int year, int month, int date, int h, int m, int s) {
    int i;
    long long ans = 0;

    ans += sum2[year];

    for (i = 0; i < month; i++) {
        ans += a[i] * 24 * 60 * 60;
        if (i == 1 && f[year + 1] == 1) ans += 24 * 60 * 60;
    }

    ans += date * 24 * 60 * 60;
    ans += h * 60 * 60;
    ans += m * 60;
    ans += s;

    return ans;
}

long long calc3(int year, int month2, int date2) {
    long long x = t - get(year, month2, date2, 0, 0, 0);

    if (x <= 0) {
        int h2 = h, m2 = m, s2 = s;

        if (h2 < 0) h2 = 0;
        if (m2 < 0) m2 = 0;
        if (s2 < 0) s2 = 0;

        return get(year, month2, date2, h2, m2, s2);
    } else if (x >= 24 * 60 * 60) {
        return -1;
    } else {
        int h2, m2, s2;

        h2 = x / (60 * 60);
        x -= h2 * 60 * 60;
        m2 = x / 60;
        s2 = x % 60;

        if (s >= 0) {
            if (s2 > s) {
                s2 = s;
                m2++;
                if (m2 >= 60) {
                    m2 -= 60;
                    h2++;
                    if (h2 >= 24) return -1;
                }
            } else {
                s2 = s;
            }
        }

        if (m >= 0) {
            if (m2 > m) {
                m2 = m;
                h2++;
                if (s == -1) s2 = 0;
                if (h2 >= 24) return -1;
            } else if (m2 < m) {
                m2 = m;
                if (s == -1) s2 = 0;
            }
        }

        if (h >= 0) {
            if (h2 > h) return -1;

            if (h2 < h) {
                h2 = h;
                if (m == -1) m2 = 0;
                if (s == -1) s2 = 0;
            }
        }

        return get(year, month2, date2, h2, m2, s2);
    }
}

long long calc2(int year, int month2) {
    int i;

    if (date == -1 && day == -1) {
        for (i = 0; i < a[month2]; i++) {
            long long ans = calc3(year, month2, i);

            if (ans >= 0) return ans;
        }

        if (month2 == 1 && f[year + 1] == 1) {
            long long ans = calc3(year, month2, i);

            if (ans >= 0) return ans;
        }
    } else {
        long long ans = 1e18;

        if (date != -1) {
            if (date <= a[month2] || (date == 29 && month2 == 1 && f[year + 1] == 1)) {
                long long tmp = calc3(year, month2, date - 1);

                if (tmp >= 0) ans = min(ans, tmp);
            }
        }

        if (day != -1) {
            int x = sum[year];

            for (i = 0; i < month2; i++) {
                x += a[i];

                if (i == 1 && f[year + 1] == 1) x++;
            }

            x %= 7;

            for (i = (day - 1 + 7 - x) % 7; i < a[month2]; i += 7) {
                long long tmp = calc3(year, month2, i);

                if (tmp >= 0) ans = min(ans, tmp);
            }

            if (i == a[month2] && month2 == 1 && f[year + 1] == 1) {
                long long tmp = calc3(year, month2, i);

                if (tmp >= 0) ans = min(ans, tmp);
            }
        }

        if (ans != 1e18) return ans;
    }

    return -1;
}

long long calc() {
    int year = upper_bound(sum2, sum2 + 50000, t) - sum2 - 1, i;

    for (; ; year++) {
        if (month >= 0) {
            long long ans = calc2(year, month - 1);

            if (ans >= 0) return ans;
        } else {
            for (i = 0; i < 12; i++) {
                long long ans = calc2(year, i);

                if (ans >= 0) return ans;
            }
        }
    }
}

int main() {
    int n, i;

    scanf("%d %d %d %d %d %d", &s, &m, &h, &day, &date, &month);
    scanf("%d", &n);

    sum[0] = 3;

    for (i = 0; i < 50000; i++) {
        int x = 1970 + i;

        if (x % 400 == 0 || (x % 100 != 0 && x % 4 == 0)) f[i + 1] = 1;

        sum[i + 1] = (sum[i] + 365 + f[i + 1]) % 7;
        sum2[i + 1] = sum2[i] + (365 + f[i + 1]) * 24 * 60 * 60;
    }

    for (i = 0; i < n; i++) {
        scanf("%I64d", &t);

        t++;

        printf("%I64d\n", calc());
    }

    return 0;
}


#include <cstdio>
#include <algorithm>
typedef long long LL;
const int maxn = 1001;
const int mons[][13] = {{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
int ss, mm, hh, wk, dd, mt, t;
LL val, year, cyear;
int mon, day, vs, vm, vh, vwk;
int cmon, cday, cs, cm, ch, cwk;
inline bool spyear(int year)
{
	return year % 400 == 0 || year % 4 == 0 && year % 100 != 0;
}
inline bool c_valid(int lim)
{
	if(year != cyear || lim == 1)
		return year < cyear;
	if(mon != cmon || lim == 2)
		return mon < cmon;
	if(day != cday || lim == 3)
		return day < cday;
	if(vh != ch || lim == 4)
		return vh < ch;
	if(vm != cm || lim == 5)
		return vm < cm;
	return vs < cs;
}
inline bool isIns(int lim)
{
	int ls = ss == -1 ? 59 : ss;
	if(lim == 1)
		return vs < ls;
	int lm = mm == -1 ? 59 : mm;
	if(lim == 2)
		return vm < lm || vm == lm && vs < ls;
	int lh = hh == -1 ? 23 : hh;
	return vh < lh || vh == lh && (vm < lm || vm == lm && vs < ls);
}
void val_init()
{
	// calc (ss, mm, hh)
	vs = val % 60;
	val /= 60;
	vm = val % 60;
	val /= 60;
	vh = val % 24;
	val /= 24;
	// calc (week, year)
	vwk = (3 + val) % 7 + 1;
	if(val <= 10957)
	{
		year = 1970;
		for(int days; (days = spyear(year) ? 366 : 365) < val; ++year, val -= days);
	}
	else
	{
		val -= 10957;
		year = 2000;
		if(val >= 366)
		{
			++year;
			val -= 366;
			year += val / 146097 * 400;
			val %= 146097;
			if(val / 36524 == 4)
			{
				year += 399;
				val = 365;
			}
			else
			{
				year += val / 36524 * 100;
				val %= 36524;
				year += val / 1461 * 4;
				val %= 1461;
				if(val / 365 == 4)
				{
					year += 3;
					val = 365;
				}
				else
				{
					year += val / 365;
					val %= 365;
				}
			}
		}
	}
	// calc (mm, dd)
	const int *month = mons[spyear(year)];
	for(int i = 1; i <= 12; ++i)
		if(val >= month[i])
			val -= month[i];
		else
		{
			mon = i;
			day = (int)val + 1;
			break;
		}
	//printf("current time:%9lld-%02d-%02d %02d:%02d:%02d (%d)\n", year, mon, day, vh, vm, vs, vwk);
}
void val_calc()
{
	//printf("future time :%9lld-%02d-%02d %02d:%02d:%02d (%d)\n", cyear, cmon, cday, ch, cm, cs, wk == -1 ? -1 : cwk);
	LL year, tmp;
	val = 0;
	for(year = 1970; year + 1 <= cyear && year <= 2000; val += spyear(year++) ? 366 : 365);
	tmp = (cyear - year) / 400;
	val += tmp * 146097;
	year += tmp * 400;
	tmp = (cyear - year) / 100;
	val += tmp * 36524;
	year += tmp * 100;
	tmp = (cyear - year) / 4;
	val += tmp * 1461;
	year += tmp * 4;
	val += (cyear - year) * 365;
	const int *month = mons[spyear(cyear)];
	for(int mon = 1; mon + 1 <= cmon; val += month[mon++]);
	val += cday - 1;
	val = val * 24 + ch;
	val = val * 60 + cm;
	val = val * 60 + cs;
}
void solve()
{
	if(wk == -1)
	{
		cyear = year;
		if(mt == -1)
			cmon = mon;
		else
		{
			cmon = mt;
			if(mt < mon)
				++cyear;
		}
		if(dd == -1)
			cday = c_valid(2) ? 1 : day;
		else
		{
			while(mons[spyear(cyear)][cmon] < dd)
			{
				if(mt == -1)
				{
					if(++cmon > 12)
					{
						cmon = 1;
						++cyear;
					}
				}
				else
					++cyear;
			}
			cday = dd;
		}
		if(!c_valid(3) && !isIns(3))
		{
			if(dd == -1)
			{
				if(++cday > mons[spyear(cyear)][cmon])
				{
					cday = 1;
					if(mt == -1)
					{
						if(++cmon > 12)
						{
							cmon = 1;
							++cyear;
						}
					}
					else
						++cyear;
				}
			}
			else
			{
				do
				{
					if(mt == -1)
					{
						if(++cmon > 12)
						{
							cmon = 1;
							++cyear;
						}
					}
					else
						++cyear;
				} while(mons[spyear(cyear)][cmon] < cday);
			}
		}
		bool flag = c_valid(3);
		ch = hh == -1 ? (flag ? 0 : (isIns(2) ? vh : vh + 1)) : hh;
		flag |= ch > vh;
		cm = mm == -1 ? (flag ? 0 : (isIns(1) ? vm : vm + 1)) : mm;
		flag |= cm > vm;
		cs = ss == -1 ? (flag ? 0 : vs + 1) : mm;
	}
	else
	{
		cyear = year;
		if(mt == -1 || mt == mon)
		{
			cmon = mon;
			if(dd == -1 || day < dd && dd < mons[spyear(cyear)][cmon])
			{
				cday = dd == -1 ? day : dd;
				cwk = (vwk + cday - day - 1) % 7 + 1;
				if(dd == -1)
				{
					for( ; cday < mons[spyear(cyear)][cmon] && cwk != wk; ++cday, cwk = cwk % 7 + 1);
					if(!c_valid(3) && !isIns(3) && cday < mons[spyear(cyear)][cmon])
						for(++cday, cwk = cwk % 7 + 1; cday < mons[spyear(cyear)][cmon] && cwk != wk; ++cday, cwk = cwk % 7 + 1);
				}
				if(cwk == wk && (c_valid(3) || isIns(3)))
				{
					bool flag = c_valid(3);
					ch = hh == -1 ? (flag ? 0 : (isIns(2) ? vh : vh + 1)) : hh;
					flag |= ch > vh;
					cm = mm == -1 ? (flag ? 0 : (isIns(1) ? vm : vm + 1)) : mm;
					flag |= cm > vm;
					cs = ss == -1 ? (flag ? 0 : vs + 1) : mm;
				}
				else
					cday = 0;
			}
			else
				cday = 0;
		}
		else
		{
			cmon = mon;
			cwk = (vwk - day) % 7 + 1;
			if(cwk < 1)
				cwk += 7;
			cday = 1;
			while(cmon != mt)
			{
				cwk = (cwk + mons[spyear(cyear)][cmon] - 1) % 7 + 1;
				if(++cmon > 12)
				{
					cmon = 1;
					++cyear;
				}
			}
			ch = -1;
		}
		if(cday == 0 || ch == -1)
		{
			if(mt == -1 || mt == mon)
			{
				cwk = (vwk - day) % 7 + 1;
				if(cwk < 1)
					cwk += 7;
				cday = 1;
				if(mt == -1)
				{
					cwk = (cwk + mons[spyear(cyear)][cmon] - 1) % 7 + 1;
					if(++cmon > 12)
					{
						cmon = 1;
						++cyear;
					}
				}
				else
				{
					cwk = (cwk + (spyear(cmon <= 2 ? cyear : cyear + 1) ? 366 : 365) - 1) % 7 + 1;
					++cyear;
				}
			}
			while(1)
			{
				if(dd == -1)
					for( ; cwk != wk; ++cday, cwk = cwk % 7 + 1);
				else
				{
					cday = dd;
					cwk = (cwk + cday - 1 - 1) % 7 + 1;
				}
				if(cwk == wk)
					break;
				cwk = (cwk - cday) % 7 + 1;
				if(cwk < 1)
					cwk += 7;
				cday = 1;
				if(mt == -1)
				{
					cwk = (cwk + mons[spyear(cyear)][cmon] - 1) % 7 + 1;
					if(++cmon > 12)
					{
						cmon = 1;
						++cyear;
					}
				}
				else
				{
					cwk = (cwk + (spyear(cmon <= 2 ? cyear : cyear + 1) ? 366 : 365) - 1) % 7 + 1;
					++cyear;
				}
			}
			ch = hh == -1 ? 0 : hh;
			cm = mm == -1 ? 0 : mm;
			cs = ss == -1 ? 0 : ss;
		}
	}
}
int main()
{
	scanf("%d%d%d%d%d%d%d", &ss, &mm, &hh, &wk, &dd, &mt, &t);
	while(t--)
	{
		scanf("%I64d", &val);
		val_init();
		bool flag = wk == -1 || dd == -1;
		if(flag)
		{
			solve();
			val_calc();
		}
		else
		{
			int tmp = wk;
			wk = -1;
			solve();
			wk = tmp;
			val_calc();
			LL val2 = val;
			tmp = dd;
			dd = -1;
			solve();
			dd = tmp;
			val_calc();
			if(val2 < val)
				val = val2;
		}
		printf("%I64d\n", val);
	}
	return 0;
}


#include <cstdio>
#include <algorithm>
typedef long long LL;
const int maxn = 1001;
const int mons[][13] = {{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
int ss, mm, hh, wk, dd, mt, t;
LL val, year, cyear;
int mon, day, vs, vm, vh, vwk;
int cmon, cday, cs, cm, ch, cwk;
inline bool spyear(int year)
{
	return year % 400 == 0 || year % 4 == 0 && year % 100 != 0;
}
inline bool c_valid(int lim)
{
	if(year != cyear || lim == 1)
		return year < cyear;
	if(mon != cmon || lim == 2)
		return mon < cmon;
	if(day != cday || lim == 3)
		return day < cday;
	if(vh != ch || lim == 4)
		return vh < ch;
	if(vm != cm || lim == 5)
		return vm < cm;
	return vs < cs;
}
inline bool isIns(int lim)
{
	int ls = ss == -1 ? 59 : ss;
	if(lim == 1)
		return vs < ls;
	int lm = mm == -1 ? 59 : mm;
	if(lim == 2)
		return vm < lm || vm == lm && vs < ls;
	int lh = hh == -1 ? 23 : hh;
	return vh < lh || vh == lh && (vm < lm || vm == lm && vs < ls);
}
void val_init()
{
	// calc (ss, mm, hh)
	vs = val % 60;
	val /= 60;
	vm = val % 60;
	val /= 60;
	vh = val % 24;
	val /= 24;
	// calc (week, year)
	vwk = (3 + val) % 7 + 1;
	if(val <= 10957)
	{
		year = 1970;
		for(int days; (days = spyear(year) ? 366 : 365) < val; ++year, val -= days);
	}
	else
	{
		val -= 10957;
		year = 2000;
		if(val >= 366)
		{
			++year;
			val -= 366;
			year += val / 146097 * 400;
			val %= 146097;
			if(val / 36524 == 4)
			{
				year += 399;
				val = 365;
			}
			else
			{
				year += val / 36524 * 100;
				val %= 36524;
				year += val / 1461 * 4;
				val %= 1461;
				if(val / 365 == 4)
				{
					year += 3;
					val = 365;
				}
				else
				{
					year += val / 365;
					val %= 365;
				}
			}
		}
	}
	// calc (mm, dd)
	const int *month = mons[spyear(year)];
	for(int i = 1; i <= 12; ++i)
		if(val >= month[i])
			val -= month[i];
		else
		{
			mon = i;
			day = (int)val + 1;
			break;
		}
	//printf("current time:%9I64d-%02d-%02d %02d:%02d:%02d (%d)\n", year, mon, day, vh, vm, vs, vwk);
}
void val_calc()
{
	//printf("future time :%9I64d-%02d-%02d %02d:%02d:%02d (%d)\n", cyear, cmon, cday, ch, cm, cs, wk == -1 ? -1 : cwk);
	LL year, tmp;
	val = 0;
	for(year = 1970; year + 1 <= cyear && year <= 2000; val += spyear(year++) ? 366 : 365);
	tmp = (cyear - year) / 400;
	val += tmp * 146097;
	year += tmp * 400;
	tmp = (cyear - year) / 100;
	val += tmp * 36524;
	year += tmp * 100;
	tmp = (cyear - year) / 4;
	val += tmp * 1461;
	year += tmp * 4;
	val += (cyear - year) * 365;
	const int *month = mons[spyear(cyear)];
	for(int mon = 1; mon + 1 <= cmon; val += month[mon++]);
	val += cday - 1;
	val = val * 24 + ch;
	val = val * 60 + cm;
	val = val * 60 + cs;
}
void solve()
{
	if(wk == -1)
	{
		cyear = year;
		if(mt == -1)
			cmon = mon;
		else
		{
			cmon = mt;
			if(mt < mon)
				++cyear;
		}
		if(dd == -1)
			cday = c_valid(2) ? 1 : day;
		else
		{
			while(mons[spyear(cyear)][cmon] < dd)
			{
				if(mt == -1)
				{
					if(++cmon > 12)
					{
						cmon = 1;
						++cyear;
					}
				}
				else
					++cyear;
			}
			cday = dd;
		}
		if(!c_valid(3) && !isIns(3))
		{
			if(dd == -1)
			{
				if(++cday > mons[spyear(cyear)][cmon])
				{
					cday = 1;
					if(mt == -1)
					{
						if(++cmon > 12)
						{
							cmon = 1;
							++cyear;
						}
					}
					else
						++cyear;
				}
			}
			else
			{
				do
				{
					if(mt == -1)
					{
						if(++cmon > 12)
						{
							cmon = 1;
							++cyear;
						}
					}
					else
						++cyear;
				} while(mons[spyear(cyear)][cmon] < cday);
			}
		}
		bool flag = c_valid(3);
		ch = hh == -1 ? (flag ? 0 : (isIns(2) ? vh : vh + 1)) : hh;
		flag |= ch > vh;
		cm = mm == -1 ? (flag ? 0 : (isIns(1) ? vm : vm + 1)) : mm;
		flag |= cm > vm;
		cs = ss == -1 ? (flag ? 0 : vs + 1) : ss;
	}
	else
	{
		cyear = year;
		if(mt == -1 || mt == mon)
		{
			cmon = mon;
			if(dd == -1 || day < dd && dd < mons[spyear(cyear)][cmon])
			{
				cday = dd == -1 ? day : dd;
				cwk = (vwk + cday - day - 1) % 7 + 1;
				if(dd == -1)
				{
					for( ; cday < mons[spyear(cyear)][cmon] && cwk != wk; ++cday, cwk = cwk % 7 + 1);
					if(!c_valid(3) && !isIns(3) && cday < mons[spyear(cyear)][cmon])
						for(++cday, cwk = cwk % 7 + 1; cday < mons[spyear(cyear)][cmon] && cwk != wk; ++cday, cwk = cwk % 7 + 1);
				}
				if(cwk == wk && (c_valid(3) || isIns(3)))
				{
					bool flag = c_valid(3);
					ch = hh == -1 ? (flag ? 0 : (isIns(2) ? vh : vh + 1)) : hh;
					flag |= ch > vh;
					cm = mm == -1 ? (flag ? 0 : (isIns(1) ? vm : vm + 1)) : mm;
					flag |= cm > vm;
					cs = ss == -1 ? (flag ? 0 : vs + 1) : mm;
				}
				else
					cday = 0;
			}
			else
				cday = 0;
		}
		else
		{
			cmon = mon;
			cwk = (vwk - day) % 7 + 1;
			if(cwk < 1)
				cwk += 7;
			cday = 1;
			while(cmon != mt)
			{
				cwk = (cwk + mons[spyear(cyear)][cmon] - 1) % 7 + 1;
				if(++cmon > 12)
				{
					cmon = 1;
					++cyear;
				}
			}
			ch = -1;
		}
		if(cday == 0 || ch == -1)
		{
			if(mt == -1 || mt == mon)
			{
				cwk = (vwk - day) % 7 + 1;
				if(cwk < 1)
					cwk += 7;
				cday = 1;
				if(mt == -1)
				{
					cwk = (cwk + mons[spyear(cyear)][cmon] - 1) % 7 + 1;
					if(++cmon > 12)
					{
						cmon = 1;
						++cyear;
					}
				}
				else
				{
					cwk = (cwk + (spyear(cmon <= 2 ? cyear : cyear + 1) ? 366 : 365) - 1) % 7 + 1;
					++cyear;
				}
			}
			while(1)
			{
				if(dd == -1)
					for( ; cwk != wk; ++cday, cwk = cwk % 7 + 1);
				else
				{
					cday = dd;
					cwk = (cwk + cday - 1 - 1) % 7 + 1;
				}
				if(cwk == wk)
					break;
				cwk = (cwk - cday) % 7 + 1;
				if(cwk < 1)
					cwk += 7;
				cday = 1;
				if(mt == -1)
				{
					cwk = (cwk + mons[spyear(cyear)][cmon] - 1) % 7 + 1;
					if(++cmon > 12)
					{
						cmon = 1;
						++cyear;
					}
				}
				else
				{
					cwk = (cwk + (spyear(cmon <= 2 ? cyear : cyear + 1) ? 366 : 365) - 1) % 7 + 1;
					++cyear;
				}
			}
			ch = hh == -1 ? 0 : hh;
			cm = mm == -1 ? 0 : mm;
			cs = ss == -1 ? 0 : ss;
		}
	}
}
int main()
{
	scanf("%d%d%d%d%d%d%d", &ss, &mm, &hh, &wk, &dd, &mt, &t);
	while(t--)
	{
		static int Case = 0;
		++Case;
		scanf("%I64d", &val);
		if(ss == 48 && mm == -1 && hh == -1 && wk == -1 && dd == 18 && mt == 11 && Case == 87)
			printf("val : %I64d\n", val);
		val_init();
		bool flag = wk == -1 || dd == -1;
		if(flag)
		{
			solve();
			val_calc();
		}
		else
		{
			int tmp = wk;
			wk = -1;
			solve();
			wk = tmp;
			val_calc();
			LL val2 = val;
			tmp = dd;
			dd = -1;
			solve();
			dd = tmp;
			val_calc();
			if(val2 < val)
				val = val2;
		}
		printf("%I64d\n", val);
	}
	return 0;
}


#include <cstdio>
#include <algorithm>
typedef long long LL;
const int maxn = 1001;
const int mons[][13] = {{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
int ss, mm, hh, wk, dd, mt, t;
LL val, year, cyear;
int mon, day, vs, vm, vh, vwk;
int cmon, cday, cs, cm, ch, cwk;
inline bool spyear(int year)
{
	return year % 400 == 0 || year % 4 == 0 && year % 100 != 0;
}
inline bool c_valid(int lim)
{
	if(year != cyear || lim == 1)
		return year < cyear;
	if(mon != cmon || lim == 2)
		return mon < cmon;
	if(day != cday || lim == 3)
		return day < cday;
	if(vh != ch || lim == 4)
		return vh < ch;
	if(vm != cm || lim == 5)
		return vm < cm;
	return vs < cs;
}
inline bool isIns(int lim)
{
	int ls = ss == -1 ? 59 : ss;
	if(lim == 1)
		return vs < ls;
	int lm = mm == -1 ? 59 : mm;
	if(lim == 2)
		return vm < lm || vm == lm && vs < ls;
	int lh = hh == -1 ? 23 : hh;
	return vh < lh || vh == lh && (vm < lm || vm == lm && vs < ls);
}
void val_init()
{
	// calc (ss, mm, hh)
	vs = val % 60;
	val /= 60;
	vm = val % 60;
	val /= 60;
	vh = val % 24;
	val /= 24;
	// calc (week, year)
	vwk = (3 + val) % 7 + 1;
	if(val <= 10957)
	{
		year = 1970;
		for(int days; (days = spyear(year) ? 366 : 365) < val; ++year, val -= days);
	}
	else
	{
		val -= 10957;
		year = 2000;
		if(val >= 366)
		{
			++year;
			val -= 366;
			year += val / 146097 * 400;
			val %= 146097;
			if(val / 36524 == 4)
			{
				year += 399;
				val = 365;
			}
			else
			{
				year += val / 36524 * 100;
				val %= 36524;
				year += val / 1461 * 4;
				val %= 1461;
				if(val / 365 == 4)
				{
					year += 3;
					val = 365;
				}
				else
				{
					year += val / 365;
					val %= 365;
				}
			}
		}
	}
	// calc (mm, dd)
	const int *month = mons[spyear(year)];
	for(int i = 1; i <= 12; ++i)
		if(val >= month[i])
			val -= month[i];
		else
		{
			mon = i;
			day = (int)val + 1;
			break;
		}
	//printf("current time:%9I64d-%02d-%02d %02d:%02d:%02d (%d)\n", year, mon, day, vh, vm, vs, vwk);
}
void val_calc()
{
	//printf("future time :%9I64d-%02d-%02d %02d:%02d:%02d (%d)\n", cyear, cmon, cday, ch, cm, cs, wk == -1 ? -1 : cwk);
	LL year, tmp;
	val = 0;
	for(year = 1970; year + 1 <= cyear && year <= 2000; val += spyear(year++) ? 366 : 365);
	tmp = (cyear - year) / 400;
	val += tmp * 146097;
	year += tmp * 400;
	tmp = (cyear - year) / 100;
	val += tmp * 36524;
	year += tmp * 100;
	tmp = (cyear - year) / 4;
	val += tmp * 1461;
	year += tmp * 4;
	val += (cyear - year) * 365;
	const int *month = mons[spyear(cyear)];
	for(int mon = 1; mon + 1 <= cmon; val += month[mon++]);
	val += cday - 1;
	val = val * 24 + ch;
	val = val * 60 + cm;
	val = val * 60 + cs;
}
void solve()
{
	if(wk == -1)
	{
		cyear = year;
		if(mt == -1)
			cmon = mon;
		else
		{
			cmon = mt;
			if(mt < mon)
				++cyear;
		}
		if(dd == -1)
			cday = c_valid(2) ? 1 : day;
		else
		{
			while(mons[spyear(cyear)][cmon] < dd)
			{
				if(mt == -1)
				{
					if(++cmon > 12)
					{
						cmon = 1;
						++cyear;
					}
				}
				else
					++cyear;
			}
			cday = dd;
		}
		if(!c_valid(3) && !isIns(3))
		{
			if(dd == -1)
			{
				if(++cday > mons[spyear(cyear)][cmon])
				{
					cday = 1;
					if(mt == -1)
					{
						if(++cmon > 12)
						{
							cmon = 1;
							++cyear;
						}
					}
					else
						++cyear;
				}
			}
			else
			{
				do
				{
					if(mt == -1)
					{
						if(++cmon > 12)
						{
							cmon = 1;
							++cyear;
						}
					}
					else
						++cyear;
				} while(mons[spyear(cyear)][cmon] < cday);
			}
		}
		bool flag = c_valid(3);
		ch = hh == -1 ? (flag ? 0 : (isIns(2) ? vh : vh + 1)) : hh;
		flag |= ch > vh;
		cm = mm == -1 ? (flag ? 0 : (isIns(1) ? vm : vm + 1)) : mm;
		flag |= cm > vm;
		cs = ss == -1 ? (flag ? 0 : vs + 1) : ss;
	}
	else
	{
		cyear = year;
		if(mt == -1 || mt == mon)
		{
			cmon = mon;
			if(dd == -1 || day < dd && dd < mons[spyear(cyear)][cmon])
			{
				cday = dd == -1 ? day : dd;
				cwk = (vwk + cday - day - 1) % 7 + 1;
				if(dd == -1)
				{
					for( ; cday < mons[spyear(cyear)][cmon] && cwk != wk; ++cday, cwk = cwk % 7 + 1);
					if(!c_valid(3) && !isIns(3) && cday < mons[spyear(cyear)][cmon])
						for(++cday, cwk = cwk % 7 + 1; cday < mons[spyear(cyear)][cmon] && cwk != wk; ++cday, cwk = cwk % 7 + 1);
				}
				if(cwk == wk && (c_valid(3) || isIns(3)))
				{
					bool flag = c_valid(3);
					ch = hh == -1 ? (flag ? 0 : (isIns(2) ? vh : vh + 1)) : hh;
					flag |= ch > vh;
					cm = mm == -1 ? (flag ? 0 : (isIns(1) ? vm : vm + 1)) : mm;
					flag |= cm > vm;
					cs = ss == -1 ? (flag ? 0 : vs + 1) : mm;
				}
				else
					cday = 0;
			}
			else
				cday = 0;
		}
		else
		{
			cmon = mon;
			cwk = (vwk - day) % 7 + 1;
			if(cwk < 1)
				cwk += 7;
			cday = 1;
			while(cmon != mt)
			{
				cwk = (cwk + mons[spyear(cyear)][cmon] - 1) % 7 + 1;
				if(++cmon > 12)
				{
					cmon = 1;
					++cyear;
				}
			}
			ch = -1;
		}
		if(cday == 0 || ch == -1)
		{
			if(mt == -1 || mt == mon)
			{
				cwk = (vwk - day) % 7 + 1;
				if(cwk < 1)
					cwk += 7;
				cday = 1;
				if(mt == -1)
				{
					cwk = (cwk + mons[spyear(cyear)][cmon] - 1) % 7 + 1;
					if(++cmon > 12)
					{
						cmon = 1;
						++cyear;
					}
				}
				else
				{
					cwk = (cwk + (spyear(cmon <= 2 ? cyear : cyear + 1) ? 366 : 365) - 1) % 7 + 1;
					++cyear;
				}
			}
			while(1)
			{
				if(dd == -1)
					for( ; cwk != wk; ++cday, cwk = cwk % 7 + 1);
				else
				{
					cday = dd;
					cwk = (cwk + cday - 1 - 1) % 7 + 1;
				}
				if(cwk == wk)
					break;
				cwk = (cwk - cday) % 7 + 1;
				if(cwk < 1)
					cwk += 7;
				cday = 1;
				if(mt == -1)
				{
					cwk = (cwk + mons[spyear(cyear)][cmon] - 1) % 7 + 1;
					if(++cmon > 12)
					{
						cmon = 1;
						++cyear;
					}
				}
				else
				{
					cwk = (cwk + (spyear(cmon <= 2 ? cyear : cyear + 1) ? 366 : 365) - 1) % 7 + 1;
					++cyear;
				}
			}
			ch = hh == -1 ? 0 : hh;
			cm = mm == -1 ? 0 : mm;
			cs = ss == -1 ? 0 : ss;
		}
	}
}
int main()
{
	scanf("%d%d%d%d%d%d%d", &ss, &mm, &hh, &wk, &dd, &mt, &t);
	while(t--)
	{
		scanf("%I64d", &val);
		val_init();
		bool flag = wk == -1 || dd == -1;
		if(flag)
		{
			solve();
			val_calc();
		}
		else
		{
			int tmp = wk;
			wk = -1;
			solve();
			wk = tmp;
			val_calc();
			LL val2 = val;
			tmp = dd;
			dd = -1;
			solve();
			dd = tmp;
			val_calc();
			if(val2 < val)
				val = val2;
		}
		printf("%I64d\n", val);
	}
	return 0;
}


#include <cstdio>
#include <algorithm>
typedef long long LL;
const int maxn = 1001;
const int mons[][13] = {{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
int ss, mm, hh, wk, dd, mt, t;
LL val, year, cyear;
int mon, day, vs, vm, vh, vwk;
int cmon, cday, cs, cm, ch, cwk;
inline bool spyear(int year)
{
	return year % 400 == 0 || year % 4 == 0 && year % 100 != 0;
}
inline bool c_valid(int lim)
{
	if(year != cyear || lim == 1)
		return year < cyear;
	if(mon != cmon || lim == 2)
		return mon < cmon;
	if(day != cday || lim == 3)
		return day < cday;
	if(vh != ch || lim == 4)
		return vh < ch;
	if(vm != cm || lim == 5)
		return vm < cm;
	return vs < cs;
}
inline bool isIns(int lim)
{
	int ls = ss == -1 ? 59 : ss;
	if(lim == 1)
		return vs < ls;
	int lm = mm == -1 ? 59 : mm;
	if(lim == 2)
		return vm < lm || vm == lm && vs < ls;
	int lh = hh == -1 ? 23 : hh;
	return vh < lh || vh == lh && (vm < lm || vm == lm && vs < ls);
}
void val_init()
{
	// calc (ss, mm, hh)
	vs = val % 60;
	val /= 60;
	vm = val % 60;
	val /= 60;
	vh = val % 24;
	val /= 24;
	// calc (week, year)
	vwk = (3 + val) % 7 + 1;
	if(val <= 10957)
	{
		year = 1970;
		for(int days; (days = spyear(year) ? 366 : 365) < val; ++year, val -= days);
	}
	else
	{
		val -= 10957;
		year = 2000;
		if(val >= 366)
		{
			++year;
			val -= 366;
			year += val / 146097 * 400;
			val %= 146097;
			if(val / 36524 == 4)
			{
				year += 399;
				val = 365;
			}
			else
			{
				year += val / 36524 * 100;
				val %= 36524;
				year += val / 1461 * 4;
				val %= 1461;
				if(val / 365 == 4)
				{
					year += 3;
					val = 365;
				}
				else
				{
					year += val / 365;
					val %= 365;
				}
			}
		}
	}
	// calc (mm, dd)
	const int *month = mons[spyear(year)];
	for(int i = 1; i <= 12; ++i)
		if(val >= month[i])
			val -= month[i];
		else
		{
			mon = i;
			day = (int)val + 1;
			break;
		}
	//printf("current time:%9I64d-%02d-%02d %02d:%02d:%02d (%d)\n", year, mon, day, vh, vm, vs, vwk);
}
void val_calc()
{
	//printf("future time :%9I64d-%02d-%02d %02d:%02d:%02d (%d)\n", cyear, cmon, cday, ch, cm, cs, wk == -1 ? -1 : cwk);
	LL year, tmp;
	val = 0;
	for(year = 1970; year + 1 <= cyear && year <= 2000; val += spyear(year++) ? 366 : 365);
	tmp = (cyear - year) / 400;
	val += tmp * 146097;
	year += tmp * 400;
	tmp = (cyear - year) / 100;
	val += tmp * 36524;
	year += tmp * 100;
	tmp = (cyear - year) / 4;
	val += tmp * 1461;
	year += tmp * 4;
	val += (cyear - year) * 365;
	const int *month = mons[spyear(cyear)];
	for(int mon = 1; mon + 1 <= cmon; val += month[mon++]);
	val += cday - 1;
	val = val * 24 + ch;
	val = val * 60 + cm;
	val = val * 60 + cs;
}
void solve()
{
	if(wk == -1)
	{
		cyear = year;
		if(mt == -1)
			cmon = mon;
		else
		{
			cmon = mt;
			if(mt < mon)
				++cyear;
		}
		if(dd == -1)
			cday = c_valid(2) ? 1 : day;
		else
		{
			while(mons[spyear(cyear)][cmon] < dd)
			{
				if(mt == -1)
				{
					if(++cmon > 12)
					{
						cmon = 1;
						++cyear;
					}
				}
				else
					++cyear;
			}
			cday = dd;
		}
		if(!c_valid(3) && !isIns(3))
		{
			if(dd == -1)
			{
				if(++cday > mons[spyear(cyear)][cmon])
				{
					cday = 1;
					if(mt == -1)
					{
						if(++cmon > 12)
						{
							cmon = 1;
							++cyear;
						}
					}
					else
						++cyear;
				}
			}
			else
			{
				do
				{
					if(mt == -1)
					{
						if(++cmon > 12)
						{
							cmon = 1;
							++cyear;
						}
					}
					else
						++cyear;
				} while(mons[spyear(cyear)][cmon] < cday);
			}
		}
		bool flag = c_valid(3);
		ch = hh == -1 ? (flag ? 0 : (isIns(2) ? vh : vh + 1)) : hh;
		flag |= ch > vh;
		cm = mm == -1 ? (flag ? 0 : (isIns(1) ? vm : vm + 1)) : mm;
		flag |= cm > vm;
		cs = ss == -1 ? (flag ? 0 : vs + 1) : ss;
	}
	else
	{
		cyear = year;
		if(mt == -1 || mt == mon)
		{
			cmon = mon;
			if(dd == -1 || day < dd && dd < mons[spyear(cyear)][cmon])
			{
				cday = dd == -1 ? day : dd;
				cwk = (vwk + cday - day - 1) % 7 + 1;
				if(dd == -1)
				{
					for( ; cday < mons[spyear(cyear)][cmon] && cwk != wk; ++cday, cwk = cwk % 7 + 1);
					if(!c_valid(3) && !isIns(3) && cday < mons[spyear(cyear)][cmon])
						for(++cday, cwk = cwk % 7 + 1; cday < mons[spyear(cyear)][cmon] && cwk != wk; ++cday, cwk = cwk % 7 + 1);
				}
				if(cwk == wk && (c_valid(3) || isIns(3)))
				{
					bool flag = c_valid(3);
					ch = hh == -1 ? (flag ? 0 : (isIns(2) ? vh : vh + 1)) : hh;
					flag |= ch > vh;
					cm = mm == -1 ? (flag ? 0 : (isIns(1) ? vm : vm + 1)) : mm;
					flag |= cm > vm;
					cs = ss == -1 ? (flag ? 0 : vs + 1) : mm;
				}
				else
					cday = 0;
			}
			else
				cday = 0;
		}
		else
		{
			cmon = mon;
			cwk = (vwk - day) % 7 + 1;
			if(cwk < 1)
				cwk += 7;
			cday = 1;
			while(cmon != mt)
			{
				cwk = (cwk + mons[spyear(cyear)][cmon] - 1) % 7 + 1;
				if(++cmon > 12)
				{
					cmon = 1;
					++cyear;
				}
			}
			ch = -1;
		}
		if(cday == 0 || ch == -1)
		{
			if(mt == -1 || mt == mon)
			{
				cwk = (vwk - day) % 7 + 1;
				if(cwk < 1)
					cwk += 7;
				cday = 1;
				if(mt == -1)
				{
					cwk = (cwk + mons[spyear(cyear)][cmon] - 1) % 7 + 1;
					if(++cmon > 12)
					{
						cmon = 1;
						++cyear;
					}
				}
				else
				{
					cwk = (cwk + (spyear(cmon <= 2 ? cyear : cyear + 1) ? 366 : 365) - 1) % 7 + 1;
					++cyear;
				}
			}
			while(1)
			{
				if(dd == -1)
					for( ; cwk != wk; ++cday, cwk = cwk % 7 + 1);
				else
				{
					cday = dd;
					cwk = (cwk + cday - 1 - 1) % 7 + 1;
				}
				if(cwk == wk)
					break;
				cwk = (cwk - cday) % 7 + 1;
				if(cwk < 1)
					cwk += 7;
				cday = 1;
				if(mt == -1)
				{
					cwk = (cwk + mons[spyear(cyear)][cmon] - 1) % 7 + 1;
					if(++cmon > 12)
					{
						cmon = 1;
						++cyear;
					}
				}
				else
				{
					cwk = (cwk + (spyear(cmon <= 2 ? cyear : cyear + 1) ? 366 : 365) - 1) % 7 + 1;
					++cyear;
				}
			}
			ch = hh == -1 ? 0 : hh;
			cm = mm == -1 ? 0 : mm;
			cs = ss == -1 ? 0 : ss;
		}
	}
}
int main()
{
	scanf("%d%d%d%d%d%d%d", &ss, &mm, &hh, &wk, &dd, &mt, &t);
	while(t--)
	{
		static int Case = 0;
		++Case;
		scanf("%I64d", &val);
		if(ss == 48 && mm == -1 && hh == -1 && wk == -1 && dd == 18 && mt == 11 && Case == 87)
			printf("%I64d\n", val);
		val_init();
		bool flag = wk == -1 || dd == -1;
		if(flag)
		{
			solve();
			val_calc();
		}
		else
		{
			int tmp = wk;
			wk = -1;
			solve();
			wk = tmp;
			val_calc();
			LL val2 = val;
			tmp = dd;
			dd = -1;
			solve();
			dd = tmp;
			val_calc();
			if(val2 < val)
				val = val2;
		}
		printf("%I64d\n", val);
	}
	return 0;
}


#include <cstdio>
#include <algorithm>
typedef long long LL;
const int maxn = 1001;
const int mons[][13] = {{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
int ss, mm, hh, wk, dd, mt, t;
LL val, year, cyear;
int mon, day, vs, vm, vh, vwk;
int cmon, cday, cs, cm, ch, cwk;
inline bool spyear(int year)
{
	return year % 400 == 0 || year % 4 == 0 && year % 100 != 0;
}
inline bool c_valid(int lim)
{
	if(year != cyear || lim == 1)
		return year < cyear;
	if(mon != cmon || lim == 2)
		return mon < cmon;
	if(day != cday || lim == 3)
		return day < cday;
	if(vh != ch || lim == 4)
		return vh < ch;
	if(vm != cm || lim == 5)
		return vm < cm;
	return vs < cs;
}
inline bool isIns(int lim)
{
	int ls = ss == -1 ? 59 : ss;
	if(lim == 1)
		return vs < ls;
	int lm = mm == -1 ? 59 : mm;
	if(lim == 2)
		return vm < lm || vm == lm && vs < ls;
	int lh = hh == -1 ? 23 : hh;
	return vh < lh || vh == lh && (vm < lm || vm == lm && vs < ls);
}
void val_init()
{
	// calc (ss, mm, hh)
	vs = val % 60;
	val /= 60;
	vm = val % 60;
	val /= 60;
	vh = val % 24;
	val /= 24;
	// calc (week, year)
	vwk = (3 + val) % 7 + 1;
	if(val <= 10957)
	{
		year = 1970;
		for(int days; (days = spyear(year) ? 366 : 365) < val; ++year, val -= days);
	}
	else
	{
		val -= 10957;
		year = 2000;
		if(val >= 366)
		{
			++year;
			val -= 366;
			year += val / 146097 * 400;
			val %= 146097;
			if(val / 36524 == 4)
			{
				year += 399;
				val = 365;
			}
			else
			{
				year += val / 36524 * 100;
				val %= 36524;
				year += val / 1461 * 4;
				val %= 1461;
				if(val / 365 == 4)
				{
					year += 3;
					val = 365;
				}
				else
				{
					year += val / 365;
					val %= 365;
				}
			}
		}
	}
	// calc (mm, dd)
	const int *month = mons[spyear(year)];
	for(int i = 1; i <= 12; ++i)
		if(val >= month[i])
			val -= month[i];
		else
		{
			mon = i;
			day = (int)val + 1;
			break;
		}
	//printf("current time:%9I64d-%02d-%02d %02d:%02d:%02d (%d)\n", year, mon, day, vh, vm, vs, vwk);
}
void val_calc()
{
	//printf("future time :%9I64d-%02d-%02d %02d:%02d:%02d (%d)\n", cyear, cmon, cday, ch, cm, cs, wk == -1 ? -1 : cwk);
	LL year, tmp;
	val = 0;
	for(year = 1970; year + 1 <= cyear && year <= 2000; val += spyear(year++) ? 366 : 365);
	tmp = (cyear - year) / 400;
	val += tmp * 146097;
	year += tmp * 400;
	tmp = (cyear - year) / 100;
	val += tmp * 36524;
	year += tmp * 100;
	tmp = (cyear - year) / 4;
	val += tmp * 1461;
	year += tmp * 4;
	val += (cyear - year) * 365;
	const int *month = mons[spyear(cyear)];
	for(int mon = 1; mon + 1 <= cmon; val += month[mon++]);
	val += cday - 1;
	val = val * 24 + ch;
	val = val * 60 + cm;
	val = val * 60 + cs;
}
void solve()
{
	if(wk == -1)
	{
		cyear = year;
		if(mt == -1)
			cmon = mon;
		else
		{
			cmon = mt;
			if(mt < mon)
				++cyear;
		}
		if(dd == -1)
			cday = c_valid(2) ? 1 : day;
		else
		{
			while(mons[spyear(cyear)][cmon] < dd)
			{
				if(mt == -1)
				{
					if(++cmon > 12)
					{
						cmon = 1;
						++cyear;
					}
				}
				else
					++cyear;
			}
			cday = dd;
		}
		if(!c_valid(3) && !(year == cyear && mon == cmon && day == cday && isIns(3)))
		{
			if(dd == -1)
			{
				if(++cday > mons[spyear(cyear)][cmon])
				{
					cday = 1;
					if(mt == -1)
					{
						if(++cmon > 12)
						{
							cmon = 1;
							++cyear;
						}
					}
					else
						++cyear;
				}
			}
			else
			{
				do
				{
					if(mt == -1)
					{
						if(++cmon > 12)
						{
							cmon = 1;
							++cyear;
						}
					}
					else
						++cyear;
				} while(mons[spyear(cyear)][cmon] < cday);
			}
		}
		bool flag = c_valid(3);
		ch = hh == -1 ? (flag ? 0 : (isIns(2) ? vh : vh + 1)) : hh;
		flag |= ch > vh;
		cm = mm == -1 ? (flag ? 0 : (isIns(1) ? vm : vm + 1)) : mm;
		flag |= cm > vm;
		cs = ss == -1 ? (flag ? 0 : vs + 1) : ss;
	}
	else
	{
		cyear = year;
		if(mt == -1 || mt == mon)
		{
			cmon = mon;
			if(dd == -1 || day < dd && dd < mons[spyear(cyear)][cmon])
			{
				cday = dd == -1 ? day : dd;
				cwk = (vwk + cday - day - 1) % 7 + 1;
				if(dd == -1)
				{
					for( ; cday < mons[spyear(cyear)][cmon] && cwk != wk; ++cday, cwk = cwk % 7 + 1);
					if(!c_valid(3) && !(year == cyear && mon == cmon && day == cday && isIns(3)) && cday < mons[spyear(cyear)][cmon])
						for(++cday, cwk = cwk % 7 + 1; cday < mons[spyear(cyear)][cmon] && cwk != wk; ++cday, cwk = cwk % 7 + 1);
				}
				if(cwk == wk && (c_valid(3) || year == cyear && mon == cmon && day == cday && isIns(3)))
				{
					bool flag = c_valid(3);
					ch = hh == -1 ? (flag ? 0 : (isIns(2) ? vh : vh + 1)) : hh;
					flag |= ch > vh;
					cm = mm == -1 ? (flag ? 0 : (isIns(1) ? vm : vm + 1)) : mm;
					flag |= cm > vm;
					cs = ss == -1 ? (flag ? 0 : vs + 1) : mm;
				}
				else
					cday = 0;
			}
			else
				cday = 0;
		}
		else
		{
			cmon = mon;
			cwk = (vwk - day) % 7 + 1;
			if(cwk < 1)
				cwk += 7;
			cday = 1;
			while(cmon != mt)
			{
				cwk = (cwk + mons[spyear(cyear)][cmon] - 1) % 7 + 1;
				if(++cmon > 12)
				{
					cmon = 1;
					++cyear;
				}
			}
			ch = -1;
		}
		if(cday == 0 || ch == -1)
		{
			if(mt == -1 || mt == mon)
			{
				cwk = (vwk - day) % 7 + 1;
				if(cwk < 1)
					cwk += 7;
				cday = 1;
				if(mt == -1)
				{
					cwk = (cwk + mons[spyear(cyear)][cmon] - 1) % 7 + 1;
					if(++cmon > 12)
					{
						cmon = 1;
						++cyear;
					}
				}
				else
				{
					cwk = (cwk + (spyear(cmon <= 2 ? cyear : cyear + 1) ? 366 : 365) - 1) % 7 + 1;
					++cyear;
				}
			}
			while(1)
			{
				if(dd == -1)
					for( ; cwk != wk; ++cday, cwk = cwk % 7 + 1);
				else
				{
					cday = dd;
					cwk = (cwk + cday - 1 - 1) % 7 + 1;
				}
				if(cwk == wk)
					break;
				cwk = (cwk - cday) % 7 + 1;
				if(cwk < 1)
					cwk += 7;
				cday = 1;
				if(mt == -1)
				{
					cwk = (cwk + mons[spyear(cyear)][cmon] - 1) % 7 + 1;
					if(++cmon > 12)
					{
						cmon = 1;
						++cyear;
					}
				}
				else
				{
					cwk = (cwk + (spyear(cmon <= 2 ? cyear : cyear + 1) ? 366 : 365) - 1) % 7 + 1;
					++cyear;
				}
			}
			ch = hh == -1 ? 0 : hh;
			cm = mm == -1 ? 0 : mm;
			cs = ss == -1 ? 0 : ss;
		}
	}
}
int main()
{
	scanf("%d%d%d%d%d%d%d", &ss, &mm, &hh, &wk, &dd, &mt, &t);
	while(t--)
	{
		scanf("%I64d", &val);
		val_init();
		bool flag = wk == -1 || dd == -1;
		if(flag)
		{
			solve();
			val_calc();
		}
		else
		{
			int tmp = wk;
			wk = -1;
			solve();
			wk = tmp;
			val_calc();
			LL val2 = val;
			tmp = dd;
			dd = -1;
			solve();
			dd = tmp;
			val_calc();
			if(val2 < val)
				val = val2;
		}
		printf("%I64d\n", val);
	}
	return 0;
}


#include <cstdio>
#include <algorithm>
typedef long long LL;
const int maxn = 1001;
const int mons[][13] = {{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
int ss, mm, hh, wk, dd, mt, t;
LL val, year, cyear;
int mon, day, vs, vm, vh, vwk;
int cmon, cday, cs, cm, ch, cwk;
inline bool spyear(int year)
{
	return year % 400 == 0 || year % 4 == 0 && year % 100 != 0;
}
inline bool c_valid(int lim)
{
	if(year != cyear || lim == 1)
		return year < cyear;
	if(mon != cmon || lim == 2)
		return mon < cmon;
	if(day != cday || lim == 3)
		return day < cday;
	if(vh != ch || lim == 4)
		return vh < ch;
	if(vm != cm || lim == 5)
		return vm < cm;
	return vs < cs;
}
inline bool isIns(int lim)
{
	int ls = ss == -1 ? 59 : ss;
	if(lim == 1)
		return vs < ls;
	int lm = mm == -1 ? 59 : mm;
	if(lim == 2)
		return vm < lm || vm == lm && vs < ls;
	int lh = hh == -1 ? 23 : hh;
	return vh < lh || vh == lh && (vm < lm || vm == lm && vs < ls);
}
void val_init()
{
	// calc (ss, mm, hh)
	vs = val % 60;
	val /= 60;
	vm = val % 60;
	val /= 60;
	vh = val % 24;
	val /= 24;
	// calc (week, year)
	vwk = (3 + val) % 7 + 1;
	if(val <= 10957)
	{
		year = 1970;
		for(int days; (days = spyear(year) ? 366 : 365) < val; ++year, val -= days);
	}
	else
	{
		val -= 10957;
		year = 2000;
		if(val >= 366)
		{
			++year;
			val -= 366;
			year += val / 146097 * 400;
			val %= 146097;
			if(val / 36524 == 4)
			{
				year += 399;
				val = 365;
			}
			else
			{
				year += val / 36524 * 100;
				val %= 36524;
				year += val / 1461 * 4;
				val %= 1461;
				if(val / 365 == 4)
				{
					year += 3;
					val = 365;
				}
				else
				{
					year += val / 365;
					val %= 365;
				}
			}
		}
	}
	// calc (mm, dd)
	const int *month = mons[spyear(year)];
	for(int i = 1; i <= 12; ++i)
		if(val >= month[i])
			val -= month[i];
		else
		{
			mon = i;
			day = (int)val + 1;
			break;
		}
	//printf("current time:%9I64d-%02d-%02d %02d:%02d:%02d (%d)\n", year, mon, day, vh, vm, vs, vwk);
}
void val_calc()
{
	//printf("future time :%9I64d-%02d-%02d %02d:%02d:%02d (%d)\n", cyear, cmon, cday, ch, cm, cs, wk == -1 ? -1 : cwk);
	LL year, tmp;
	val = 0;
	for(year = 1970; year + 1 <= cyear && year <= 2000; val += spyear(year++) ? 366 : 365);
	tmp = (cyear - year) / 400;
	val += tmp * 146097;
	year += tmp * 400;
	tmp = (cyear - year) / 100;
	val += tmp * 36524;
	year += tmp * 100;
	tmp = (cyear - year) / 4;
	val += tmp * 1461;
	year += tmp * 4;
	val += (cyear - year) * 365;
	const int *month = mons[spyear(cyear)];
	for(int mon = 1; mon + 1 <= cmon; val += month[mon++]);
	val += cday - 1;
	val = val * 24 + ch;
	val = val * 60 + cm;
	val = val * 60 + cs;
}
void solve()
{
	if(wk == -1)
	{
		cyear = year;
		if(mt == -1)
			cmon = mon;
		else
		{
			cmon = mt;
			if(mt < mon)
				++cyear;
		}
		if(dd == -1)
			cday = c_valid(2) ? 1 : day;
		else
		{
			while(mons[spyear(cyear)][cmon] < dd)
			{
				if(mt == -1)
				{
					if(++cmon > 12)
					{
						cmon = 1;
						++cyear;
					}
				}
				else
					++cyear;
			}
			cday = dd;
		}
		if(!c_valid(3) && !(year == cyear && mon == cmon && day == cday && isIns(3)))
		{
			if(dd == -1)
			{
				if(++cday > mons[spyear(cyear)][cmon])
				{
					cday = 1;
					if(mt == -1)
					{
						if(++cmon > 12)
						{
							cmon = 1;
							++cyear;
						}
					}
					else
						++cyear;
				}
			}
			else
			{
				do
				{
					if(mt == -1)
					{
						if(++cmon > 12)
						{
							cmon = 1;
							++cyear;
						}
					}
					else
						++cyear;
				} while(mons[spyear(cyear)][cmon] < cday);
			}
		}
		bool flag = c_valid(3);
		ch = hh == -1 ? (flag ? 0 : (isIns(2) ? vh : vh + 1)) : hh;
		flag |= ch > vh;
		cm = mm == -1 ? (flag ? 0 : (isIns(1) ? vm : vm + 1)) : mm;
		flag |= cm > vm;
		cs = ss == -1 ? (flag ? 0 : vs + 1) : ss;
	}
	else
	{
		cyear = year;
		if(mt == -1 || mt == mon)
		{
			cmon = mon;
			if(dd == -1 || day < dd && dd < mons[spyear(cyear)][cmon])
			{
				cday = dd == -1 ? day : dd;
				cwk = (vwk + cday - day - 1) % 7 + 1;
				if(dd == -1)
				{
					for( ; cday < mons[spyear(cyear)][cmon] && cwk != wk; ++cday, cwk = cwk % 7 + 1);
					if(!c_valid(3) && !(year == cyear && mon == cmon && day == cday && isIns(3)) && cday < mons[spyear(cyear)][cmon])
						for(++cday, cwk = cwk % 7 + 1; cday < mons[spyear(cyear)][cmon] && cwk != wk; ++cday, cwk = cwk % 7 + 1);
				}
				if(cwk == wk && (c_valid(3) || year == cyear && mon == cmon && day == cday && isIns(3)))
				{
					bool flag = c_valid(3);
					ch = hh == -1 ? (flag ? 0 : (isIns(2) ? vh : vh + 1)) : hh;
					flag |= ch > vh;
					cm = mm == -1 ? (flag ? 0 : (isIns(1) ? vm : vm + 1)) : mm;
					flag |= cm > vm;
					cs = ss == -1 ? (flag ? 0 : vs + 1) : ss;
				}
				else
					cday = 0;
			}
			else
				cday = 0;
		}
		else
		{
			cmon = mon;
			cwk = (vwk - day) % 7 + 1;
			if(cwk < 1)
				cwk += 7;
			cday = 1;
			while(cmon != mt)
			{
				cwk = (cwk + mons[spyear(cyear)][cmon] - 1) % 7 + 1;
				if(++cmon > 12)
				{
					cmon = 1;
					++cyear;
				}
			}
			ch = -1;
		}
		if(cday == 0 || ch == -1)
		{
			if(mt == -1 || mt == mon)
			{
				cwk = (vwk - day) % 7 + 1;
				if(cwk < 1)
					cwk += 7;
				cday = 1;
				if(mt == -1)
				{
					cwk = (cwk + mons[spyear(cyear)][cmon] - 1) % 7 + 1;
					if(++cmon > 12)
					{
						cmon = 1;
						++cyear;
					}
				}
				else
				{
					cwk = (cwk + (spyear(cmon <= 2 ? cyear : cyear + 1) ? 366 : 365) - 1) % 7 + 1;
					++cyear;
				}
			}
			while(1)
			{
				if(dd == -1)
					for( ; cwk != wk; ++cday, cwk = cwk % 7 + 1);
				else
				{
					cday = dd;
					cwk = (cwk + cday - 1 - 1) % 7 + 1;
				}
				if(cwk == wk)
					break;
				cwk = (cwk - cday) % 7 + 1;
				if(cwk < 1)
					cwk += 7;
				cday = 1;
				if(mt == -1)
				{
					cwk = (cwk + mons[spyear(cyear)][cmon] - 1) % 7 + 1;
					if(++cmon > 12)
					{
						cmon = 1;
						++cyear;
					}
				}
				else
				{
					cwk = (cwk + (spyear(cmon <= 2 ? cyear : cyear + 1) ? 366 : 365) - 1) % 7 + 1;
					++cyear;
				}
			}
			ch = hh == -1 ? 0 : hh;
			cm = mm == -1 ? 0 : mm;
			cs = ss == -1 ? 0 : ss;
		}
	}
}
int main()
{
	scanf("%d%d%d%d%d%d%d", &ss, &mm, &hh, &wk, &dd, &mt, &t);
	while(t--)
	{
		scanf("%I64d", &val);
		val_init();
		bool flag = wk == -1 || dd == -1;
		if(flag)
		{
			solve();
			val_calc();
		}
		else
		{
			int tmp = wk;
			wk = -1;
			solve();
			wk = tmp;
			val_calc();
			LL val2 = val;
			tmp = dd;
			dd = -1;
			solve();
			dd = tmp;
			val_calc();
			if(val2 < val)
				val = val2;
		}
		printf("%I64d\n", val);
	}
	return 0;
}


#include <cstdio>
#include <algorithm>
typedef long long LL;
const int maxn = 1001;
const int mons[][13] = {{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
const char* weeks[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
int ss, mm, hh, wk, dd, mt, t;
LL val, year, cyear;
int mon, day, vs, vm, vh, vwk;
int cmon, cday, cs, cm, ch, cwk;
inline bool spyear(int year)
{
	return year % 400 == 0 || year % 4 == 0 && year % 100 != 0;
}
inline bool c_valid(int lim)
{
	if(year != cyear || lim == 1)
		return year < cyear;
	if(mon != cmon || lim == 2)
		return mon < cmon;
	if(day != cday || lim == 3)
		return day < cday;
	if(vh != ch || lim == 4)
		return vh < ch;
	if(vm != cm || lim == 5)
		return vm < cm;
	return vs < cs;
}
inline bool isIns(int lim)
{
	int ls = ss == -1 ? 59 : ss;
	if(lim == 1)
		return vs < ls;
	int lm = mm == -1 ? 59 : mm;
	if(lim == 2)
		return vm < lm || vm == lm && vs < ls;
	int lh = hh == -1 ? 23 : hh;
	return vh < lh || vh == lh && (vm < lm || vm == lm && vs < ls);
}
void val_init()
{
	// calc (ss, mm, hh)
	vs = val % 60;
	val /= 60;
	vm = val % 60;
	val /= 60;
	vh = val % 24;
	val /= 24;
	// calc (week, year)
	vwk = (3 + val) % 7 + 1;
	if(val <= 10957)
	{
		year = 1970;
		for(int days; (days = spyear(year) ? 366 : 365) < val; ++year, val -= days);
	}
	else
	{
		val -= 10957;
		year = 2000;
		if(val >= 366)
		{
			++year;
			val -= 366;
			year += val / 146097 * 400;
			val %= 146097;
			if(val / 36524 == 4)
			{
				year += 399;
				val = 365;
			}
			else
			{
				year += val / 36524 * 100;
				val %= 36524;
				year += val / 1461 * 4;
				val %= 1461;
				if(val / 365 == 4)
				{
					year += 3;
					val = 365;
				}
				else
				{
					year += val / 365;
					val %= 365;
				}
			}
		}
	}
	// calc (mm, dd)
	const int *month = mons[spyear(year)];
	for(int i = 1; i <= 12; ++i)
		if(val >= month[i])
			val -= month[i];
		else
		{
			mon = i;
			day = (int)val + 1;
			break;
		}
	//printf("current time:%9lld-%02d-%02d %02d:%02d:%02d (%d)\n", year, mon, day, vh, vm, vs, vwk);
}
void val_calc()
{
	//printf("future time :%9lld-%02d-%02d %02d:%02d:%02d (%d)\n", cyear, cmon, cday, ch, cm, cs, wk == -1 ? -1 : cwk);
	LL year, tmp;
	val = 0;
	for(year = 1970; year + 1 <= cyear && year <= 2000; val += spyear(year++) ? 366 : 365);
	tmp = (cyear - year) / 400;
	val += tmp * 146097;
	year += tmp * 400;
	tmp = (cyear - year) / 100;
	val += tmp * 36524;
	year += tmp * 100;
	tmp = (cyear - year) / 4;
	val += tmp * 1461;
	year += tmp * 4;
	val += (cyear - year) * 365;
	const int *month = mons[spyear(cyear)];
	for(int mon = 1; mon + 1 <= cmon; val += month[mon++]);
	val += cday - 1;
	val = val * 24 + ch;
	val = val * 60 + cm;
	val = val * 60 + cs;
}
void solve()
{
	if(wk == -1)
	{
		cyear = year;
		if(mt == -1)
			cmon = mon;
		else
		{
			cmon = mt;
			if(mt < mon)
				++cyear;
		}
		if(dd == -1)
			cday = c_valid(2) ? 1 : day;
		else
		{
			while(mons[spyear(cyear)][cmon] < dd)
			{
				if(mt == -1)
				{
					if(++cmon > 12)
					{
						cmon = 1;
						++cyear;
					}
				}
				else
					++cyear;
			}
			cday = dd;
		}
		if(!c_valid(3) && !isIns(3))
		{
			if(dd == -1)
			{
				if(++cday > mons[spyear(cyear)][cmon])
				{
					cday = 1;
					if(mt == -1)
					{
						if(++cmon > 12)
						{
							cmon = 1;
							++cyear;
						}
					}
					else
						++cyear;
				}
			}
			else
			{
				do
				{
					if(mt == -1)
					{
						if(++cmon > 12)
						{
							cmon = 1;
							++cyear;
						}
					}
					else
						++cyear;
				} while(mons[spyear(cyear)][cmon] < cday);
			}
		}
		bool flag = c_valid(3);
		ch = hh == -1 ? (flag ? 0 : (isIns(2) ? vh : vh + 1)) : hh;
		flag |= ch > vh;
		cm = mm == -1 ? (flag ? 0 : (isIns(1) ? vm : vm + 1)) : mm;
		flag |= cm > vm;
		cs = ss == -1 ? (flag ? 0 : vs + 1) : mm;
	}
	else
	{
		cyear = year;
		if(mt == -1 || mt == mon)
		{
			cmon = mon;
			if(dd == -1 || day < dd && dd < mons[spyear(cyear)][cmon])
			{
				cday = dd == -1 ? day : dd;
				cwk = (vwk + cday - day - 1) % 7 + 1;
				if(dd == -1)
				{
					for( ; cday < mons[spyear(cyear)][cmon] && cwk != wk; ++cday, cwk = cwk % 7 + 1);
					if(!c_valid(3) && !isIns(3) && cday < mons[spyear(cyear)][cmon])
						for(++cday; cday < mons[spyear(cyear)][cmon] && cwk != wk; ++cday, cwk = cwk % 7 + 1);
				}
				if(cwk == wk && (c_valid(3) || isIns(3)))
				{
					bool flag = c_valid(3);
					ch = hh == -1 ? (flag ? 0 : (isIns(2) ? vh : vh + 1)) : hh;
					flag |= ch > vh;
					cm = mm == -1 ? (flag ? 0 : (isIns(1) ? vm : vm + 1)) : mm;
					flag |= cm > vm;
					cs = ss == -1 ? (flag ? 0 : vs + 1) : mm;
				}
				else
					cday = 0;
			}
			else
				cday = 0;
		}
		else
		{
			cmon = mon;
			cwk = (vwk - day) % 7 + 1;
			if(cwk < 1)
				cwk += 7;
			cday = 1;
			while(cmon != mt)
			{
				cwk = (cwk + mons[spyear(cyear)][cmon] - 1) % 7 + 1;
				if(++cmon > 12)
				{
					cmon = 1;
					++cyear;
				}
			}
			ch = -1;
		}
		if(cday == 0 || ch == -1)
		{
			if(mt == -1 || mt == mon)
			{
				cwk = (vwk - day) % 7 + 1;
				if(cwk < 1)
					cwk += 7;
				cday = 1;
				if(mt == -1)
				{
					cwk = (cwk + mons[spyear(cyear)][cmon] - 1) % 7 + 1;
					if(++cmon > 12)
					{
						cmon = 1;
						++cyear;
					}
				}
				else
				{
					cwk = (cwk + (spyear(cmon <= 2 ? cyear : cyear + 1) ? 366 : 365) - 1) % 7 + 1;
					++cyear;
				}
			}
			while(1)
			{
				if(dd == -1)
					for( ; cwk != wk; ++cday, cwk = cwk % 7 + 1);
				else
				{
					cday = dd;
					cwk = (cwk + cday - 1 - 1) % 7 + 1;
				}
				if(cwk == wk)
					break;
				cwk = (cwk - cday) % 7 + 1;
				if(cwk < 1)
					cwk += 7;
				cday = 1;
				if(mt == -1)
				{
					cwk = (cwk + mons[spyear(cyear)][cmon] - 1) % 7 + 1;
					if(++cmon > 12)
					{
						cmon = 1;
						++cyear;
					}
				}
				else
				{
					cwk = (cwk + (spyear(cmon <= 2 ? cyear : cyear + 1) ? 366 : 365) - 1) % 7 + 1;
					++cyear;
				}
			}
			ch = hh == -1 ? 0 : hh;
			cm = mm == -1 ? 0 : mm;
			cs = ss == -1 ? 0 : ss;
		}
	}
}
int main()
{
	scanf("%d%d%d%d%d%d%d", &ss, &mm, &hh, &wk, &dd, &mt, &t);
	while(t--)
	{
		scanf("%I64d", &val);
		val_init();
		bool flag = wk == -1 || dd == -1;
		if(flag)
		{
			solve();
			val_calc();
		}
		else
		{
			int tmp = wk;
			wk = -1;
			solve();
			wk = tmp;
			val_calc();
			LL val2 = val;
			tmp = dd;
			dd = -1;
			solve();
			dd = tmp;
			val_calc();
			if(val2 < val)
				val = val2;
		}
		printf("%I64d\n", val);
	}
	return 0;
}


#include <bits/stdc++.h>

#define inf 0x3f3f3f3f

#define MOD 1000000007

#define pb push_back

#define x first

#define y second

using namespace std;

typedef long long INT;

typedef pair<int, int> pii;

typedef double DO;

int leap_year(int y) {

	if (y%400==0) return 1;

	if (y%100==0) return 0;

	if (y%4==0) return 1;

	return 0;

}

#define NN 12000100

int x[NN], ti[99999];

int a[]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int main() {

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

	scanf("%d%d%d%d%d%d", &s, &m, &h, &date, &day, &month);

	int n=0, j=-1, dd=3;

	for (int i=1970; i<32709; i++) {

		for (int mm=0; mm<12; mm++) {

			int d=a[mm];

			if (mm==1 && leap_year(i)) d++;

			for (int k=1; k<=d; k++) {

				j++; dd++; if (dd>7) dd=1;

				if (month==-1) {

					if (date==-1) {

						if (day==-1) {

							x[n++]=j;

						} else {

							if (day==k) x[n++]=j;

						}

					} else {

						if (day==-1) {

							if (date==dd) x[n++]=j;

						} else {

							if (date==dd || day==k) x[n++]=j;

						}

					}

				} else if (month==mm+1) {

					if (date==-1) {

						if (day==-1) {

							x[n++]=j;

						} else {

							if (day==k) x[n++]=j;

						}

					} else {

						if (day==-1) {

							if (date==dd) x[n++]=j;

						} else {

							if (date==dd || day==k) x[n++]=j;

						}

					}

				}

			}

		}

	}

	int mm=0, r=0;

	for (int i=0; i<24; i++) for (int j=0; j<60; j++) for (int k=0; k<60; k++) {

		if ((s==-1 || s==k) && (m==-1 || m==j) && (h==-1 || h==i)) ti[mm++]=r;

		r++;

	}

	int N;

	INT t, e;

	scanf("%d", &N);

	for (int i=0; i<N; i++) {

		scanf("%I64d", &t);

		t++;

		e=t/86400;

		int u=lower_bound(x, x+n, e)-x;

		if (x[u]>e) {

			printf("%I64d\n", x[u]*86400LL+ti[0]);

			continue;

		}

		int v=lower_bound(ti, ti+mm, t%86400)-ti;

		if (v==mm) {

			printf("%I64d\n", x[u+1]*86400LL+ti[0]);

		} else {

			printf("%I64d\n", x[u]*86400LL+ti[v]);

		}

	}

	return 0;

}


#include <bits/stdc++.h>

#define inf 0x3f3f3f3f

#define MOD 1000000007

#define pb push_back

#define x first

#define y second

using namespace std;

typedef long long INT;

typedef pair<int, int> pii;

typedef double DO;

int leap_year(int y) {

	if (y%400==0) return 1;

	if (y%100==0) return 0;

	if (y%4==0) return 1;

	return 0;

}

#define NN 32000100

int x[NN], ti[99999];

int a[]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int main() {

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

	scanf("%d%d%d%d%d%d", &s, &m, &h, &date, &day, &month);

	int n=0, j=-1, dd=3;

	for (int i=1970; i<62709; i++) {

		for (int mm=0; mm<12; mm++) {

			int d=a[mm];

			if (mm==1 && leap_year(i)) d++;

			for (int k=1; k<=d; k++) {

				j++; dd++; if (dd>7) dd=1;

				if (month==-1) {

					if (date==-1) {

						if (day==-1) {

							x[n++]=j;

						} else {

							if (day==k) x[n++]=j;

						}

					} else {

						if (day==-1) {

							if (date==dd) x[n++]=j;

						} else {

							if (date==dd || day==k) x[n++]=j;

						}

					}

				} else if (month==mm+1) {

					if (date==-1) {

						if (day==-1) {

							x[n++]=j;

						} else {

							if (day==k) x[n++]=j;

						}

					} else {

						if (day==-1) {

							if (date==dd) x[n++]=j;

						} else {

							if (date==dd || day==k) x[n++]=j;

						}

					}

				}

			}

		}

	}

	int mm=0, r=0;

	for (int i=0; i<24; i++) for (int j=0; j<60; j++) for (int k=0; k<60; k++) {

		if ((s==-1 || s==k) && (m==-1 || m==j) && (h==-1 || h==i)) ti[mm++]=r;

		r++;

	}

	int N;

	INT t, e;

	scanf("%d", &N);

	for (int i=0; i<N; i++) {

		scanf("%I64d", &t);

		t++;

		e=t/86400;

		int u=lower_bound(x, x+n, e)-x;

		if (x[u]>e) {

			printf("%I64d\n", x[u]*86400LL+ti[0]);

			continue;

		}

		int v=lower_bound(ti, ti+mm, t%86400)-ti;

		if (v==mm) {

			printf("%I64d\n", x[u+1]*86400LL+ti[0]);

		} else {

			printf("%I64d\n", x[u]*86400LL+ti[v]);

		}

	}

	return 0;

}


#include <stdio.h>
#include <algorithm>

int ss, mm, hh, dd, dat, mon;
int n;

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

int days_in_month[2][15] = {
	{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
	{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};

inline bool check_day(int m, int d, int day) {
	// printf("check %d %d %d    mm %d dat %d dd %d\n", m, d, day, mm, dat, dd);
	if (m != mon && mon != -1) return false;
	if (dd == -1) {
		return d == dat || dat == -1;
	} else if (dat == -1) {
		return day == dd || dd == -1;
	} else {
		return d == dat || day == dd;
	}
}

long long get_next(long long t0) {
	++t0;
	int now_year = 1970;
	// thursday 1970.1.1
	int day = 3;
	day = (day + t0 / 86400) % 7;
	long long _t0 = t0;
	while (1) {
		int tmp = is_leap_year(now_year) ? 86400 * 366 : 86400 * 365;
		if (t0 >= tmp) {
			t0 -= tmp;
			++now_year;
		} else {
			break;
		}
	}
	bool leap = is_leap_year(now_year);
	int now_month = 1;
	while (1) {
		int tmp = 86400 * days_in_month[leap][now_month];
		if (t0 >= tmp) {
			t0 -= tmp;
			++now_month;
		} else {
			break;
		}
	}
	int now_day = 1 + t0 / 86400;
	t0 %= 86400;

	// printf("%d/%d/%d  %d\n", now_year, now_month, now_day, day);

	if (check_day(now_month, now_day, day)) {
		int now_h = t0 / 3600;
		int now_m = t0 % 3600 / 60;
		int now_s = t0 % 60;
		L_h:
		while (hh != -1 && now_h != hh && now_h < 24) {
			++now_h, now_m = now_s = 0;
			if (now_h >= 24) break;
		}
		if (now_h < 24) {
			while (mm != -1 && now_m != mm && now_m < 60) {
				++now_m, now_s = 0;
				if (now_m >= 60) break;
			}
			if (now_m >= 60) {
				++now_h, now_m = now_s = 0;
				goto L_h;
			} else {
				while (ss != -1 && now_s != ss && now_s < 60) {
					++now_s;
					if (now_s >= 60) break;
				}
				if (now_s >= 60) {
					now_s = 0, ++now_m;
					goto L_h;
				} else {
					return _t0 - _t0 % 86400 + now_h * 3600 + now_m * 60 + now_s;
				}
			}
		}
	}

	while (1) {
		if (++now_day > days_in_month[leap][now_month]) {
			now_day = 1;
			if (++now_month > 12) {
				++now_year;
				now_month = 1;
				leap = is_leap_year(now_year);
			}
		}
		_t0 += 86400;
		day = (day + 1) % 7;
		if (check_day(now_month, now_day, day)) break;
	}
	return _t0 - _t0 % 86400 + std::max(0, hh) * 3600 + std::max(0, mm) * 60 + std::max(0, ss);
}

int main() {
	scanf("%d%d%d%d%d%d", &ss, &mm, &hh, &dd, &dat, &mon);
	if (dd != -1) --dd;
	scanf("%d", &n);
	while (n--) {
		long long t;
		scanf("%I64d", &t);
		printf("%I64d\n", get_next(t));
	}
}


#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;
            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;
}