Ferry Loading IV

Interview

Time limit1sMemory limit128 MB

Summary
Simulate a ferry of given length that repeatedly crosses between two banks, loading waiting cars in arrival order.
Level

Easy3 of 10

Topics
Simulation, Queue
Solved
No attempts yet

Problem

Before bridges were common, ferries were used to carry cars across rivers. Unlike their larger cousins, river ferries run along a guide line and are powered by the river's current. Cars drive onto the ferry from one end, the ferry crosses the river, and the cars drive off from the other end.

A ferry ll meters long crosses the river. A car may arrive at either bank to be carried to the opposite side. The ferry moves continuously back and forth between the banks as long as it is carrying a car, or at least one car is waiting at either bank. Whenever the ferry reaches a bank it unloads whatever it is carrying and then loads the cars waiting to cross, taking them in the order they arrived and only as far as they fit on its deck. The deck holds a single lane of cars. The ferry starts on the left bank, where it broke down and took a long time to repair; while it was out of service, lines of cars formed on both banks waiting to cross the river.

Input

The first line contains cc, the number of test cases. Each test case begins with two integers ll and mm. The next mm lines describe the cars in the order they arrive: each line gives the length of a car in centimeters and the bank at which it arrives, either left or right.

Output

For each test case, print a single line containing the number of times the ferry must cross the river to serve all of the waiting cars.

Examples1

  1. Example 1

    Input
    4
    20 4
    380 left
    720 left
    1340 right
    1040 left
    15 4 
    380 left
    720 left
    1340 right
    1040 left
    15 4 
    380 left
    720 left
    1340 left
    1040 left
    15 4 
    380 right
    720 right
    1340 right
    1040 right
    
    Expected output
    3
    3
    5
    6