#include <iostream>
#include <iomanip>
#include <cmath>
#include <vector>
#include <algorithm>
#include <string>
#include <map>

#define pii pair<int, int>
#define S second
#define F first
#define pb push_back

using namespace std;

typedef long long ll;

void solve (){
  string s;
  cin >> s;
  vector <int> v(s.size());
  v[0] = 1;
  for (int i = 0; i < s.size()-1; i++){
    if (s[i] > s[i+1]) {
      v[i+1] = v[i]+1;
    } else {
      v[i+1] = v[i];
    }
    //cout << v[i] << " ";
  }
  //cout << v[v.size()-1] << "\n";

  ll n, l, r;
  cin >> n;
  for (int i = 0; i < n; i++){
    cin >> l >> r;
    cout << v[r-1] - v[l-1] + 1 << "\n";
  }
  return;
}

int main(){
  solve();

  return 0;
}
