MOD = 10**9 + 7 def compare_strings(a, b): if len(a) > len(b): return True elif len(a) < len(b): return False else: return a >= b def mod_num(s, mod): result = 0 for c in s: result = (result * 10 + int(c)) % mod return result T = int(input()) for _ in range(T): c_str, p_str = input().split() # Check if P >= MOD (quick check based on string length) if len(p_str) > 10: print(0) continue p_int = int(p_str) if p_int >= MOD: print(0) continue # Compute 2*p_int - 1 q_int = 2 * p_int - 1 q_str = str(q_int) # Check if C >= q_str if not compare_strings(c_str, q_str): print(0) continue # Calculate c_mod and compute the product c_mod = mod_num(c_str, MOD) product = 1 for k in range(p_int): term = (c_mod - p_int + 1 - k) % MOD product = (product * term) % MOD print(product)