#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> v;
  int l = 0;
  while (s.size() > 0 && s[0] == s.back()) {
    s.pop_back();
    l++;
  }
  if (s.empty()) {
    cout << (l + 2) / 3 << '\n';
    return;
  }
  int ans = 0;
  s.push_back('$');
  for (int i = 1; (1 > 0 ? i <= s.size() - 1 : i >= s.size() - 1); i += 1) {
    l++;
    if (s[i] != s[i - 1]) {
      ans += l / 3;
      l = 0;
    }
  }
  cout << ans << '\n';
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
}
