#!/bin/python3 import sys def initialize(s): # This function is called once before all queries. s=input().strip() def answerQuery(l, r): # Return the answer for this query modulo 1000000007. if l == 1: return l % p n= (n + 1) // 2 r = 2 * ((pow(l, n + 1, p) - 1) * modular_inverse(l - 1, p) - 1) if n % 2: r -= pow(l, r, p) return r % p if __name__ == "__main__": s = input().strip() 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) print(result)