#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

int main() {
  string s;
  cin >> s;
  vector<int> a(s.size());
  int wordsAmount = 1;
  a[0] = 1;
  for (int i = 1; i < s.size(); i++) {
    if ((s[i] == s[i - 1]) || (s[i] > s[i - 1]))
      a[i] = wordsAmount;
    else {
      wordsAmount++;
      a[i] = wordsAmount;
    }
  }
  int q;
  scanf("%d", &q);
  int l, r;
  while (q--) {
    scanf("%d%d", &l, &r);
    printf("%d\n", a[r - 1] - a[l - 1] + 1);
  }
}
