#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  ;
  long long t;
  cin >> t;
  while (t--) {
    long long n, ans = 0, p = 0;
    map<long long, long long> mp;
    mp[0] = 1;
    string s;
    cin >> n;
    cin >> s;
    for (long long i = 0; i < (long long)(s).size(); i++) {
      if (s[i] == '0')
        p--;
      else
        p += (long long)(s[i] - '0') - 1;
      ans += (long long)mp[p];
      mp[p]++;
    }
    cout << ans << '\n';
  }
}
