#include <bits/stdc++.h>
using namespace std;
int t = 4550000, T_d[5200100];
vector<int> d_cl;
int z_int(char a) {
  int res = a - 48;
  return res;
}
int main() {
  int q;
  cin >> q;
  for (int w = 0; w < q; w++) {
    long long n, ans = 0;
    cin >> n;
    string s;
    cin >> s;
    for (int i = 0; i < n; i++) {
      if (s[i] == '0') {
        if (T_d[t] == 0) d_cl.push_back(t);
        T_d[t]++;
        t++;
        ans += T_d[t];
      } else {
        if (T_d[t] == 0) d_cl.push_back(t);
        T_d[t]++;
        t += (1 - z_int(s[i]));
        ans += T_d[t];
      }
    }
    cout << ans << endl;
    for (int i = 0; i < d_cl.size(); i++) T_d[d_cl[i]] = 0;
    d_cl.clear();
    t = 4550000;
  }
  return 0;
}
