#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
int s[11];
bool f[110][1000];
int sum, num;
int main() {
	int cas;
	scanf("%d", &cas);
	while(cas--) {
		sum = 0;
		num = 0;
		for (int j = 1; j <= 9; ++j) {
			scanf("%d", &s[j]);
			sum += s[j] * j;
			num += s[j];
		}
		memset(f, 0, sizeof(f));
		//for (int j = 0; j <= 100; ++j) f[j][0] = 1;
		f[0][0] = 1;
		int i = 0;
		for (int p = 1; p <= 9; ++p) {
			for (int c = 1; c <= s[p]; ++c) {
				
				
				for (int j = sum; j >= p; --j) {
					for (int i = 1; i <= num; ++i) {
						if (f[i-1][j-p]) {
							f[i][j] = 1;
//						printf("%d %d .. \n", i, j);
						}
					}
				}
			}
		}
		int flag = 0;
		int ret = 100000000;
		for (int j = 0; j <= num; ++j) {
			for (int k = 0; k <= sum; ++k) {
				if (f[j][k] && (   abs(sum-k-k) % 11 == 0   ) ) {
					flag = 1;
					ret = min( max(0, abs(j-num+j)-1), ret );
				}
			}		
		}
		if (flag) printf("%d\n", num + ret); else puts("-1");
	}
	return 0;
}
