#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool isLeap(LL x) {
if (x > 1582) {
if (x % 4 == 0 && x % 100 != 0) return 1;
if (x % 400 == 0) return 1;
return 0;
} else if (0 < x && x <= 1582) {
if (x % 4 == 0) return 1;
return 0;
} else {
x = -x;
if (x % 4 == 1) return 1;
return 0;
}
}
LL n;
void print(LL y, int m, int d, bool bc) {
if (bc) y = -y;
cout << d << ' ' << m << ' ' << y;
if (bc) {
cout << " BC" << endl;
} else {
cout << endl;
}
}
const LL magic = 2299161LL;
int getDay(LL y, int m) {
if (m != 2) return days[m];
else {
if (isLeap(y)) return 29;
else return 28;
}
}
void bf(LL y = -4713, int m = 1) {
int cnt = 0;
while (true) {
if (n - cnt < getDay(y, m)) {
print(y, m, n - cnt + 1, (y < 0));
return;
} else {
cnt += days[m];
if (m == 2 && isLeap(y)) {
cnt++;
}
m++;
if (m == 13) {
y++;
if (y == 0) y = 1;
m = 1;
}
}
}
}
void solve() {
if (n < magic) {
bf();
return;
} else if (n == magic) {
print(1582, 10, 15, 0);
} else {
n -= magic;
if (n <= 77) {
if (n <= 16) {
print(1582, 10, n + 15, false);
} else if (n > 16 && n <= 16 + 30) {
n -= 16;
print(1582, 11, n, false);
} else if (n > 16 + 30) {
n -= (16 + 30);
print(1582, 12, n, false);
}
return;
} else if (n <= 6287) {
n -= 78;
bf(1583, 1);
} else {
n -= 6287;
LL y = n / 146097, r = n % 146097;
n = r;
bf(y * 400 + 1600, 1);
}
}
}
int main() {
int q;
cin >> q;
while (q--) {
cin >> n;
solve();
}
return 0;
}