#include<iostream>
#include<cmath>
using namespace std;
int t;
long long k;
long long n[3],c[4]={60,60,24},num;
long long times[7]={0,0,0,0,0,0,0};
bool yearX(int y){
if((y%4==0&&y%100!=0)||(y%400==0&&y%3200!=0)||(y%153600!=0&&y%3200==0)){
return true;
}
else{
return false;
}
}
int monthX(int m,int x){
x=yearX(x);
if(m==1||m==3||m==5||m==7||m==8||m==10||m==12){
return 31;
}
else if(m==2){
if(x==1){
return 29;
}
else{
return 28;
}
}
else{
return 30;
}
}
void timeX(){
for(int i=1;i<=3;i++){
if(times[i]>=c[i]){
times[i+1]+=times[i]/c[i-1];
times[i]%=c[i-1];
}
}
// int flag=yearX(times[6]);
while(times[4]>=monthX(times[5],times[6])){
times[4]-=monthX(times[5],times[6]);
times[5]++;
if(times[5]>12){
times[5]=1;
times[6]+=1;
}
// flag=yearX(times[6]);
}
}
int main(){
cin>>t;
for(int i=1;i<=t;i++){
cin>>num;
for(int j=6;j>=1;j--){
cin>>times[j];
}
k=pow(2,num-1)-1;
times[1]+=k;
timeX();
for(int j=6;j>=1;j--){
cout<<times[j]<<' ';
}
cout<<endl;
}
}