#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
#pragma GCC optimize(3 , "Ofast" , "inline")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <unordered_map>
#include <vector>
#include <map>
#include <list>
#include <queue>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <stack>
#include <set>
#include <bitset>
#include <deque>
using namespace std ;
#define ios ios::sync_with_stdio(false) , cin.tie(0)
#define x first
#define y second
#define pb push_back
#define ls rt << 1
#define rs rt << 1 | 1
typedef long long ll ;
const double esp = 1e-6 , pi = acos(-1) ;
typedef pair<int , int> PII ;
const int N = 1e6 + 10 , INF = 0x3f3f3f3f , mod = 1e9 + 7;
int a[2][11] , c[100] , cnt ;
__int128 dp[1100][2] ;
void print(__int128 x) {
vector<int> v ;
while(x) v.push_back(x % 10) , x /= 10 ;
if(v.size() == 0) v.push_back(0) ;
reverse(v.begin() , v.end()) ;
for(auto x : v)
printf("%d" , x) ;
printf("\n") ;
}
__int128 dfs(int u , int flag , int limit , int t) {
if(u == 0) return flag == 0 ;
if(!limit && dp[u][flag] != -1) return dp[u][flag] ;
int end = limit ? c[u] : 9 ;
__int128 ans = 0 ;
for(int i = 0 ;i <= end; i ++ )
if(a[t][i]) ans += dfs(u - 1 , flag && i == 0 , limit && end == i , t) ;
if(!limit) dp[u][flag] = ans ;
return ans ;
}
__int128 solve(string x , int t) {
cnt = 0 ;
for(int i = x.size() - 1; i >= 0 ;i -- ) c[++ cnt] = x[i] - '0' ;
return dfs(cnt , 1 , 1 , t) + a[t][0] ;
}
__int128 in() {
__int128 x = 0 ;
char ch = getchar() ;
while(ch < '0' || ch > '9') ch = getchar() ;
while(ch >= '0' && ch <= '9') x = x * 10 + ch - 48 , ch = getchar() ;
return x ;
}
__int128 qmi(__int128 a , __int128 b) {
__int128 res = 1 ;
while(b) {
if(b & 1) res = res * a ;
a = a * a ;
b >>= 1 ;
}
return res ;
}
__int128 t[N] ;
int work()
{
__int128 q = 0 , cnt1 = 0 ;
for(int i = 0; i < 10 ;i ++ ) cin >> a[0][i] , cnt1 += a[0][i] ;
for(int i = 0 ;i < 10 ;i ++ ) cin >> a[1][i] , q += a[1][i] ;
memset(dp , -1 , sizeof dp) ;
string x ;
cin >> x ;
__int128 p = solve(x , 0) ;
__int128 d = cnt1 ;
if(a[0][0] == 0) {
for(int i = 1; i < cnt ;i ++ )
p += d , d *= cnt1 ;
}
string ans = "" ;
t[0] = 1 ;
__int128 res = 0 ;
int flag = 0 ;
if(a[1][0]) p -- ;
if(p == 0) {
for(int j = 0 ;j < 10 ;j ++ )
if(a[1][j]) {
cout << j << endl ;
return 0 ;
}
}
for(int i = 1; ;i ++ ) {
t[i] = t[i - 1] * q ;
for(int j = 1 ; j < 10 ;j ++ ) {
if(a[1][j]) {
if(res + t[i - 1] >= p) {
p -= res ;
ans += j + '0' ;
flag = i ;
break ;
}
res += t[i - 1] ;
}
}
if(flag) break ;
}
for(int i = flag - 1; i >= 1; i -- ) {
int ok = 0 , tt = -1 ;
__int128 res = 0 ;
for(int j = 0 ;j < 10 ;j ++ ) {
if(a[1][j]) {
tt = j ;
if(res + t[i - 1] >= p) {
p -= res ;
ans += j + '0' ;
ok = 1;
break ;
}
res += t[i - 1] ;
}
}
if(!ok && tt != -1) ans += tt + '0' , p -= (__int128)q * t[i - 1] ;
}
cout << ans << "\n" ;
return 0 ;
}
int main()
{
// freopen("C://Users//spnooyseed//Desktop//in.txt" , "r" , stdin) ;
// freopen("C://Users//spnooyseed//Desktop//out.txt" , "w" , stdout) ;
int n ;
ios ;
cin >> n ;
while(n --)
work() ;
return 0 ;
}
/*
*/