using System;
using System.Collections.Generic;
using System.Text;

namespace TestCSharpBot
{
    class Lift
    {
        private int currentFloor;
        private List<Order> innerOrders = new List<Order>();

        public Lift()
        {
            currentFloor = 1;
        }

        public int getCurrentFloor()
        {
            return currentFloor;
        }

        public void changeCurrentFloor(int delta)
        {
            currentFloor += delta;
        }

        public List<Order> getInnerOrders()
        {
            return innerOrders;
        }
    }

    class Order
    {
        private int startTime;
        private int endTime;
        private int sourceFloor;
        private bool upDirection;
        private int targetFloor;
        private int liftIndex;

        public Order(int time, int sourceFloor, bool upDirection)
        {
            startTime = time;
            this.sourceFloor = sourceFloor;
            this.upDirection = upDirection;
            targetFloor = -1;
            endTime = -1;
            liftIndex = -1;
        }

        public int getStartTime()
        {
            return startTime;
        }

        public int getEndTime()
        {
            return endTime;
        }

        public void setEndTime(int endTime)
        {
            this.endTime = endTime;
        }

        public int getSourceFloor()
        {
            return sourceFloor;
        }

        public bool isUpDirection()
        {
            return upDirection;
        }

        public int getTargetFloor()
        {
            return targetFloor;
        }

        public void setTargetFloor(int targetFloor)
        {
            this.targetFloor = targetFloor;
        }

        public int getLiftIndex()
        {
            return liftIndex;
        }

        public void setLiftIndex(int liftIndex)
        {
            this.liftIndex = liftIndex;
        }
    }

    class GameState
    {
        private const int MAX_WAIT_TIME = 360;

        private int buildingHeight;
        private int liftCount;
        private List<Order> orders = new List<Order>();
        private List<Lift> lifts = new List<Lift>();

        public GameState(int buildingHeight, int liftCount)
        {
            this.buildingHeight = buildingHeight;
            this.liftCount = liftCount;
            for (int i = 0; i < liftCount; i++)
                lifts.Add(new Lift());
        }

        public int getBuildingHeight()
        {
            return buildingHeight;
        }

        public int getLiftCount()
        {
            return liftCount;
        }

        public Lift getLift(int liftIndex)
        {
            return lifts[liftIndex];
        }

        //Check order existance on specified floor
        public bool ExistsOrder(int floor)
        {
            for (int i = 0; i < orders.Count; i++)
                if (orders[i].getSourceFloor() == floor && orders[i].getLiftIndex() == -1)
                    return true;
            return false;
        }

        public bool ExistsUpOrder(int floor)
        {
            for (int i = 0; i < orders.Count; i++)
                if (orders[i].getSourceFloor() == floor && orders[i].isUpDirection() && orders[i].getLiftIndex() == -1)
                    return true;
            return false;
        }

        public void AddNewOrder(int time, int floor, char direction)
        {
            orders.Add(new Order(time, floor, direction == 'u' || direction == 'U'));
        }

        public void RemoveElapsedOrders(int time)
        {
            for (int i = 0; i < orders.Count; i++)
                if (orders[i].getLiftIndex() == -1)
                    if (time - orders[i].getStartTime() > MAX_WAIT_TIME)
                    {
                        orders.RemoveAt(i);
                        i--;
                    }
        }

        public void BoardPerson(int time, int liftIndex, int targetFloor)
        {
            bool isUp = (lifts[liftIndex].getCurrentFloor() < targetFloor);
            for (int i = 0; i < orders.Count; i++)
                if (orders[i].getLiftIndex() == -1)
                    if (orders[i].getSourceFloor() == lifts[liftIndex].getCurrentFloor())
                        if (orders[i].isUpDirection() == isUp)
                        {
                            orders[i].setLiftIndex(liftIndex);
                            orders[i].setTargetFloor(targetFloor);
                            lifts[liftIndex].getInnerOrders().Add(orders[i]);
                            break;
                        }
        }

        public void UnboardPersons(int liftIndex)
        {
            for (int i = 0; i < lifts[liftIndex].getInnerOrders().Count; i++)
                if (lifts[liftIndex].getCurrentFloor() == lifts[liftIndex].getInnerOrders()[i].getTargetFloor())
                {
                    lifts[liftIndex].getInnerOrders().RemoveAt(i);
                    i--;
                }
        }

