#include <bits/stdc++.h>
/*
#include <ext/rope>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_cxx;
using namespace __gnu_pbds;
*/

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,tune=native")

#define fast ios_base::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr);

#define CR const&

#define FORI(i, a, b) for(int i = a, max##i = b; i < max##i; ++i)
#define FOR(i, n) FORI(i, 0, n)
#define RFORI(i, a, b) for(int i = a, min##i = b; i >= min##i; --i)
#define RFOR(i, n) RFORI(i, n, 0)
#define FORA(i, a) for(auto i : a)
#define FORAR(i, a) for(auto& i : a)
#define FORACR(i, a) for(auto CR i : a)
#define ALL(obj) begin(obj), end(obj)
#define RALL(obj) rbegin(obj), rend(obj)
#define Count(q) while(q--)

#define OK cerr << "OK" << endl;

#define pb push_back


using namespace std;

template<class T>
using V = vector<T>;
template<class T>
using VV = V<V<T>>;

using ld = long double;
using ll = long long;
using PLL = pair<ll, ll>;
using PII = pair<int, int>;
using VI = V<int>;
using VLL = V<ll>;
using VVI = VV<int>;
using VVLL = VV<ll>;
using Gr = VVI;
using MLL = map<ll, ll>;

template<class T>
constexpr T sqr(T x)
{
    return x * x;
}

template<class T, class U = T>
void cmin(T& a, U const& b)
{
    if(b < a)
        a = b;
}

template<class T, class U = T>
void cmax(T& a, U const& b)
{
    if(a < b)
        a = b;
}

ll get_time()
{
    using namespace chrono;
    return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
}
mt19937 rng(get_time());

int main()
{
    fast

    string s;
    cin >> s;
    int n = s.size();

    int q;
    cin >> q;

    VI go(n);
    int j = 0;
    FOR(i, n)
    {
        cmax(j, i + 1);
        while(j < n && s[j] >= s[j - 1])
            ++j;
        go[i] = j;
    }

    Count(q)
    {
        int l, r;
        cin >> l >> r;
        --l;
        --r;

        int ans = 0;

        for(int i = l; i <= r; i = go[i], ++ans);

        cout << ans << "\n";
    }
}
