#include <bits/stdc++.h>

using namespace std;
typedef int ll;
#define INF 1000000007
#define MAXN 200007

ll isPrime(ll n) {
    if (n < 2) {
        return false;
    }
    for (ll i = 2; i * i <= n; i++) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}

ll binpow(ll n, ll p) {
    if (p == 0) {
        return 1;
    }
    if (p % 2 == 0) {
        ll b = binpow(n, p / 2);
        return b * b;
    }
    return binpow(n, p - 1) * n;
}

bool isPal(ll n) {
    string s = to_string(n);
    for (ll i = 0; i < s.length() / 2; i++) {
        if (s[i] != s[s.length() - i - 1]) {
            return false;
        }
    }
    return true;
}


void solve() {
    string s;
    cin >> s;
    ll pre[s.length()];
    pre[0] = 0;
    for (ll i = 1; i < s.length(); i++) {
        if (s[i] >= s[i - 1]) {
            pre[i] = pre[i - 1];
        } else {
            pre[i] = pre[i - 1] + 1;
        }
    }
    ll q;
    cin >> q;
    /*for (ll i = 0; i < s.length(); i++) {
        cout << pre[i] << " ";
    }*/
    while (q--) {
        ll l, r;
        cin >> l >> r;
        cout << pre[r - 1] - pre[l - 1] + 1 << "\n";
    }
}

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

    ll t = 1;
    //cin >> t;
    while (t--) {
        solve();
    }
}
