Birthday Gift Sequence

For each query (x, K), form every non-empty subset sum of {1, x, x^2, ...}, sort uniquely, and sum the K-th values over all queries modulo 1e9+7.

Medium6MathCombinatoricsImplementationBit manipulationNo attempts yetTime limit2sMemory limit512 MB

Problem

Donghyeok has NN friends. They wanted to give him a sequence for his birthday, but the sequence has far too many terms, so each friend gave him one number xx together with the rule for building the sequence from it. Today the friends want to ask what the KK-th term of their sequence is. Donghyeok memorized none of it, so compute the answers for him.

The sequence MiM_i built from the number xix_i given by the ii-th friend follows this rule. Let A={xi0,xi1,xi2,}A = \{x_i^0, x_i^1, x_i^2, \dots\} be the set of the powers of xix_i. List every non-empty finite subset of AA as A0,A1,A2,A_0, A_1, A_2, \dots, and let aja_j be the sum of all elements of the subset AjA_j. The sequence MiM_i is the list of those values aja_j sorted in increasing order, and no value appears twice.

For example, with x=3x = 3 the set is A={1,3,9,27,}A = \{1, 3, 9, 27, \dots\} and the subset sums in increasing order give M=1,3,4,9,10,12,13,M = 1, 3, 4, 9, 10, 12, 13, \dots.

Input

The first line contains the number of friends NN (1N100,0001 \le N \le 100{,}000).

Each of the next NN lines contains the number xx (2x1,0002 \le x \le 1{,}000) given by a friend and the position KK (1K1,000,000,0001 \le K \le 1{,}000{,}000{,}000) that friend asks about, separated by a space.

Output

For every friend, take the KiK_i-th term of the sequence MiM_i built from xix_i, add all of those terms together, and print the remainder of that total divided by 1,000,000,0071{,}000{,}000{,}007 on the first line.