#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    string a;
    cin >> a;
    int b[n + 2];
    long long int s = 0;
    for (int i = 0; i < n; i++) {
      s += (a[i] - '0');
      b[i] = s - i;
    }
    int an = 0;
    s = 0;
    for (int i = 0; i < n; i++) {
      s += a[i] - '0';
      if (s == i + 1) an++;
    }
    sort(b, b + n);
    vector<long long int> c;
    int ss = 1;
    for (int i = 1; i < n; i++) {
      if (b[i] != b[i - 1]) {
        c.push_back(ss);
        ss = 1;
      } else {
        ss++;
      }
    }
    c.push_back(ss);
    long long int ans = 0;
    for (int i = 0; i < c.size(); i++) {
      if (c[i] > 1) {
        ans += (c[i] * (c[i] - 1)) / 2;
      }
    }
    cout << ans + an << "\n";
  }
  return 0;
}
