#C1613. Directory Management System

    ID: 44838 Type: Default 1000ms 256MiB

Directory Management System

Directory Management System

Sarah is designing a new algorithm for a directory management system. In this problem, you are given a list of files, each having a name (a string of lowercase letters) and a size in kilobytes. You are tasked with executing a series of operations on these files to efficiently manage the directory.

The operations are as follows:

  1. SORT: Sort the files in lexicographical order based on their names. Formally, if the file names are \( f_1, f_2, \dots, f_n \), then after sorting they satisfy \( f_1 \leq f_2 \leq \dots \leq f_n \) in dictionary order.
  2. SHORTEST: Identify and output the name of the file with the smallest size. If multiple files have the same smallest size, choose the one that is lexicographically smallest.
  3. MOVE A B X: Transfer \( X \) kilobytes from file A to file B. This operation is only valid if file A has at least \( X \) kilobytes. If not, the operation is ignored.

Your program should handle multiple test cases. For each test case, apply the operations sequentially and output the result of each SHORTEST operation.

inputFormat

The input is read from standard input (stdin) and has the following format:

T
N
name1 size1
name2 size2
...
nameN sizeN
M
op1
op2
...
opM

Where:

  • \( T \) is the number of test cases.
  • For each test case:
    • \( N \) is the number of files.
    • The next \( N \) lines each contain a file name and its size (in kilobytes), separated by a space.
    • \( M \) is the number of operations.
    • The next \( M \) lines contain one of the following operations:
      • SORT
      • SHORTEST
      • MOVE A B X: where A is the source file, B is the destination file, and X is the amount to transfer.

outputFormat

For each SHORTEST operation encountered in each test case, print the name of the file meeting the criteria. The output should be sent to standard output (stdout), with each result printed on a new line.

## sample
1
3
filea 1000
fileb 2000
filec 1500
5
SORT
SHORTEST
MOVE filea fileb 500
SHORTEST
MOVE fileb filec 2500
filea

filea

</p>