#include <bits/stdc++.h>

#define ll long long
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define watch(x) cout << (#x) << " : " << x << '\n'
#define boost ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long

using namespace std;

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

main () {
    boost;
    solve();
    return 0;
}
