#!/bin/python import sys from itertools import permutations def permutations(string): if len(string) == 1: return string else: recursive_perms = [] for c in string: for perm in permutations(string.replace(c,'',1)): recursive_perms.append(c+perm) return set(recursive_perms) def initialize(s): # This function is called once before all queries. return s def answerQuery(l, r): # Return the answer for this query modulo 1000000007. word= s[l-1:r] while(len(word)>1): words = [''.join(p) for p in permutations(word)] if any(w ==w[::-1] for w in words): return len(word) -1 else: word=word[1:] #words= permutations(word) if __name__ == "__main__": s = raw_input().strip() initialize(s) q = int(raw_input().strip()) for a0 in xrange(q): l, r = raw_input().strip().split(' ') l, r = [int(l), int(r)] result = answerQuery(l, r) print result