#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int tc = 1;
  cin >> tc;
  while (tc--) {
    int n;
    cin >> n;
    string s;
    cin >> s;
    int pre[n + 1], suff[n + 1];
    pre[0] = 0;
    for (int i = 1; i <= n; i++) {
      pre[i] = pre[i - 1] + (s[i - 1] - '0');
    }
    suff[n] = 0;
    for (int i = n - 1; i >= 0; i--) {
      suff[i] = suff[i + 1] + (s[i] - '0');
    }
    for (int i = 0; i <= n; i++) {
      pre[i] = pre[i] - (i + 1);
    }
    for (int i = n, j = 1; i >= 0; i--) {
      suff[i] = suff[i] - j;
      j++;
    }
    map<int, int> cnt1, cnt2;
    long long ans = 0;
    for (int i = 0; i <= n; i++) {
      if (cnt1.count(pre[i]) > 0) {
        ans += cnt1[pre[i]];
      }
      cnt1[pre[i]]++;
    }
    for (int i = n; i >= 0; i--) {
      if (cnt2.count(suff[i]) > 0) {
        ans += cnt2[suff[i]];
      }
      cnt2[suff[i]]++;
    }
    cout << ans / 2 << endl;
  }
}
