设移除 $v$ 的代价是 $f(v) = x$,那么满足 $x | v$,并且 $x, 2x, 3x, \ldots, (k - 1)x$ 在集合中都不存在,并且都已经被移除,其中 $v = kx$。可以根据这一点来求出所有的 $f(v)$,不过要反过来求。对于每个 $x$,考虑所有的 $kx$,如果 $x, 2x, 3x, \ldots, kx$ 都不在集合中,并且 $kx$ 还没有被移除,那么 $kx$ 的代价就是 $x$。

// Date: Sun Sep 17 19:57:48 2023

#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

const int INF = 0x3f3f3f3f, MOD = 1e9 + 7;
const double eps = 1e-8;
const int dir[8][2] = {
    {0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1},
};

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef pair<int, int> PII;

const ull Pr = 131;

#define LN ListNode
#define LNP ListNode *
#define TN TreeNode
#define TNP TreeNode *

#define For(i, a, b) for (int i = int(a); i < int(b); ++i)
#define Rof(i, a, b) for (int i = int(b) - 1; i >= int(a); --i)
#define For1(i, a, b) for (int i = int(a); i <= int(b); ++i)
#define Rof1(i, a, b) for (int i = int(b); i >= int(a); --i)
#define ForE(i, j) for (int i = h[j]; i != -1; i = ne[i])

#define f1 first
#define f2 second
#define pb push_back
#define has(a, x) (a.find(x) != a.end())
#define nonempty(a) (!a.empty())
#define all(a) (a).begin(), (a).end()
#define SZ(a) int((a).size())

#ifdef _DEBUG
#define debug1(x) cout << #x " = " << x << endl;
#define debug2(x, y) cout << #x " = " << x << " " #y " = " << y << endl;
#define debug3(x, y, z)                                                        \
  cout << #x " = " << x << " " #y " = " << y << " " #z " = " << z << endl;
#else
#define debug1
#define debug2
#define debug3
#endif

#ifdef _DEBUG

struct ListNode {
  int val;
  ListNode *next;
  ListNode() : val(0), next(nullptr) {}
  ListNode(int val) : val(val), next(nullptr) {}
  ListNode(int val, ListNode *next) : val(val), next(next) {}
};

struct TreeNode {
  int val;
  TreeNode *left;
  TreeNode *right;
  TreeNode() : val(0), left(nullptr), right(nullptr) {}
  TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  TreeNode(int x, TreeNode *left, TreeNode *right)
      : val(x), left(left), right(right) {}
};

#endif

const int N = 1000010;
bool vis[N];
int a[N], n, t, c[N];

int main(void) {
#ifdef _DEBUG
  freopen("1734c.in", "r", stdin);
#endif
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  cin >> t;
  while (t--) {
    string s;
    cin >> n >> s;

    For1(i, 1, n) {
      a[i] = (s[i - 1] == '1');
      vis[i] = false;
      c[i] = 0;
    }
    ll ans = 0;

    For1(i, 1, n) {
      if (!a[i]) {
        if (!vis[i]) {
          vis[i] = true;
          c[i] = i;
        }

        for (int j = i + i; j <= n; j += i) {
          if (a[j])
            break;
          if (!vis[j]) {
            vis[j] = true;
            c[j] = i;
          }
        }
      }
    }

    For1(i, 1, n) {
      if (!a[i]) {
        ans += c[i];
      }
    }

    cout << ans << '\n';
  }

  return 0;
}