///*Bismillahir Rahmanir Rahim***///
/***Stay_Home_Stay_Safe***/
///**Author Thasin Sheikh**///
#include<bits/stdc++.h>
using namespace std;
#define MAX 1e6+10
#define MOD 1000000007
#define PI acos(-1)
#define R 510
#define makefast__  ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using ll=long long int ;
using dl =double;
using ld=long double;
using ull=unsigned long long;
const long N = 3e5 + 10;
const long MAXN=5e5+10;
ll aarray[N];
ll magic[R][R];
vector<ll>primes;
bool check[1000001];
ll large[10000005];
ll vis[N];
map<ll,ll>mp;
vector<ll>v;
void solve()
{
    ll i,j,k,m,n,l,r,q,ans,sum=0;
    string str;
    bool yes=true;
    cin>>n;
    vector<ll>c;
    c=v;
    while(1)
    {
        if(n<c.back())
            c.pop_back();
        while(n>=c.back())
        {
            n-=c.back();
            sum++;
        }
        c.pop_back();
        if(n<2050)
        {
            break;
        }
    }
    if(n)
    {
        cout<<-1<<"\n";
    }
    else cout<<sum<<"\n";
}
int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    //tle khaite chas to endl use kor
    makefast__
    string str;
    ll i,j,n,m,k,t;
    ll p=10;
    m=2050;
    while(m<=1000000000000000000)
    {
        v.push_back(m);
        m*=p;
    }
    cin>>t;
    while(t--)
    {
        solve();
    }

    return 0;
}


