def convert_time str
    h, m = str.split(":").map(&:to_i)
    h * 60 + m
end

while true
    n = gets.to_i
    break if n == 0

    hsh = Hash.new{|h, k| h[k] = [0, nil]}
    arr, mode = [], false
    n.times{
        date, time, io, id = gets.chomp.split(" ")
        if id == "000"
            if io == "I"
                arr.each{|pg| hsh[pg][1] = convert_time(time)}
            else
                arr.each{|pg| hsh[pg][0] += (convert_time(time) - hsh[pg][1])}
            end
            mode = !mode
        else
            if io == "I"
                hsh[id][1] = convert_time(time) if mode
                arr << id
            else
                hsh[id][0] += (convert_time(time) - hsh[id][1]) if mode
                arr.delete id
            end
        end
    }
    arr = hsh.to_a.sort_by{|k, v| -v[0]}
    puts (arr.empty? ? 0 : arr[0][1][0])  
end
