#include <bits/stdc++.h>
using namespace std;
#define fast_io cin.tie(0);ios_base::sync_with_stdio(0);
string to_string(string s) { return '"' + s + '"';}
string to_string(char s) { return string(1, s);}
string to_string(const char* s) { return to_string((string) s);}
string to_string(bool b) { return (b ? "true" : "false");}
template <typename A> string to_string(A);
template <typename A, typename B>string to_string(pair<A, B> p) {return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}
template <typename A> string to_string(A v) {bool f = 1; string r = "{"; for (const auto &x : v) {if (!f)r += ", "; f = 0; r += to_string(x);} return r + "}";}
void debug_out() { cout << endl; }
void show() { cout << endl; }
void pret() { cout << endl; exit(0);}
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {cout << " " << to_string(H); debug_out(T...);}
template <typename Head, typename... Tail> void show(Head H, Tail... T) {cout <<H<<" "; show(T...);}
template <typename Head, typename... Tail> void pret(Head H, Tail... T) {cout <<H<<" "; pret(T...);}
#define pr(...) cout << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
typedef long long ll;
#define int ll
typedef long double ld;
typedef vector<int> vi;
#define disp(x) cout<<x<<" ";
#define rep(i,a,b) for(int i=a;i<(int)b;i++)
#define fo(i,a,b) for(int i=a;i<=(int)b;i++)
#define rf(i,a,b) for(int i=a;i>=(int)b;i--)
#define mp make_pair
#define pb emplace_back
#define F first
#define S second
#define endl '\n'
//cout.setf(ios::fixed);cout.precision(18)
const int MOD = 1e9+7;
const int maxn = 2e5+10;

int k, factorial[17], h[maxn][17], p[17];
int tot = 0, nCr[17][17];
vi dp(16, 0);

void add(int &ans, int add){
	add %= MOD;
	ans += add;
	if(ans >= MOD) ans -= MOD;
	if(ans < 0) ans += MOD;
}

void calc(){
	p[0] = 0;
	fo(i, 1, 16){
		p[i] = i;
	}
	h[0][0] = 1;
	fo(n, 1, maxn - 1){
		fo(x, 0, k - 1){
			if(n < x) continue;
			int &ans = h[n][x];
			for (int i = 0; i <= x; ++i)
			{
				int mul = 1;
				if(i % 2) mul = -1;
				add(ans, mul * nCr[x][i] * p[k - i]);
			}
		}
		fo(i, 1, 16){
			p[i] = (p[i] * i) % MOD;
		}
	}
}

int find(int n, int x){
	if(x < 0) return 0;
	if(n == 0){
		if(x == 0) return 1;
		return 0;
	}
	assert(x <= k);
	int ans = nCr[16 + x - k][x];
	// show(n, x, ans, h[n][x]);
	ans =(ans * h[n][x]) % MOD;
	return ans;
}

void increase(int id){
	dp[id]++;
	if(dp[id] == 1) tot++;
}

void decrease(int id){
	dp[id]--;
	if(dp[id] == 0) tot--;
}

