#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <iostream>
#include <climits>
#include <cfloat>

using namespace std;


int main()
{
	int n;
	scanf("%d", &n);
	for (int testCase = 0; testCase < n; ++testCase)
	{
		int score = 0;
		int out = 0;
		bool runner[3];
		fill(runner, runner+3, false);
		while (out < 3)
		{
			char buf[128];
			scanf("%s", buf);
			char c = buf[1];
			if (c == 'I')
			{
				if (runner[2])
					++score;
				runner[2] = runner[1];
				runner[1] = runner[0];
				runner[0] = true;
			}
			else if (c == 'U')
				++out;
			else
			{
				for (int i = 0; i < 3; ++i)
				{
					if (runner[i])
						++score;
					runner[i] = false;
				}
				++score;
			}
		}
		printf("%d\n", score);
	}

	return 0;
}