#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    string s;
    cin >> s;
    bool first = true;
    char last = s[0];
    int count = 0;
    for (int i = n - 1; i > 0; i--) {
      if (last != s[i]) {
        break;
      } else {
        count++;
      }
    }
    int ans = 0;
    int seen = count + 1;
    if (seen == n) {
      cout << (n + 2) / 3 << endl;
      continue;
    }
    for (int i = 1; i < n - count; i++) {
      if (s[i] == last) {
        seen++;
      } else {
        last = s[i];
        int z = (seen) / 3;
        ans = ans + z;
        seen = 1;
      }
    }
    int z = (seen) / 3;
    ans = ans + z;
    cout << ans << endl;
  }
  return 0;
}
