#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int l, t, n, i, j, k, s, p, q, c;
  cin >> t;
  while (t--) {
    cin >> n;
    string arr;
    int ap[n];
    cin >> arr;
    s = 0;
    i = n - 1;
    c = 0;
    while ((arr[0] == arr.back()) && arr.size()) {
      c++;
      arr.pop_back();
    }
    if (arr.empty()) {
      if (c <= 2)
        cout << "0\n";
      else {
        k = ceil(n / 3.0);
        cout << k << "\n";
      }
    } else {
      arr.push_back('%');
      for (j = 1; j < arr.size(); j++) {
        c++;
        if (arr[j] != arr[j - 1]) {
          s += c / 3;
          c = 0;
        }
      }
      cout << s << "\n";
    }
  }
  return 0;
}
