#include <bits/stdc++.h>
using namespace std;
void solve() {
  long n, ans = 0;
  long count = 0;
  cin >> n;
  string s;
  cin >> s;
  while (s.size() && s[0] == s.back()) {
    count++;
    s.pop_back();
  }
  if (s.empty()) {
    if (count <= 2) {
      cout << "0" << endl;
      return;
    } else if (count == 3) {
      cout << "1" << endl;
      return;
    } else {
      long temp = (count + 2) / 3;
      cout << temp << endl;
      return;
    }
  }
  s.push_back('&');
  for (long i = 0; i < s.size() - 1; i++) {
    count++;
    if (s[i] != s[i + 1]) {
      ans += count / 3;
      count = 0;
    }
  }
  cout << ans << endl;
}
int main() {
  long t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
