#include <bits/stdc++.h>
using namespace std;
int main() {
  int t, n, sum, c = 0;
  string s;
  cin >> t;
  int i, j;
  char ch;
  while (t) {
    t--;
    cin >> n;
    cin >> s;
    sum = 0;
    c = 1;
    ch = s[0];
    i = n - 1;
    while (i > 0 && ch == s[i]) {
      c++;
      i--;
    }
    if (i == 0) {
      double d = c * 1.0;
      cout << int(ceil(c / (3.0))) << "\n";
      continue;
    }
    j = 1;
    while (j <= i && ch == s[j]) {
      j++;
      c++;
    }
    sum = sum + c / 3;
    c = 1;
    if (j <= i) ch = s[j];
    j++;
    while (j <= i) {
      if (ch == s[j]) {
        c++;
        j++;
      } else {
        ch = s[j];
        sum = sum + c / 3;
        c = 1;
        j++;
      }
    }
    cout << sum + c / 3 << "\n";
    sum = 0;
  }
}
