#include<bits/stdc++.h>
using namespace std;
int Q;
int mouths[12+1]={0,31,28,31,30,31,30,31,31,30,31,30,31};
string change(long long);
bool isLeapYear(int,bool);
int main(){
scanf("%d",&Q);
for(int i=1;i<=Q;i++){
long long r;
scanf("%lld",&r);
printf("%s\n",change(r).c_str());
}
return 0;
}
string change(long long rday){
int Day = 1;
int Mouth = 1;
long long Year = -4713;
bool isLY=false;
bool LY_4=true;
string answer="";
for(int i=1;i<=rday;i++){
if(Day==5&&Mouth==10&&Year==1582){
Day=15;
LY_4=false;
continue;
}
if(Mouth==2){
isLY=isLeapYear(Year,LY_4);
mouths[2]=isLY?29:28;
}
if(++Day>mouths[Mouth]){
Day=1;
Mouth++;
}
if(Mouth>13){
Mouth=1;
Year++;
if(Year==0){
Year=1;
}
}
}
if(Year<0){
answer=to_string(Day)+" "+to_string(Mouth)+" "+to_string(abs(Year))+" BC";
}
else{
answer=to_string(Day)+" "+to_string(Mouth)+" "+to_string(abs(Year));
}
return answer;
}
bool isLeapYear(int Year,bool LY_4){
if(LY_4){
if(Year<0){
Year=abs(Year)-1;
}
if(Year%4==0){
return true;
}
else{
return false;
}
}
if(!LY_4){
if(Year%400==0){
return true;
}
else if(Year%100!=0&&Year%4==0){
return true;
}
else{
return false;
}
}
}