In the Continental Magic Association, n mages are undergoing the First-Class Mage Exam. Each mage has a mana value represented by an integer ranging from -99 to 99. The Great Mage Serie, intending to prevent Frieren from passing easily, wants to evaluate the mages from multiple complex perspectives and will issue q queries. Each query specifies a different sorting rule.

Your task is to sort the mages based on the rule specified in the query. If a tie occurs under any rule, you should resolve it by sorting the mages' names in ascending lexicographical (alphabetical) order.
The q queries will be represented by an integer rule (from 0 to 4), corresponding to the following sorting rules:
Rule 0 (Ascending): Sort mages by their power value in ascending order.
Rule 1 (Square Ascending): Sort mages by the square of their power value in ascending order.
Rule 2 (Digit Sum Ascending): Sort mages by the sum of their power value's digits in ascending order (ignoring the negative sign). For example, the digit sum of -85 is 8 + 5 = 13.
Rule 3 (Swapped Digit Ascending): Swap the tens digit and the units digit of the power value, and sort in ascending order. For example, 78 becomes 87, -6 becomes -60.
Rule 4 (Prime First Ascending): Take the absolute value of each mage's power value. If this absolute value is a prime number (e.g., 2, 3, 5...), the mage must be placed at the front. The remaining mages are placed behind. Within the prime and non-prime groups, sort them by their original power value in ascending order.
The first line contains two integers n and q, representing the number of mages and the number of queries.
The next n lines each contain a string name and their value.
The next q lines each contain a single integer rule, representing the sorting rule for that query.
Constraints:
1 <= n, q <= 100
len(name) < 30
-99 <= value <= 99
Testcase Design:
For each query, output the sorted names of the mages.
Remember to add '\n' at the end of every line.