

#include <bits/stdc++.h>

#define loop(s, e, i) for (int i = s; i < e; ++i)
#define print(s) cout << s << endl;
#define DIV 1000000007
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
const ll INF = 1e18+7;

ll ceildiv(ll a, ll b) { return (a+b-1)/b; } // 切り上げ
ll floordiv(ll a, ll b) { return a/b; } // 切り下げ

int show_matrix(vector<vector<int>> &dp) {
  loop(0, dp.size(), i) {
    loop(0, dp[i].size(), j) {
      cout << dp[i][j] << " ";
    }
    cout << endl;
  }
  return 0;
}

/*
浮動小数点の入力
cout << fixed << setprecision(9) << endl;
*/

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  ll N;
  cin >> N;

  vector<ll> A(N);
  ll cmax = -1;
  loop(0, N, i) {
    cin >> A[i];
  }

  vector<ll> C(N+1);
  vector<ll> M(N+1);
  loop(0, N, i) {
    C[i+1] = C[i] + A[i];
    M[i+1] = max(M[i], A[i]);
  }

  ll sum = 0;
  loop(0, N, i) {
    ll res = sum + C[i+1] + M[i+1] * (i+1);
    print(res)
    sum = sum + C[i+1];
  }
}

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int,int>;

const string a = "atcoder";

void solve(){
    string s;
    cin >> s;
    if(s > a){
        cout << 0 << endl;
        return;
    }
    string t = s;
    sort(t.rbegin(), t.rend());
    if(t <= a){
        cout << -1 << endl;
        return;
    }
    int ans = 0, i = 0;
    while(s[i] == 'a') i++;
    while(s <= a){
        swap(s[i], s[i-1]);
        i--;
        ans++;
    }
    cout << ans << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    rep(i,t) solve();
}