#include using namespace std; const int MOD = 1000000007; static string norm(string s) { size_t i = 0; while (i + 1 < s.size() && s[i] == '0') ++i; return s.substr(i); } static int cmp(const string &a, const string &b) { string x = norm(a), y = norm(b); if (x.size() != y.size()) return x.size() < y.size() ? -1 : 1; if (x < y) return -1; if (x > y) return 1; return 0; } static string subBig(const string &a, const string &b) { string x = norm(a), y = norm(b); int n = x.size(), m = y.size(); string r(n, '0'); int borrow = 0; for (int i = 0; i < n; ++i) { int d = (x[n - 1 - i] - '0') - borrow; if (i < m) d -= (y[m - 1 - i] - '0'); if (d < 0) { d += 10; borrow = 1; } else borrow = 0; r[n - 1 - i] = char('0' + d); } return norm(r); } static string addOne(const string &a) { string s = a; int c = 1; for (int i = (int)s.size() - 1; i >= 0 && c; --i) { c += s[i] - '0'; s[i] = char('0' + c % 10); c /= 10; } if (c) s.insert(s.begin(), '1'); return s; } static int modInt(const string &s, int m) { long long r = 0; for (char c : s) r = (r * 10 + (c - '0')) % m; return (int)r; } static long long toIntIfSmall(const string &s, long long limit) { string t = norm(s); char buf[32]; snprintf(buf, sizeof(buf), "%lld", limit); if (t.size() > strlen(buf)) return -1; if (t.size() == strlen(buf) && t > string(buf)) return -1; return stoll(t); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; if (!(cin >> T)) return 0; while (T--) { string C, P; cin >> C >> P; C = norm(C); P = norm(P); if (P == "0") { cout << 1 << '\n'; continue; } long long pVal = toIntIfSmall(P, MOD); if (pVal < 0) { cout << 0 << '\n'; continue; } int k = (int)pVal; if (cmp(C, P) < 0) { cout << 0 << '\n'; continue; } string n = addOne(subBig(C, P)); // n = C - P + 1 if (cmp(n, P) < 0) { cout << 0 << '\n'; continue; } int n0 = modInt(n, MOD); if (n0 < k) { cout << 0 << '\n'; continue; } long long ans = 1; for (int i = 0; i < k; ++i) ans = ans * (n0 - i) % MOD; cout << ans << '\n'; } }