#include <bits/stdc++.h>
#define fr(i,x,y) for(int i=x;i<y;i++)
#define rfr(i,x,y) for(int i=x;i>=y;i--)
#define int long long

using namespace std;

const int N=1e5;

void solve()
{
  string s;
  cin>>s;
  vector<int> mas(s.size());
  fr(i,1,s.size())
  {
    if(s[i]<s[i-1])mas[i]=1;
    mas[i]+=mas[i-1];
  }
  int q;
  cin>>q;
  while(q--)
  {
    int l,r;
    cin>>l>>r;
    cout<<mas[r-1]-mas[l-1] + 1<<'\n';
  }
}

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