#include <iostream>
using namespace std;
//Експлозия по време на въпрос
int main() {
    int M, S;
    cin >> M >> S;

    int K;
    cin >> K;

    int N;
    cin >> N;

    int total = M * 60 + S;
    int currentTime = 0;
    int player = K;

    for (int i = 0; i < N; i++) {
        int T;
        char Z;
        cin >> T >> Z;

        currentTime += T;

        if (currentTime > total) {
            cout << player << endl;
            return 0;
        }

        if (Z == 'T') {
            player++;
            if (player > 8) player = 1;
        }
    }

    return 0;
}

