#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char a[1000005];
int main(){
int m;
scanf("%d", &m);
getchar();
while (m--){
scanf("%s", &a);
int len = strlen(a);
int sum = 0;
for (int i = 0; i != len; i++)
sum = (sum + a[i] - 48) % 9;
printf("%d\n", sum);
}
return 0;
}