#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char *str;
int temp;
int N;
int i;
scanf("%d", &N);
while(N--){
str = (char*)malloc(sizeof(char) * 1000000);
scanf("%s", str);
temp = 0;
for(i = 0; str[i] != 0; i++){
temp = str[i] - '0' + temp * 10;
while(temp >= 9){
temp = temp % 9;
}
}
printf("%d\n", temp);
free(str);
}
return 0;
}
#include<string.h>
#include<stdlib.h>
int main()
{
char *str;
int temp;
int N;
int i;
scanf("%d", &N);
while(N--){
str = (char*)malloc(sizeof(char) * 1000000);
scanf("%s", str);
temp = 0;
for(i = 0; str[i] != 0; i++){
temp = str[i] - '0' + temp * 10;
while(temp >= 9){
temp = temp % 9;
}
}
printf("%d\n", temp);
free(str);
}
return 0;
}