#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t = 1;
  cin >> t;
  while (t--) {
    long long int x;
    cin >> x;
    string s;
    cin >> s;
    string temp;
    long long int y = s.size();
    while (s.size() < x) {
      for (long long int i = 0;; i++) {
        if ((s[i] - '0') > 1) {
          temp = "";
          for (long long int j = i + 1; j < s.size(); j++) temp.push_back(s[j]);
          if ((s[i] - '0') == 2)
            s += temp;
          else
            s += temp + temp;
        }
        if (s.size() >= x) break;
      }
    }
    long long int dp[x];
    dp[0] = (y - 1) * (s[0] - '0') + 1;
    for (long long int i = 1; i < x; i++) {
      dp[i] = (dp[i - 1] - i - 1) * (s[i] - '0') + (i + 1);
    }
    cout << dp[x - 1] << "\n";
  }
}
