#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define fi first #define se second #define pb push_back #define all(x) (x).begin(), (x).end() #define sz(x) (int((x).size())) #define bit(x) (1 << (x)) #define cnt1(x) (__builtin_popcount(x)) template inline void chkmax(T& x, U y) { if (x < y) x = y; } template inline void chkmin(T& x, U y) { if (y < x) x = y; } typedef long long LL; typedef double DB; typedef pair PII; typedef vector VI; const int M = 1000000007; int check(LL n, LL k, LL r) { LL t = (n - r) * 2 / k - k + 1; if (t < 0 || t % 2 == 1) return -1; t /= 2; LL rlt = 1; for (int i = 0; i < k - r; i++) rlt = rlt * (t + i) % M; for (int i = k - r; i < k; i++) rlt = rlt * (t + i + 1) % M; return rlt; } int calc(LL n, LL k) { if (n < k * (k + 1) / 2) return -1; int rlt = -1; if (k & 1) chkmax(rlt, check(n, k, n % k)); else { chkmax(rlt, check(n, k, n % (k / 2))); chkmax(rlt, check(n, k, n % (k / 2) + (k / 2))); } return rlt; } int main() { // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); int tc, n, k; for (scanf("%d", &tc); tc--; ) { scanf("%d%d", &n, &k); printf("%d\n", calc(n, k)); } return 0; }