int32_t main(){
    fast_io;
    string st;
    cin >> st >> k;
    factorial[0] = 1;
    fo(i, 1, 16) factorial[i] = (factorial[i - 1] * i);
    for (int i = 0; i < 17; ++i)
    {
    	for (int j = 0; j < i + 1; ++j)
    	{
    		nCr[i][j] = (factorial[i]) / (factorial[j] * factorial[i - j]);
    		nCr[i][j] %= MOD;
    	}
    }
    calc();
    vi v;
    for(const auto &it: st){
    	if(it >= '0' and it <= '9') v.emplace_back(it - '0');
    	else v.emplace_back(10 + it - 'A');
    }
    int ans = 0, rem = v.size();
    for(auto it: v){
    	if(tot > k) break;
    	rem--;
    	for (int i = 0; i < it; ++i)
    	{
    		increase(i);
    		if(tot == 1 and dp[0] > 0){
    		}
    		else add(ans, find(rem, k - tot));
    		decrease(i);
    	}
    	increase(it);
    }
    if(tot == k) add(ans, 1);
    // show(ans);
    rem = v.size() - 1;
    fo(i, 1, v.size() - 1){
    	rem--;
    	add(ans, 15 * find(rem, k - 1));
    }
    show(ans);
    return 0;
}
#include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define pb push_back
#define COUT(x) cout << (x) << "\n"
#define COUTF(x) cout << setprecision(15) << (x) << "\n"
#define ENDL cout << "\n"
#define DF(x) x.erase(x.begin())
#define ALL(x) x.begin(), x.end()
#define SORT(x) sort(ALL(x))
#define RSORT(x) sort(x.rbegin(), x.rend())
#define REVERSE(x) reverse(ALL(x))
#define MAX(x) *max_element(ALL(x))
#define MAXI(x) max_element(ALL(x)) - x.begin()
#define SUM(x) accumulate(ALL(x), 0ll)
#define COUNT(x, y) count(ALL(x), y);
#define ANS cout << ans << "\n"
#define YES cout << "YES\n";
#define NO cout << "NO\n";
#define Yes cout << "Yes\n";
#define No cout << "No\n";
#define init() \
  cin.tie(0);  \
  ios::sync_with_stdio(false)
#define debug(x) cerr << "[debug] " << #x << ": " << x << endl;
#define debugV(v)                       \
  cerr << "[debugV] " << #v << ":";     \
  rep(z, v.size()) cerr << " " << v[z]; \
  cerr << endl;
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using mll = map<ll, ll>;
using qll = queue<ll>;
using P = pair<ll, ll>;
using vp = vector<P>;
using vs = vector<string>;

template <typename T>
inline istream& operator>>(istream& i, vector<T>& v) {
  rep(j, v.size()) i >> v[j];
  return i;
}

template <typename T1, typename T2>
inline istream& operator>>(istream& i, pair<T1, T2>& v) {
  return i >> v.first >> v.second;
}

constexpr ll INF = 0x3f3f3f3f3f3f3f3f;
constexpr ld PI = 3.141592653589793238462643383279;
ll get_digit(ll x) {
  return to_string(x).size();
}

ll gcd(ll x, ll y) {
  return y ? gcd(y, x % y) : x;
}

ll lcm(ll a, ll b) {
  return a / gcd(a, b) * b;
}

template <class T>
void chmax(T& a, const T& b) {
  if (a < b) a = b;
}

template <class T>
void chmin(T& a, const T& b) {
  if (b < a) a = b;
}

ll mod_pow(ll x, ll n, ll mod) {
  if (n == 0) return 1;
  ll res = mod_pow(x * x % mod, n / 2, mod);
  if (n & 1) res *= x;
  res %= mod;
  return res;
}

inline ll mod(ll a, ll m) {
  return (a % m + m) % m;
}

ll ext_gcd(ll a, ll b, ll& p, ll& q) {
  if (b == 0) {
    p = 1;
    q = 0;
    return a;
  }
  ll d = ext_gcd(b, a % b, q, p);
  q -= a / b * p;
  return d;
}

P crt(ll b1, ll m1, ll b2, ll m2) {
  ll p, q;
  ll d = ext_gcd(m1, m2, p, q);
  if ((b2 - b1) % d != 0) return {0, -1};
  ll m = lcm(m1, m2);
  ll tmp = (b2 - b1) / d * p % (m2 / d);
  ll r = mod(b1 + m1 * tmp, m);
  return make_pair(r, m);
}

signed main() {
  init();
  ll T;
  cin >> T;
  while (T--) {
    ll N;
    cin >> N;
    vs S(3);
    cin >> S;
    cout << 1;
    rep(i, N) cout << 0;
    rep(i, N) cout << 1;
    ENDL;
  }

  return 0;
}