// A. Sum of 2050
// problem link - https://codeforces.com/contest/1517/problem/A
// #tags - math, number theory
// date - 24 april 2021

#include <bits/stdc++.h>
#define FOR(i, a) for (int i = 0; i < (a); i++)
#define rep(i, a, n) for (int i = a; i < (n); i++)
#define fastio                    \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0)
#define ll long long
#define coutarr(i, n, arr)      \
    for (int i = 0; i < n; i++) \
        cout << arr[i] << ' ' << endl;
using namespace std;
const ll MOD = 1e9 + 7;
map<char, int> mp;
ll solve()
{
    ll n = 0;
    cin >> n;
    if (n < 2050)
        return -1;
    if (n % 2050 == 0)
    {
        ll sum = 0;
        ll y = n / 2050;
        while (y != 0)
        {
            sum += (y % 10);
            y /= 10;
        }
        return sum;
    }
    return -1;
}
int main()
{
    fastio;
    int T;
    cin >> T;
    while (T-- > 0)
    {
        cout << solve() << endl;
    }
    return 0;
}