#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long LL; typedef pair PII; typedef vector VI; typedef vector VPII; typedef pair PLL; typedef pair PIL; typedef pair PLI; typedef double DB; #define pb push_back #define mset(a, b) memset(a, b, sizeof a) #define all(x) (x).begin(), (x).end() #define bit(x) (1 << (x)) #define bitl(x) (1LL << (x)) #define sqr(x) ((x) * (x)) #define sz(x) ((int)(x.size())) #define counti(x) (__builtin_popcount(x)) #define clz(x) (__builtin_clz(x)) #define ctz(x) (__builtin_ctz(x)) #define countl(x) (__builtin_popcountll(x)) #define rep(i, n) for (int (i) = 0; (i) < (int)(n); ++(i)) #define Error(x) cout << #x << " = " << endl #define X first #define Y second 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; } int n, k; const int MOD = 1e9 + 7; int main() { int ncase; for (scanf("%d", &ncase); ncase--;) { scanf("%d%d", &n, &k); if ((LL)k * (k + 1) / 2 > n) { puts("-1"); continue; } if ((2 * n) % k == 0) { int y = 2 * n / k - k + 1; if (y % 2 == 0) { y /= 2; int ans = 1; for (int i = y; i < y + k; i++) { ans = (LL)ans * i % MOD; } printf("%d\n", ans); continue; } } n -= k * (k + 1) / 2; int a = (k - n % k) % k; int x = (n + a) / k; int ans = 1; for (int i = x; i <= x + k; i++) { if (i == x + a) continue; ans = (LL) ans * i % MOD; } printf("%d\n", ans); } return 0; }