#include <iostream>
using namespace std;

int main()
{
    int m, s, ans = 0;
    cin >> m >> s;
    s = m * 60 + s;
    int current;
    cin >> current;
    int n;
    cin >> n;
    for(int i = 0; i < n; i ++)
    {
        int t;
        char ch;
        cin >> t >> ch;
        if(s > 0)
        {
            s -= t;
            if(s <= 0) ans = current;
            else
            {
                if(ch == 'T') current ++;
                if(current > 8) current = 1;
            }
        }
    }
    cout << ans << endl;
    return 0;
}
