#include <bits/stdc++.h>
//author: @van4o_0
using namespace std;

typedef long long ll;
typedef uint64_t ull;
typedef long double ld;

typedef pair<int, int> p_ii;
typedef pair<int, ll> p_il;
typedef pair <ll, ll> p_ll;

typedef map<int, int> dict_ii;
typedef map <ll, ll> dict_ll;
typedef map<int, string> dict_is;

typedef vector<int> vi;
typedef vector <vi> vvi;
typedef vector <ll> vl;
typedef vector <vl> vvl;
typedef vector<bool> vb;
typedef vector <vb> vvb;
typedef vector<char> vc;
typedef vector <vc> vvc;

//define all(x) (x).begin(), (x).end()
//define sort(x) sort(all(x))

template<typename T>
istream &operator>>(istream &input, vector <T> &arr) {
    for (auto &e: arr) input >> e;
    return input;
}

template<typename T>
//print vector, default sep=" "
ostream &operator<<(ostream &print, vector <T> &arr_out) {
    string sep = " ";
    for (auto x: arr_out) print << x << sep;
    return print;
}

//define r(i, a, b) for (int i = a; i < (b); ++i)
//define range(i, b) for (int i = 0; i < (b); ++i)

int calk(vi &mas, int l, int r){
    int left = 0, right = mas.size();


    int now = right - left, mid;
    for(int i =0;i<100;i++){
        mid = (left + right)/2;

        if(mas[mid]>r) right = mid;
        else left = mid;

        if((right-left) == now) break;
        now = right - left;
    }

    int rez = left;

    left = 0, right = mas.size();


    now = right - left;
    for(int i =0;i<100;i++){
        mid = (left + right)/2;

        if(mas[mid]>l) right = mid;
        else left = mid;

        if((right-left) == now) break;
        now = right - left;
    }

    return rez - left+1;

}


void solve() {
    string s; cin >> s;
    vi a;
    char last = 'x';
    for(int i = 0;i<s.size();i++){
        if(s[i] < last){
            a.push_back(i+1);
        }
        last = s[i];
    }

    int q;cin >> q;
    for(int i =0;i<q;i++){
        int g1, g2; cin >> g1 >> g2;

        cout << calk(a, g1, g2) << endl;
    }

}


int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int tt = 1;
    // cin>>tt;
    while (tt--) solve();
    return 0;
}