# coding:utf-8 MOD = 1000000007 fact = [1]*2000010 inv = [1]*2000010 invfact = [1]*2000010 for i in range(2, 2000010, 1): inv[i] = inv[MOD%i]*(MOD-MOD//i)%MOD fact[i] = fact[i-1]*i%MOD invfact[i] = invfact[i-1]*inv[i]%MOD def c(n, k): if k<0 or k>n:return 0 return fact[n]*invfact[k]*invfact[n-k]%MOD def p(n, k): if k<0 or k>n:return 0 return fact[n]*fact[n-k]%MOD def h(n, k): if n==0 and k==0:return 1 if n<=0 or k<0:return 0 return c(n+k-1, k) T = int(input()) for i in range(T): s = input() x = s[0] n, k = map(int, s[2:-1].split(',')) if x=="C": print(c(n, k)) if x=="P": print(p(n, k)) if x=="H": print(h(n, k))