CODE:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = a; i <= b; ++i)
inline int read(){
register int x = 0, f = 1;
register char c = getchar();
while (c < '0' || c > '9'){
if (c == '-')
f = -1;
c = getchar();
}
while (c >= '0' && c <= '9'){
x = x * 10 + c - '0';
c = getchar();
}
return x * f;
}
inline int readll(){
register long long x = 0;
int f = 1;
register char c = getchar();
while (c < '0' || c > '9'){
if (c == '-')
f = -1;
c = getchar();
}
while (c >= '0' && c <= '9'){
x = x * 10 + c - '0';
c = getchar();
}
return x * f;
}
void print(int x){
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
print(x / 10);
putchar(x % 10 + '0');
}
const long long middle = 2299160LL;
const long long offset = 2298874LL;
const long long days_per_cycle = 400 * 365 + 97;
const int days[] = { 0 , 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 };
vector <long long> ConvertToGDate(long long r) {
r -= offset;
long long cycle = r / days_per_cycle;
r %= days_per_cycle;
for (int year = 1582;; ++year) {
bool leap_year = false;
if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) leap_year = true;
int total_days = 365 + leap_year;
if (r >= total_days) {
r -= total_days;
continue;
}
for (int month = 1; ; ++month) {
total_days = days[month];
if (month == 2 && leap_year)total_days++;
if (r >= total_days) {
r -= total_days;
continue;
}
return { year + cycle * 400 , month , r + 1 };
}
}
}
vector <long long> ConvertToDate(long long r) {
if (r > middle)
return ConvertToGDate(r);
for (int year = -4713;; ++year) {
if (year == 0)++year;
bool leapyear = false;
if (year > 0 && year % 4 == 0)leapyear = true;
if (year < 0 && (year + 1) % 4 == 0)leapyear = true;
int total_days = 365 + leapyear;
if (r >= total_days) {
r -= total_days;
continue;
}
for (int month = 1; ; ++month) {
total_days = days[month];
if (month == 2 && leapyear)total_days++;
if (r >= total_days) {
r -= total_days;
continue;
}
return { year , month , r + 1 };
}
}
}
int main() {
int q = read();
while (q--) {
long long r = readll();
auto ans = ConvertToDate(r);
if (ans[0] > 0) {
print(ans[2]);
putchar(' ');
print(ans[1]);
putchar(' ');
print(ans[0]);
putchar('\n');
}
else {
print(ans[2]);
putchar(' ');
print(ans[1]);
putchar(' ');
print(-ans[0]);
puts(" BC");
}
}
return 0;
}