        public void ReadEvents()
        {
            string[] lineParams = SampleBot.GetSplittedLine();
            int time = int.Parse(lineParams[0]);
            RemoveElapsedOrders(time);

            int orderFloor = int.Parse(lineParams[1]);
            if (orderFloor != -1)
            {
                char direction = lineParams[2][0];
                AddNewOrder(time, orderFloor, direction);
            }

            int boardCount = SampleBot.GetIntsFromLine()[0];
            for (int i = 0; i < boardCount; i++)
            {
                int[] prm = SampleBot.GetIntsFromLine();
                int liftIndex = prm[0];
                int personCount = prm[1];
                for (int j = 0; j < personCount; j++)
                {
                    int targetFloor = prm[j+2];
                    BoardPerson(time, liftIndex, targetFloor);
                }
            }
        }

        public void UpdateLiftState(String action)
        {
            for (int i = 0; i < action.Length; i++)
            {
                if (action[i] == 'S')
                {
                    //nothing to do
                }
                else if (action[i] == 'U')
                {
                    lifts[i].changeCurrentFloor(1);
                }
                else if (action[i] == 'D')
                {
                    lifts[i].changeCurrentFloor(-1);
                }
                else if (action[i] == 'u')
                {
                    UnboardPersons(i);
                }
                else if (action[i] == 'd')
                {
                    UnboardPersons(i);
                }
            }
        }
    }

    class Bot
    {
        private GameState gameState;

        public Bot()
        {
        }

        public String GetName()
        {
            return "SampleCSharpBot";
        }

        public void SetParams(int buildingHeight, int liftCount, int gameTime)
        {
            gameState = new GameState(buildingHeight, liftCount);
        }

        public String GetAction()
        {
            gameState.ReadEvents();

            String action = "";
            for (int i = 0; i < gameState.getLiftCount(); i++)
                action += GetAction(i);

            gameState.UpdateLiftState(action);
            return action;
        }

        private String GetAction(int liftIndex)
        {
            int currentFloor = gameState.getLift(liftIndex).getCurrentFloor();
            List<Order> innerOrders = gameState.getLift(liftIndex).getInnerOrders();

            if (innerOrders.Count > 0)
            {
                bool wantUp = false;
                bool wantDown = false;
                bool wantExit = false;
                for (int i = 0; i < innerOrders.Count; i++)
                    if (innerOrders[i].getTargetFloor() == currentFloor)
                        wantExit = true;
                    else if (innerOrders[i].getTargetFloor() > currentFloor)
                        wantUp = true;
                    else
                        wantDown = true;

                if (wantExit)
                {
                    if (wantUp)
                        return "u";
                    if (wantDown)
                        return "d";
                    if (currentFloor < gameState.getBuildingHeight())
                        return "u";
                    else
                        return "d";
                }

                if (wantUp)
                    return "U";
                else
                    return "D";
            }
            else
            {
                int nearestOrderFloor = FindNearestOrder(currentFloor);
                if (nearestOrderFloor == -1)
                    return "S";

                if (nearestOrderFloor == currentFloor)
                    if (gameState.ExistsUpOrder(currentFloor))
                        return "u";
                    else
                        return "d";

                if (nearestOrderFloor > currentFloor)
                    return "U";
                else
                    return "D";
            }
        }

        //Actually, it's not 'nearest' one :) Please implement nearest by yourself
        private int FindNearestOrder(int floor)
        {
            for (int i = floor; i >= 1; i--)
                if (gameState.ExistsOrder(i))
                    return i;

            for (int i = floor + 1; i <= gameState.getBuildingHeight(); i++)
                if (gameState.ExistsOrder(i))
                    return i;

            return -1;
        }
    }


    class SampleBot
    {
        public static int[] GetIntsFromLine()
        {
            return Array.ConvertAll<string, int>(GetSplittedLine(), delegate(string input) { return int.Parse(input); });
        }

        public static string[] GetSplittedLine()
        {
            string line = Console.ReadLine();
            return line.Split(new char[] { ' ', '\t' });
        }

        static void Main(string[] args)
        {
            Bot bot = new Bot();
            while (true)
            {
                string command = Console.ReadLine().Trim();

                if ("GetName" == command)
                {
                    Console.WriteLine(bot.GetName());
                }
                else if ("SetParams" == command)
                {
                    int[] line = GetIntsFromLine();
                    int buildingHeight = line[0];
                    int liftCount = line[1];
                    int gameTime = line[2];

                    bot.SetParams(buildingHeight, liftCount, gameTime);
                }
                else if ("GetAction" == command)
                {
                    Console.WriteLine(bot.GetAction());
                }
                else
                {
                    Console.WriteLine("Unknown command: " + command);
                }
            }
        }
    }
}
