#define _USE_MATH_DEFINES
 
#include <bits/stdc++.h>
 
#ifndef ONLINE_JUDGE
    #define my_dbg
#endif
 
#define speed_up ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
#define open_files freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
#define all(x) x.begin(), x.end()
 
typedef long long ll;
 
using namespace std;

void solve() {
    string s;
    cin >> s;
    ll n = s.size();
    s += 'a';
    vector<ll> incs;
    for (ll i = 1; i <= n; ++i) {
        if (s[i] < s[i - 1]) incs.emplace_back(i);
    }
    ll q;
    cin >> q;
    n = incs.size();
    while (q--) {
        ll l, r;
        cin >> l >> r;
        ll lb = -1, rb = n; 
        while (rb - lb > 1) {
            ll mid = (rb + lb) / 2;
            if (incs[mid] >= r) rb = mid;
            else lb = mid;
        }
        ll rans = rb;
        lb = -1, rb = n;
        while (rb - lb > 1) {
            ll mid = (rb + lb) / 2;
            if (incs[mid] < l) lb = mid;
            else rb = mid;
        }
        cout << rans - lb << "\n";
    }
}   
 
signed main() {
 
    speed_up
 
#ifdef my_dbg
    open_files
    clock_t processor_time = clock();
#endif
 
    signed t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
 
#ifdef my_dbg
    cout << "\nTIME : " << (clock() - processor_time) / (double)CLOCKS_PER_SEC;
#endif
 
}