#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  string a;
  cin >> a;
  unordered_map<int, int> mp2;
  mp2[0]++;
  long long ans = 0, sum = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] - '0' == 0) {
      sum--;
    } else {
      sum = sum + (a[i] - '0' - 1);
    }
    if (mp2.find(sum) != mp2.end()) {
      ans = ans + mp2[sum];
    }
    mp2[sum]++;
  }
  cout << ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
}
