#include using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int test; cin >> test; while(test--){ long long M; cin >> M; long long power10 = 1; while(M%10 == 0) power10 *= 10,M /= 10; string s = ""; for(int i=1; i<=9; i++){ int d; cin >> d; while(d--) s += '0'+i; } bool ok = false; do{ long long now = stoll(s); if(now%M == 0){ ok = true; cout << now*power10 << endl; break; } }while(next_permutation(s.begin(),s.end())); if(!ok) cout << -1 << endl; } }