#include using namespace std; // macros #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define MIN(a,b) (a>b?b:a) #define MAX(a,b) (a>b?a:b) #define INT_IN(a) int a; cin >> a #define STR_IN(s) string s; cin >> s #define PRINTLN(s) cout << s << endl #define PRINTDOUBLELN(s) cout << setprecision(12) << (s) << endl; // util functions template inline T sqr(T x) {return x*x;} inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;} template inline string toString(T x) {ostringstream sout;sout<inline T max(vector v){T max=v[0]; FOR(i, 1, v.size())max=MAX(v[i], max); return max;} template inline T min(vector v){T min=v[0]; FOR(i, 1, v.size())min=MIN(v[i], min); return min;} template inline vector concat(vector a, vector b){vector ab; ab.reserve(a.size()+b.size());ab.insert(ab.end(),a.begin(),a.end());ab.insert(ab.end(),b.begin(),b.end());return ab;} // types typedef long long LL; // consts const double EPS = 1e-10; const double PI = acos(-1.0); //debug #define dump(x) #x << " = " << (x) << endl #define debug(x) #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl int combination(int n, int r){ if(n == r) return 1; if((r << 1) > n) r = n - r; if(r == 0) return 1; int res = n; FOR(i, 2, r + 1){ res *= n - i + 1; res /= i; } return res; } int main(){ INT_IN(T); REP(i, T){ STR_IN(S); int l = S.length(); if(l == 1){ PRINTLN(S); }else{ vector iS; REP(j, l) iS.push_back(S[j] - '0'); int n = l - 1; int sum = 0; REP(r, l){ sum += combination(n, r) % 9 * iS[r]; } if(sum % 9 != 0)sum %= 9; else sum = 9; PRINTLN(sum); } } return 0; }