#include <bits/stdc++.h>
using namespace std;
signed main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    string str;
    cin >> n >> str;
    vector<long long> a(n), pref(n + 1, 0);
    map<long long, long long> m;
    for (long long i = 0; i < n; i++) {
      a[i] = str[i] - '0';
      pref[i + 1] = pref[i] + a[i];
      m[pref[i + 1] - (i + 1)]++;
    }
    long long ans = 0;
    for (long long i = 0; i < n; i++) {
      long long curr = pref[i + 1] - (i + 1);
      if (curr == 0) {
        ans += m[curr];
      } else {
        if (m[curr] != 1) ans += m[curr] - 1;
      }
      if (m[curr] == 1) {
        m.erase(curr);
      } else
        m[curr]--;
    }
    cout << ans << "\n";
  }
  return 0;
}
