#include
#include
using namespace std;
int date[100000];
int month[100000];
int year[100000];
int prime1(int a) {//儒略历
int prime = 0;
if (a > 0&&a <= 1582) {
if (abs(a) % 4 == 0)prime = 1;
}
else if (a < 0) {
if ((abs(a + 1) % 4) == 0)prime = 1;
}
return prime;
}
int prime2(int a) {
int p = 0;
if ((a % 4 == 0 && a % 100 != 0) || a % 400 == 0)p = 1;
return p;
}
void MonthDate(int a,int j, int year) {
int mon = 1, i;
int d[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
int d1[12] = { 31,29,31,30,31,30,31,31,30,31,30,31 };
if ((year == 1582 && a > 274) || year > 1582)a += 10;
if (year < 1582 && prime1(year)==1) {
for (i = 0;; i++) {
a = a - d1[i];
if (a <= 0)break;
mon++;
}
date[j]= d1[i] + a + 1;
month[j]= mon;
}
else if (year < 1582 && prime1(year) == 0) {
for (i = 0;; i++) {
a -= d[i];
if (a <= 0)break;
mon++;
}
date[j]= d[i] + a + 1;
month[j]= mon;
}
else if (year >= 1582 && prime2(year)==1) {
for (i = 0;; i++) {
a -= d1[i];
if (a <= 0)break;
mon++;
}
date[j]= d1[i] + a + 1;
month[j]= mon;
}
else if (year >= 1582 && prime2(year) == 0) {
for (i = 0;; i++) {
a -= d[i];
if (a <= 0)break;
mon++;
}
date[j]= d[i] + a + 1;
month[j]= mon;
}
}
int main() {
int a;
int sum;
int i;
cin >> a;
int b[100000];
for (int j = -4713; j <= 1582; j++) {
if (prime1(j))sum += 366;
else sum += 365;
}
sum += 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 4;
for (i = 0; i < a; i++) {
cin >> b[i];
if (b[i] == sum) { date[i] = 4; month[i] = 10; year[i] = 1582; }
else if (b[i] < sum) {
for (int j = -4713;; j++) {
if (j < 0 || (j > 0 && j < 1582)) { if (prime1(j))b[i] -= 366; else b[i] -= 365; }
else if (j >= 1582) { if (prime2(j))b[i] -= 366; else b[i] -= 365; }
if (b[i] <= 0) { year[i] = j; break; }
}
if (year[i] < 1582 && prime1(year[i])) { b[i] += 366; }
else if (year[i] < 1582 && prime1(year[i]) == 0) {
b[i] += 365; }
else if (year[i] >= 1582 && prime2(year[i])) { b[i] += 366; }
else if (year[i] >= 1582 && prime2(year[i]) == 0) {
b[i] += 365;
}
if (b[i] != sum)MonthDate(b[i],i,year[i]);
}
}
for (int i = 0; i < a; i++) {
if (i != a - 1) {
if (year[i] < 0)cout << date[i] << " " << month[i] << " " << abs(year[i]) << " BC" << endl;
else cout << date[i] << " " << month[i] << " " << year[i] << endl;
}
else {
if (year[i] < 0)cout << date[i] << " " << month[i] << " " << abs(year[i]) << " BC";
else cout << date[i] << " " << month[i] << " " << year[i];
}
}
return 0;
}