#include <bits/stdc++.h>
using namespace std;
long long int mod1 = 998244353, mod2 = (long long int)1e9 + 7;
long long int power(long long int n, long long int k, long long int mod) {
  long long int ans = 1;
  n = n % mod;
  while (k > 0) {
    if (k & 1) ans = (ans * n) % mod;
    n = (n * n) % mod;
    k >>= 1;
  }
  return (ans + mod) % mod;
}
struct triplet {
  long long int a, b, c;
};
bool operator<(const triplet &t1, const triplet &t2) {
  if (t1.a < t2.a) return true;
  if (t1.a == t2.a && t1.b < t2.b) return true;
  if (t1.a == t2.a && t1.b == t2.b && t1.c < t2.c) return true;
  return false;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t = 1;
  cin >> t;
  while (t--) {
    long long int n;
    cin >> n;
    string s;
    cin >> s;
    long long int x = 0;
    while (n >= 1 && s[0] == s[n - 1]) {
      x++;
      n--;
      s.pop_back();
    }
    if (n == 0) {
      cout << 1 + (x - 1) / 3 << "\n";
      continue;
    }
    long long int ans = 0, j = 0, i = 0;
    while (i < n && j < n) {
      while (j < n && s[i] == s[j]) {
        j++;
      }
      ans += (j - i + x) / 3;
      x = 0;
      i = j;
    }
    cout << ans << "\n";
  }
}
