#include<bits/stdc++.h>
using namespace std;
const int N = 100010, M = 100010, mod = 1e9 + 7;
int n, m, k, top, t;
typedef long long LL;
int a[N];

int main()
{
	cin >> t;
	while(t--)
	{
		LL n, mod = 20500;
		cin >> n;
		int res = 0, tmp, fl = 0;
		while(n){
			tmp = (n % mod) / (mod / 10);
			res += tmp;
			if(tmp * (mod / 10) != n % mod) fl = 1;
			n -= n % mod;
			mod *= 10;
		}
		if(fl) puts("-1");
		else
			cout << res << endl;
	}

	return 0;
}

