#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define INIT std::ios::sync_with_stdio(false);std::cin.tie(0); #define VAR(type, ...)type __VA_ARGS__;Scan(__VA_ARGS__); template void Scan(T& t) { std::cin >> t; } templatevoid Scan(First& first, Rest&...rest) { std::cin >> first; Scan(rest...); } #define OUT(d) std::cout< c(n);for(auto& i:c)std::cin>>i; #define MAT(type, c, m, n) std::vector> c(m, std::vector(n));for(auto& r:c)for(auto& i:r)std::cin>>i; #define ALL(a) (a).begin(),(a).end() #define FOR(i, a, b) for(int i=(a);i<(b);++i) #define RFOR(i, a, b) for(int i=(b)-1;i>=(a);--i) #define REP(i, n) for(int i=0;i=0;--i) #define FORLL(i, a, b) for(ll i=ll(a);i=ll(a);--i) #define REPLL(i, n) for(ll i=0;i=0;--i) #define PAIR std::pair #define PAIRLL std::pair #define IN(a, x, b) (a<=x && x(end-start).count();std::cerr<<"[Time:"< tmp(a);std::cerr << #a << "\t:";for(int i=0; i(a.size()); ++i){std::cerr << tmp.front() << "\n";tmp.pop();}std::cerr << "\n";} #define CHMAX(a, b) a = (((a)<(b)) ? (b) : (a)) #define CHMIN(a, b) a = (((a)>(b)) ? (b) : (a)) #define EXCEPTION(msg) throw std::string("Exception : " msg " [ in ") + __func__ + " : " + std::to_string(__LINE__) + " lines ]" #define TRY(cond, msg) try {if (cond) EXCEPTION(msg);}catch (std::string s) {std::cerr << s << std::endl;} #define int ll using ll = long long; using ull = unsigned long long; constexpr int INFINT = 1 << 30; // 1.07x10^ 9 constexpr int INFINT_LIM = (1LL << 31) - 1; // 2.15x10^ 9 constexpr ll INFLL = 1LL << 60; // 1.15x10^18 constexpr double EPS = 1e-9; constexpr int MOD = 1000000007; constexpr double PI = 3.141592653589793238; long C(long n, long r, int p, int q, std::vector& fact, std::vector& ifact) { if (n < 0 || r < 0 || r > n)return 0; int P = 1; for (int i = 0; i < q; i++)P *= p; long z = n - r; int e0 = 0; for (long u = n / p; u > 0; u /= p)e0 += u; for (long u = r / p; u > 0; u /= p)e0 -= u; for (long u = z / p; u > 0; u /= p)e0 -= u; int em = 0; for (long u = n / P; u > 0; u /= p)em += u; for (long u = r / P; u > 0; u /= p)em -= u; for (long u = z / P; u > 0; u /= p)em -= u; long ret = 1; while (n > 0) { ret = ret * fact[(int)(n%P)] % P * ifact[(int)(r%P)] % P * ifact[(int)(z%P)] % P; n /= p; r /= p; z /= p; } for (int i = 0; i < e0; i++)ret = ret * p % P; if (!(p == 2 && q >= 3) && (em & 1) == 1)ret = (P - ret) % P; return ret; } void makeFF(int p, int q, std::vector& fact, std::vector& ifact) { int P = 1; for (int i = 0; i < q; i++)P *= p; fact.resize(P + 1, 0); ifact.resize(P + 1, 0); fact[0] = 1; for (int i = 1; i <= P; i++) { if (i % p == 0) { fact[i] = fact[i - 1]; } else { fact[i] = fact[i - 1] * i % P; } } for (int i = 0; i <= P; i++) { long ret = 1; long mul = fact[i]; for (long n = P / p*(p - 1) - 1; n > 0; n >>= 1) { if ((n & 1) == 1) { ret = (ret * mul) % P; } mul = (mul * mul) % P; } ifact[i] = (int)ret; } } ll fix(ll& n) { return n = (n + 8) % 9 + 1; } signed main() { INIT; VAR(int, Q); std::vector fa, ifa; makeFF(3, 2, fa, ifa); REP(q, Q) { VAR(std::string, s); if (s.size() == 1 && s[0] == '0') { OUT(0)BR; continue; } int n = s.size(); ll ans = 0; REP(i, n) { ans += C(n - 1, i, 3, 2, fa, ifa) * (s[i] - '0'); fix(ans); } OUT(ans)BR; } return 0; }