import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class SampleBot
{
    private static class Lift
    {
        private int currentFloor;
        private List<Order> innerOrders = new ArrayList<Order>();

        public Lift()
        {
            currentFloor = 1;
        }

        public int getCurrentFloor()
        {
            return currentFloor;
        }

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

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

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

        public Order(int time, int sourceFloor, boolean 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 boolean 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;
        }
    }

    private static class GameState
    {
        private static final int MAX_WAIT_TIME = 360;

        private int buildingHeight;
        private int liftCount;
        private List<Order> orders = new ArrayList<Order>();
        private List<Lift> lifts = new ArrayList<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.get(liftIndex);
        }

        //Check order existance on specified floor
        public boolean existsOrder(int floor)
        {
            for (int i = 0; i < orders.size(); i++)
                if (orders.get(i).getSourceFloor() == floor && orders.get(i).getLiftIndex() == -1)
                    return true;
            return false;
        }

        public boolean existsUpOrder(int floor)
        {
            for (int i = 0; i < orders.size(); i++)
                if (orders.get(i).getSourceFloor() == floor && orders.get(i).isUpDirection() && orders.get(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.size(); i++)
                if (orders.get(i).getLiftIndex() == -1)
                    if (time - orders.get(i).getStartTime() > MAX_WAIT_TIME)
                    {
                        orders.remove(i);
                        i--;
                    }
        }

        public void boardPerson(int time, int liftIndex, int targetFloor)
        {
            boolean isUp = (lifts.get(liftIndex).getCurrentFloor() < targetFloor);
            for (int i = 0; i < orders.size(); i++)
                if (orders.get(i).getLiftIndex() == -1)
                    if (orders.get(i).getSourceFloor() == lifts.get(liftIndex).getCurrentFloor())
                        if (orders.get(i).isUpDirection() == isUp)
                        {
                            orders.get(i).setLiftIndex(liftIndex);
                            orders.get(i).setTargetFloor(targetFloor);
                            lifts.get(liftIndex).getInnerOrders().add(orders.get(i));
                            break;
                        }
        }

        public void unboardPersons(int liftIndex)
        {
            for (int i = 0; i < lifts.get(liftIndex).getInnerOrders().size(); i++)
                if (lifts.get(liftIndex).getCurrentFloor() == lifts.get(liftIndex).getInnerOrders().get(i).getTargetFloor())
                {
                    lifts.get(liftIndex).getInnerOrders().remove(i);
                    i--;
                }
        }

        public void readEvents(Scanner scanner)
        {
            int time = scanner.nextInt();
            removeElapsedOrders(time);

            int orderFloor = scanner.nextInt();
            if (orderFloor != -1)
            {
                char direction = scanner.next().charAt(0);
                addNewOrder(time, orderFloor, direction);
            }

            int boardCount = scanner.nextInt();
            for (int i = 0; i < boardCount; i++)
            {
                int liftIndex = scanner.nextInt();
                int personCount = scanner.nextInt();
                for (int j = 0; j < personCount; j++)
                {
                    int targetFloor = scanner.nextInt();
                    boardPerson(time, liftIndex, targetFloor);
                }
            }
        }

        public void updateLiftState(String action)
        {
            for (int i = 0; i < action.length(); i++)
            {
                if (action.charAt(i) == 'S')
                {
                    //nothing to do
                }
                else if (action.charAt(i) == 'U')
                {
                    lifts.get(i).changeCurrentFloor(1);
                }
                else if (action.charAt(i) == 'D')
                {
                    lifts.get(i).changeCurrentFloor(-1);
                }
                else if (action.charAt(i) == 'u')
                {
                    unboardPersons(i);
                }
                else if (action.charAt(i) == 'd')
                {
                    unboardPersons(i);
                }
            }
        }
    }

    private static class Bot
    {
        private GameState gameState;

        public Bot()
        {
        }

        public String getName()
        {
            return "SampleJavaBot";
        }

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

        public String getAction(Scanner scanner)
        {
            gameState.readEvents(scanner);

            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.size() > 0)
            {
                boolean wantUp = false;
                boolean wantDown = false;
                boolean wantExit = false;
                for (int i = 0; i < innerOrders.size(); i++)
                    if (innerOrders.get(i).getTargetFloor() == currentFloor)
                        wantExit = true;
                    else if (innerOrders.get(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;
        }
    }

    public static void main(String argc[])
    {
        Scanner scanner = new Scanner(System.in);
        Bot bot = new Bot();
        while (true)
        {
            String command = scanner.next();

            if ("GetName".equals(command))
            {
                System.out.println(bot.getName());
            }
            else if ("SetParams".equals(command))
            {
                int buildingHeight = scanner.nextInt();
                int liftCount = scanner.nextInt();
                int gameTime = scanner.nextInt();

                bot.setParams(buildingHeight, liftCount, gameTime);
            }
            else if ("GetAction".equals(command))
            {
                System.out.println(bot.getAction(scanner));
            }
            else
            {
                System.out.println("Unknown command: " + command);
            }
        }

    }
}
