IDENTIFICATION DIVISION.
PROGRAM-ID. 158D.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
  SELECT STDIO ASSIGN TO KEYBOARD. 
*>
DATA DIVISION.
FILE SECTION.
FD STDIO.
01 INP        PIC X(100000).
*>
WORKING-STORAGE SECTION.
01 IN2        PIC X(20).
01 Q          PIC 9(6).
01 POS1       PIC 9(6) COMP.
01 POS2       PIC 9(6) COMP.
01 POS1W      PIC 9(6) COMP.
01 POS2W      PIC 9(6) COMP.
01 FORMA.
   03 FILLER  PIC X(200000).
   03 S       PIC X(100000).
   03 FILLER  PIC X(200000).
01 FORMA-R    REDEFINES FORMA.
   03 S-R     PIC X(1) OCCURS 500000.
01 T          PIC 9(1).
01 F          PIC 9(1).
01 C          PIC X(1).
01 W          PIC X(1).
01 I          PIC 9(6) COMP.
01 F-T        PIC S9(1).
*> 
PROCEDURE DIVISION.
MAIN-001.
  OPEN INPUT STDIO.
  READ STDIO.
  CLOSE STDIO.
*>
  UNSTRING INP DELIMITED BY ALL ' '
      INTO S COUNT POS2.
  COMPUTE POS1 = 1    + 200000.
  COMPUTE POS2 = POS2 + 200000.
*>  
  ACCEPT Q.
*>
  MOVE 1 TO F-T.
  PERFORM VARYING I FROM 1 BY 1
            UNTIL I > Q
    ACCEPT IN2
    UNSTRING IN2 DELIMITED BY ALL ' '
        INTO T F C
*>
    IF (T = 1)
      COMPUTE F-T = F-T * -1
      MOVE POS1  TO POS1W
      MOVE POS2  TO POS1
      MOVE POS1W TO POS2
    ELSE
      IF (F = 1)
        COMPUTE POS1 = POS1 - F-T
        MOVE C           TO S-R(POS1)
      ELSE
        COMPUTE POS2 = POS2 + F-T
        MOVE C           TO S-R(POS2)
      END-IF
    END-IF
  END-PERFORM.
 
  IF (F-T = -1)
    MOVE POS1  TO      POS1W
    MOVE POS2  TO POS1 POS2W
    MOVE POS1W TO POS2
*>
    PERFORM UNTIL (POS1W <= POS2W)
      MOVE S-R(POS1W) TO W
      MOVE S-R(POS2W) TO S-R(POS1W)
      MOVE W          TO S-R(POS2W)
      COMPUTE POS1W = POS1W - 1
      COMPUTE POS2W = POS2W + 1
    END-PERFORM
  END-IF.
*>
  DISPLAY FORMA(POS1:POS2 - POS1 + 1).
*>
MAIN-EXIT.
  STOP RUN.