#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  int t = 1;
  cin >> t;
  while (t--) {
    int n;
    scanf("%d", &n);
    string s;
    cin >> s;
    int count = 0;
    while (s[0] == s.back() && (int)(s).size() > 0) {
      s.pop_back();
      count++;
    }
    int ans = 0;
    if ((int)(s).size() == 0) {
      ans += count / 3;
      if (count % 3 > 0) ans++;
      printf("%d\n", ans);
      continue;
    }
    for (int i = 0; i < (int)(s).size(); i++) {
      int j = i - 1;
      if (j == -1) j = (int)(s).size() - 1;
      if (s[i] == s[j] || (s[i] != s[j] && i == 0 && count > 0)) {
        count++;
      } else {
        ans += count / 3;
        count = 1;
      }
    }
    ans += count / 3;
    printf("%d\n", ans);
  }
}
