#include <bits/stdc++.h>
using namespace std;
long long int power(long long int x, long long int n) {
  long long int result = 1;
  while (n > 0) {
    if (n % 2 == 1) result = result * x;
    x = x * x;
    n = n / 2;
  }
  return result;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, ans = 0;
  cin >> a;
  while (a--) {
    cin >> b;
    string A;
    cin >> A;
    c = 0;
    ans = 0;
    while (A.size() && A[0] == A.back()) {
      c++;
      A.pop_back();
    }
    if (A.empty()) {
      if (c <= 2) {
        cout << 0 << '\n';
      } else {
        cout << (c + 2) / 3 << '\n';
      }
    } else {
      for (d = 1; d < A.size(); d++) {
        c++;
        if (A[d] != A[d - 1]) {
          ans = ans + (c / 3);
          c = 0;
        }
      }
      c++;
      ans = ans + (c / 3);
      cout << ans << '\n';
    }
  }
  return 0;
}
