#include <iostream>
#include <queue>
#include <functional>
using namespace std ;
typedef long long LL;
const int MOD = 1e9 + 7 ;
const int maxn = 10010 ;
LL pre[maxn], fac[maxn];
LL t, n, k;
void init()
{
pre[0 ] = 1 , pre[1 ] = 0 , pre[2 ] = 1 ;
fac[1 ] = 1 , fac[2 ] = 2 ;
for (int i = 3 ; i <= 10010 ; i++)
{
pre[i] = (((i - 1 ) % MOD*(pre[i - 1 ] + pre[i - 2 ]) % MOD)) % MOD;
fac[i] = ((fac[i - 1 ] % MOD)*(i%MOD)) % MOD;
}
}
LL qpow(LL a, LL x) {
LL res = 1 ;
while (x) {
if (x & 1 ) {
res = (res * a) % MOD;
}
a = (a * a) % MOD;
x >>= 1 ;
}
return res;
}
LL C(LL n, LL k)
{
if (n < k)
return 0 ;
if (k > n - k)
k = n - k;
LL a = 1 , b = 1 ;
for (int i = 0 ; i < k; i++)
{
a = a*(n - i) % MOD;
b = b*(i + 1 ) % MOD;
}
return a*qpow(b, MOD - 2 ) % MOD;
}
int main()
{
init();
cin >> t;
while (t--)
{
cin >> n >> k;
LL ans = 0 ;
for (int i = 0 ; i < k; i++)
{
ans = ((ans%MOD) + (C(n, i)*pre[n - i]) % MOD) % MOD;
}
cout << (MOD + (fac[n] % MOD) - (ans%MOD)) % MOD<<endl;
}
}