#include <bits/stdc++.h>
using namespace std;
void run() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  if (equal(s.begin() + 1, s.end(), s.begin())) {
    if (n == 2) {
      cout << "0" << endl;
      return;
    } else if (n == 3) {
      cout << "1" << endl;
      return;
    } else {
      cout << (n + 2) / 3 << endl;
      return;
    }
  }
  int cnt = 0, ans = 0;
  while (s.size() && s[0] == s.back()) {
    cnt++;
    s.pop_back();
  }
  s.push_back('x');
  for (int i = 0; i < s.size() - 1; i++) {
    cnt++;
    if (s[i] != s[i + 1]) {
      ans += (cnt / 3);
      cnt = 0;
    }
  }
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int t;
  cin >> t;
  while (t--) run();
}
