#include <bits/stdc++.h>
using namespace std;
pair<int, int> mp[2000000], mp2[2000000];
int x, n, t;
int it;
inline void add(int x) {
  if (mp[x].first < it) {
    mp[x].first = it;
    mp[x].second = 0;
  }
  ++mp[x].second;
}
inline int get(int x) {
  if (mp[x].first < it) return 0;
  return mp[x].second;
}
inline void add2(int x) {
  x = abs(x);
  if (mp2[x].first < it) {
    mp2[x].first = it;
    mp2[x].second = 0;
  }
  ++mp2[x].second;
}
inline int get2(int x) {
  x = abs(x);
  if (mp2[x].first < it) return 0;
  return mp2[x].second;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> t;
  while (t-- > 0) {
    ++it;
    string s;
    cin >> n;
    cin >> s;
    int sum = 0;
    long long ans = 0;
    for (int i = 1; i <= n; ++i) {
      int x = s[i - 1] - '0';
      if (sum - i + 1 >= 0)
        add(sum - i + 1);
      else
        add2(sum - i + 1);
      sum += x;
      if (sum - i >= 0)
        ans += get(sum - i);
      else
        ans += get2(sum - i);
    }
    cout << ans << endl;
  }
  return 0;
}
