#include<bits/stdc++.h>
using namespace std;#define LL long long#define MXM 6#define mod(x) ((x)%123456789)int L, M, f[6]={2,1,27,9,3,1};int A[MXM][MXM]={{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}};struct mat{
LL d[MXM][MXM];
mat operator*(const mat x){
mat ret;
LL tmp;for(int i =0; i < MXM; i++){for(int j =0; j < MXM; j++){
tmp =0;for(int k =0; k < MXM; k++){
tmp =mod(tmp + d[i][k]* x.d[k][j]);}
ret.d[i][j]= tmp;}}return ret;}voidinit_unit(){// 初始化为单位矩阵for(int i =0; i < MXM; i++)for(int j =0; j < MXM; j++)
d[i][j]= i == j ?1:0;}voidinit(){// 初始化为变换矩阵for(int i =0; i < MXM; i++)for(int j =0; j < MXM; j++)
this->d[i][j]= A[i][j];}}ma;
mat matrixPow(mat base, LL pow){
mat res;
res.init_unit();while(pow){if(pow &1) res = res * base;
base = base * base;
pow >>=1;}return res;}intmain(){
LL T, n;;scanf("%lld",&T);while(T--){scanf("%lld",&n);if(n <=2){printf("%d\n",mod(f[n]));continue;}
ma.init();
ma =matrixPow(ma, n-2);
LL ans =0;for(int i =0; i < MXM; i++)
ans =mod(ans+ma.d[0][i]*f[i]);printf("%lld\n", ans);}return0;}
#include<bits/stdc++.h>
using namespace std;#define LL long long#define MXM 6#define mod(x) ((x)%123456789)int L, M, f[6]={2,1,27,9,3,1};int A[MXM][MXM]={{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}};struct mat{
LL d[MXM][MXM];
mat operator*(const mat x){
mat ret;
LL tmp;for(int i =0; i < MXM; i++){for(int j =0; j < MXM; j++){
tmp =0;for(int k =0; k < MXM; k++){
tmp =mod(tmp + d[i][k]* x.d[k][j]);}
ret.d[i][j]= tmp;}}return ret;}
LL operator*(constint* p){// 乘列向量
LL res =0;for(int i =0; i < MXM; i++) res =mod(res+d[0][i]*p[i]);return res;}voidinit_unit(){// 初始化为单位矩阵for(int i =0; i < MXM; i++)for(int j =0; j < MXM; j++) d[i][j]= i == j ?1:0;}voidinit(){// 初始化为变换矩阵for(int i =0; i < MXM; i++)for(int j =0; j < MXM; j++) this->d[i][j]= A[i][j];}}ma;
mat matrixPow(mat base, LL pow){
mat res;
res.init_unit();while(pow){if(pow &1) res = res * base;
base = base * base;
pow >>=1;}return res;}intmain(){
LL T, n;;scanf("%lld",&T);while(T--){scanf("%lld",&n);if(n <=2){printf("%d\n",mod(f[n]));continue;}
ma.init();
ma =matrixPow(ma, n-2);printf("%lld\n", ma*f);}return0;}