#include #define _overload(_1,_2,_3,name,...) name #define _rep(i,n) _range(i,0,n) #define _range(i,a,b) for(int i=int(a);i=int(b);--i) #define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__) using namespace std; char s[100010]; int main(void){ int t,all=0; scanf("%d",&t); rep(loop,t){ scanf("%s",s); int n=strlen(s); assert(1<=n); rep(i,n) assert(isdigit(s[i])); vector seq[2]; rep(i,n) seq[0].push_back(s[i]-'0'); bool allzero=true; rep(i,n) if(seq[0][i]!=0) allzero=false; if(allzero){ puts("0"); continue; } int cur=0,nxt=1; while(seq[cur].size()>=2){ seq[nxt].clear(); int m=seq[cur].size(); rep(i,m-1) seq[nxt].push_back((seq[cur][i]+seq[cur][i+1])%9); swap(cur,nxt); } int ans=seq[cur][0]; if(ans==0) ans=9; printf("%d\n",ans); } assert(all<=100000); return 0; }