#include #include #include using namespace std; using namespace atcoder; using mint = modint998244353; #define rep(i,n) for (int i = 0; i < (n); ++i) #define Inf32 1000000001 #define Inf64 4000000000000000001 int main(){ int _t; cin>>_t; rep(_,_t){ long long M; cin>>M; long long ans = 0; rep(i,9){ int d; cin>>d; rep(j,d){ ans *= 10; ans += i+1; } } rep(i,9)ans *= 10; ans += (M-(ans%M))%M; cout<