code:
#include <bits/stdc++.h>
#define R register
#define inl inline
#define fastios ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define Debug(file) freopen(file".in","r",stdin);freopen(file".out","w",stdout);
using namespace std;
class bigint{
public:
vector<int> s;
bigint operator =(string str){
s.clear();
for(int i = 0;i < str.size();i ++){
s.push_back(str[i]-'0');
}
return *this;
}
bigint operator =(int num){
while(num){
s.push_back(num%10);
num/=10;
}
return *this;
}
bigint(int num=0){
*this = num;
}
bigint operator *(bigint p){
bigint c;
for(int i = 0;i < s.size()+p.s.size();i ++)c.s.push_back(0);
for(int i = 0;i <s.size();i ++){
int num = 0;
for(int j = 0;j < p.s.size();j ++){
num = c.s[i+j]+s[i]*p.s[j];
c.s[i+j] = num%10;
num/=10;
}
if(num)c.s[p.s.size()+i]=num;
}
while(c.s.back()==0&&c.s.size()>1) c.s.pop_back();
return c;
}
};
ostream& operator <<(ostream& o,bigint p){
for(int i = p.s.size()-1;i>=0;i--){
cout << p.s[i];
}
return o;
}
istream& operator >>(istream& i,bigint& p){
string a;
if(i>>a)p = a;
return i;
}
int main() {
int t;
cin >> t;
for(int i = 0;i < t;i ++){
int x,y,sum = 0;
cin >> x >> y;
bigint c = x;
for(int j = 1;j < x;j ++){
c = c*bigint(j);
}
for(int i = 0;i < c.s.size();i ++){
if(c.s[i] == y)sum ++;
}
cout << sum << endl;
// cout << c << endl;
}
return 0;
}