#include <bits/stdc++.h>
            using namespace std;
            #define bust ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
            #define fr first
            #define sd second
            #define X real()
            #define Y imag()
            #define pb push_back
            typedef long long ll;
            typedef unsigned long long ull;
            typedef long double ld;
            #define FOR(i, a, b) for (int i = a; i < (b); ++i)
            #define F0R(i, b) for (int i = 0; i < (b); ++i)
            #define F1R(i, b) for (int i = 1; i < (b); ++i)
            #define FORd(i, a, b) for (int i = (b) - 1 ; i >= a; --i)
            #define F0Rd(i, b) for (int i = (b) - 1; i >= 0; --i)
            #define FORll(i, a, b) for (ll i = a; i < (b); ++i)
            #define F0Rll(i, b) for (ll i = 0; i < (b); ++i)
            #define F1Rll(i, b) for (ll i = 1; i < (b); ++i)
            #define FORdll(i, a, b) for (ll i = (b) - 1 ; i >= a; --i)
            #define F0Rdll(i, b) for (ll i = (b) - 1; i >= 0; --i)
            #define pr(qw) cout << (qw) << '\n'
            #define el pr('\n')
            #define pr2(s, q) cout << (s) << ' ' << (q) << '\n'
            #define ret return
            typedef pair<int, int> pii;
            typedef pair<long long, long long> pll;
            typedef complex<ll> C;
            main(){
                string s;
                cin >> s;
                int q, n;

                n = (int) s.length();
                int a[n], p[n];
                a[n-1] = 0;
                cin >> q;
                F0R(i, n-1){
                    a[i] = 0;
                    if(s[i]>s[i+1]) a[i] = 1;
                }
                p[0] = a[0];
                FOR(i, 1, n) p[i] = p[i-1] + a[i];
             /*   F0R(i, n) cout << a[i] << ' ';
                el;
                F0R(i, n) cout << p[i] << ' ';*/
                while(q--){
                    int l, r;
                    cin >> l >> r;
                    pr(p[r-2]-p[l-1]+1);

                }











            }
