#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t, n;
  string s;
  cin >> t;
  while (t--) {
    cin >> n;
    cin >> s;
    int i = 1;
    while (i < n && s[i] == s[i - 1]) i++;
    if (i == n) {
      cout << ceil(n / 3.0) << "\n";
      continue;
    }
    int first = i;
    int j = i + 1;
    int res = 0;
    while (j < n) {
      while (j < n && s[j] == s[j - 1]) j++;
      if (j != n) {
        res += (j - i) / 3;
        i = j;
        j++;
      }
    }
    if (s[n - 1] != s[0]) {
      res += (n - i) / 3 + first / 3;
    } else {
      res += (first + n - i) / 3;
    }
    cout << res << "\n";
  }
  return 0;
}
