#include using namespace std; #define fst(t) std::get<0>(t) #define snd(t) std::get<1>(t) #define thd(t) std::get<2>(t) using ll = long long; using P = std::tuple; const int dx[8] = {-1, 1, 0, 0, -1, -1, 1, 1}, dy[8] = {0, 0, -1, 1, -1, 1, -1, 1}; int S[10000100]; int inv[10]; template T expt(T a, T n, T mod = std::numeric_limits::max()){ T res = 1; while(n){ if(n & 1){res = res * a % mod;} a = a * a % mod; n >>= 1; } return res; } int main(){ //* std::cin.tie(nullptr); std::ios::sync_with_stdio(false); /*/ /*/ for(int i=0;i<9;++i){ // generate inv if(i % 3 == 0){inv[i] = 1;} else{ inv[i] = expt(i % 9, 5, 9); } } int T; scanf("%d", &T); for(int _=0;_= 1){ int x = i; while(x % 3 == 0){ --three_n; x /= 3; } m = m * (x > 1 ? inv[x % 9] : 1) % 9; x = n - i + 1; while(x % 3 == 0){ ++three_n; x /= 3; } m = m * x % 9; } int a = S[i], three_n2 = three_n + (a % 3 == 0) + (a % 9 == 0); if(three_n2 < 2){ res = (res + a * m % 9 * expt(3, three_n) % 9) % 9; } } printf("%d\n", res > 0 ? res : 9); } }