#!/bin/python3 import sys from itertools import permutations l=list() def initialize(s): store=[] for x in range(0, len(s)+1): l.extend(list(permutations(s, x))) for each in l: store.append(''.join(each)) return set(store) # This function is called once before all queries. def answerQuery(l, r, store): maximum=0 for each in store: if len(each)==abs(r-1) and each==each[::-1]: maximum+=1 return maximum # Return the answer for this query modulo 1000000007. if __name__ == "__main__": s = input().strip() _store=initialize(s) q = int(input().strip()) for a0 in range(q): l, r = input().strip().split(' ') l, r = [int(l), int(r)] result = answerQuery(l, r, _store) print(result)