#include <bits/stdc++.h>
using namespace std;
#define MOD 123456789
#define N 6
typedef long long LL;
typedef pair<int, int> Author;
vector<pair<string, int> > VP;
struct Matrix{
LL a[N][N];
Matrix(){memset(a, 0, sizeof(a));}
Matrix operator * (const Matrix& y){
Matrix ans;
//矩阵相乘
for(int i = 0; i < N; i++)for(int j = 0; j < N; j++)for(int k = 0; k < N; k++)ans.a[i][j] = (ans.a[i][j] + a[i][k] * y.a[k][j]) % MOD;
return ans;
}
void operator = (const Matrix b){for(int i = 0; i < N; i++)for(int j = 0; j < N; j++)a[i][j] = b.a[i][j];}
};
LL Solve(Matrix ans, LL x){
Matrix trs;
//只是为了初始化,结构体内数组无法直接初始化
for(int i = 0; i < N; i++) trs.a[i][i] = 1; //构造矩阵
while(x){
if(x & 1) trs = trs * ans;
x >>= 1;
ans = ans * ans;
}
return (trs.a[0][0]*2 + trs.a[0][1] + trs.a[0][2] * 27 + trs.a[0][3] * 9 + trs.a[0][4] * 3 + trs.a[0][5]) % MOD;
}
int main(void){
ios::sync_with_stdio(false); cin.tie(0);
LL T, LL, n, ans; Matrix base;
long long mm[6][6] = {
{1, 2, 1, 0, 0, 0},
{1, 0, 0, 0, 0, 0},
{0, 0, 1, 3, 3, 1},
{0, 0, 0, 1, 2, 1},
{0, 0, 0, 0, 1, 1},
{0, 0, 0, 0, 0, 1}
};
cin>>T;
for(int i = 0;i < N; i++)for(int j = 0;j < N; j++) base.a[i][j] = mm[i][j];
while(T--){
cin>>n;
cout<<Solve(base, n - 2)<<endl; //求第F[n]
}
return EXIT_SUCCESS;
}