using System;
using System.Linq;
using System.Collections.Generic;

class Solution {
  public static void Main(string[] args) {
    //data
    string[] data = Console.ReadLine().Split(' ');
    string sentence = data[0];
    //sentence analysis
    char currentLetter = sentence[0];
    char previousLetter = sentence[0];
    List<int> newSentences = new List<int>();
    for (int currentPosition = 0; currentPosition < sentence.Length; currentPosition++)
    {
        currentLetter = sentence[currentPosition];
        if (!(Convert.ToInt32(currentLetter) >= Convert.ToInt32(previousLetter)))
        {
            newSentences.Add(currentPosition);
        }
        previousLetter = currentLetter;
    }

    //questions
    int q = Convert.ToInt32(Console.ReadLine());
    for (int i = 0; i < q; i++)
    {
        data = Console.ReadLine().Split(' ');
        int l = Convert.ToInt32(data[0]) - 1;
        int r = Convert.ToInt32(data[1]) - 1;
        int result = 1;
        //answer
        foreach (int newSentenceStartIndex in newSentences)
        {
            if (newSentenceStartIndex > l && newSentenceStartIndex <= r)
            {
                result++;
            }
        }
        Console.WriteLine(result);
    }
  }
}
