#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pb push_back
#define ff first
#define ss second
#define YES printf("YES\n")
#define NO printf("NO\n")
#define sci(x) scanf("%d", &x)
#define LL_INF (1LL << 62)
#define INF (1 << 30)
#define SetBit(x, k) (x |= (1LL << k))
#define ClearBit(x, k) (x &= ~(1LL << k))
#define CheckBit(x, k) (x & (1LL << k))
#define mod 1000000007
#define N 200005


int main(){
	int t;
	cin >> t;
	while(t--){
		string s;
		cin >> s;

		int f = 0;
		string ss = "atcoder";

		for(int i = 0; i < 7; i++){
			if(i>=s.size()) break;
			if(s[i]<ss[i]) break;
			if(s[i]==ss[i]){
				if(i==6 && s.size()>ss.size()) f = 1;
				continue;
			}
			if(s[i]>ss[i]){
				f = 1;
				break;
			}
		}

		if(f){
			cout << 0 << "\n";
			continue;
		}

		for(int i = 0; i < s.size(); i++){
			if(s[i]!='a'){
				if(s[i]>'t') cout << i-1 << "\n";
				else cout << i << "\n";
				f = 1;
				break;
			}
		}

		if(!f) cout << -1 << "\n";
	}

	return 0;
}

#include <bits/stdc++.h>
using namespace std;

struct fast_io {
    fast_io() {
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(10);
    }
} __fast_io;

inline int64_t trisum(int32_t n) {
    if (n < 0)
        return 0;
    int64_t nll = n;
    return (nll * (nll + 1)) / 2;
}

int main() {
    int32_t T, L, R;
    cin >> T;

    while (T--) {
        cin >> L >> R;
        cout << trisum(R - 2 * L + 1) << '\n';
    }
}
