// #pragma GCC optimize ("Ofast")
#include <bits/stdc++.h>
#define int long long
#define ft first
#define sc second
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define GG cout << " OPA " << '\n';

using namespace std;

const int N = 3e5 + 5;
const int INF = 1e9;

int a[N];
int pref[N];

void solve() {
    string s;
    cin >> s;
    for (int i = 1; i < s.size(); i ++) {
        if (s[i] < s[i - 1]) a[i] = 1;
        pref[i] = pref[i - 1] + a[i];
    }
    int q;
    cin >> q;
    while (q --) {
        int l, r;
        cin >> l >> r;
        cout << pref[r - 1] - pref[l - 1] + 1 << '\n';
    }
}

main() {
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cerr.tie(0);
    int test = 1;
    // cin >> test;
    while(test --) {
        solve();
    }
}