#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int tc, n;
  string s;
  cin >> tc;
  for (int i = 0; i < tc; i++) {
    cin >> n;
    cin >> s;
    int ind = n - 1;
    if (s[n - 1] == s[0]) {
      while (s[ind] == s[0]) {
        ind--;
      }
    }
    int count = 0;
    ind++;
    int start = ind % n;
    if (ind == 0) {
      if (n == 3)
        count++;
      else {
        count += (2 + (n - 4) / 3);
      }
    }
    for (int j = 0; j < ind; j++) {
      if (j == (ind - 1)) {
        if (start > j) {
          count += (n - start + j + 1) / 3;
        } else {
          count += (j + 1 - start) / 3;
        }
      } else if (s[j] != s[j + 1]) {
        if (start > j) {
          count += (n - start + j + 1) / 3;
        } else {
          count += (j + 1 - start) / 3;
        }
        start = j + 1;
      }
    }
    cout << count << "\n";
  }
}
