#include <bits/stdc++.h>
using namespace std;

int main() {
  int N;
  cin >> N;
  int ans = 111;
  for (int i = 1; i < 10; i++) {
    if (N <= ans * i) {
      cout << ans * i << endl;
      break;
    }
    else if (i == 9) {
      cout << 1111 << endl;
    }
  }
}
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <utility>
#include <algorithm>
#include <numeric>
#include <typeinfo>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <ctime>

using namespace std;

#define dump(n) cout<<"# "<<#n<<"="<<(n)<<endl
#define debug(n) cout<<__FILE__<<","<<__LINE__<<": #"<<#n<<"="<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,n) repi(i,0,n)
#define iter(c) __typeof((c).begin())
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();i++)
#define allof(c) (c).begin(),(c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int,int> pii;

int main()
{
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
		int score=0;
		int runner=0;
		for(int j=0;j<3;){
			string s;
			cin>>s;
			if(s=="HIT"){
				if(runner==3)
					score++;
				else
					runner++;
			}
			else if(s=="HOMERUN"){
				score+=runner+1;
				runner=0;
			}
			else
				j++;
		}
		cout<<score<<endl;
	}
	
	return 0;
}