#include <iostream>
using namespace std;

int main()
{
	int a, b;
	string s;
	int n;

	cin >> n;
	for(int icase=1; icase<=n; icase++) {
		cin >> a >> s >> b;
		cout << "Case " << icase << ":" << endl;
		if (a < 0 || a >= 360)
			cout << "ERROR: first input out of range: " << a << endl;
		if (s != "after" && s != "til")
			cout << "ERROR: bad word: " << s << endl;
		if (b < 1 || b > 12)
			cout << "ERROR: second input out of range: " << a << endl;
	}
}

