#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//#define int long long
#define ll long long
#define ff first
#define ss second
#define num ss
#define value ff.ff
#define cntt ff.ss
#define pb push_back
#define m_p make_pair
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
#pragma GCC target("avx,avx2")
using namespace std;
using namespace __gnu_pbds;
#define ordered set tree < int ,  null_type ,  less ,  rb_tree_tag ,  tree_order_statistics_node_update >
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//constexpr int N=2147483647;
//constexpr int INF=2147483647;
//constexpr int BIGINF=1e18;
//constexpr int N=500001;
//constexpr int mod=998244353;
int A[100001];
vector<int>v;
int binp_l(int x)
{
    int l=0, r=v.size()-1;
    while(r-l>1)
    {
        int mid=(l+r)/2;
        if(v[mid]+1<=x)
            l=mid; else
            r=mid;
    }
    if(v[r]+1<=x)
    {
        return r;
    }
    return l;
}
int binp_r(int x)
{
    int l=0, r=v.size()-1;
    while(r-l>1)
    {
        int mid=(l+r)/2;
        if(v[mid]<=x)
            l=mid; else
            r=mid;
    }
    if(v[l]>=x)
    {
        return l;
    }
    return r;
}
void solve()
{
    string s;
    cin>>s;
    v.pb(0);
    for(int i=0; i<s.size(); i++)
    {
        int j=i+1;
        while(j<s.size() && s[j]>=s[j-1])
        {
            j++;
        }
        v.pb(j-i);
        v[v.size()-1]+=v[v.size()-2];
        i=j-1;
    }
    int q;
    cin>>q;
    for(int qq=0; qq<q; qq++)
    {
        int l, r;
        cin>>l>>r;
        cout<<binp_r(r)-binp_l(l)<<'\n';
    }


}
main()
{
    cin.tie(nullptr);
    cout.tie(nullptr);
    ios_base::sync_with_stdio(false);
    int t=1;
//    cin>>t;
    while(t)
    {
        t--;
        solve();
    }
}
