#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool umax(T &x, T y) {
  return (y > x) ? x = y, true : false;
}
template <typename T>
void read(T &x) {
  cin >> x;
}
template <typename T, typename T0>
void read(T &x, T0 &y) {
  cin >> x >> y;
}
template <typename T, typename T0, typename T1>
void read(T &x, T0 &y, T1 &z) {
  cin >> x >> y >> z;
}
template <typename T, typename T0, typename T1, typename T2>
void read(T &x, T0 &y, T1 &z, T2 &w) {
  cin >> x >> y >> z >> w;
}
template <typename T>
void read(vector<T> &oneD, long long int n) {
  for (long long int i = 0; i < n; i++) {
    read(oneD[i]);
  }
}
template <typename T>
void read(T oneD[], long long int n) {
  for (long long int i = 0; i < n; i++) {
    read(oneD[i]);
  }
}
void sol() {
  long long int n, mi = 0, ans = 0, flag = -1;
  read(n);
  string s;
  read(s);
  for (int i = (0); ((1) > 0 ? i < (n - 1) : i > (n - 1)); i += (1)) {
    if (s[i] != s[i + 1]) {
      flag = i;
      break;
    }
  }
  if (flag == -1) {
    cout << (n + 2) / 3 << "\n";
    return;
  }
  string s2;
  for (int i = (0); ((1) > 0 ? i < (n) : i > (n)); i += (1)) {
    s2 += s[(i + 1 + flag) % n];
  }
  long long int r = 0;
  for (int l = (0); ((1) > 0 ? l < (n) : l > (n)); l += (1)) {
    while (r < n && s2[l] == s2[r]) {
      r++;
    }
    ans += (r - l) / 3;
    if (r > l) l = r - 1;
  }
  cout << ans;
  cout << "\n";
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int t = 1;
  read(t);
  for (int i = (0); ((1) > 0 ? i < (t) : i > (t)); i += (1)) {
    sol();
  }
}
