After escaping the Magical Corridor, you realize your ordeal is far from over—there’s a second challenge, and KenKenKen’s relentless pursuit continues.
This time, however, his curse has weakened. The restriction on your bag—limiting you to carrying only one gem of each color—has vanished!
Now, you can collect as many gems of any color as you wish, but your objective remains unchanged:
The magical movement rules are still in effect:
Can you use this newfound freedom to outsmart KenKenKen and finally escape? Your cunning and strategy will decide your fate.
The first line contains two integers N and Q, the length of the corridor and the number of queries.
The second line contains N integers C1,C2,...,CN , where Ci represents the color of the gem at position i.
The third line contains N integers W1,W2,...,WN , where Wi represents the weight of the gem at position i.
The next Q lines each contain a single integer m, specifying the endpoint for each query.
For each query, output two integers in a single line:
Please remember to print "\n" at the end of each line.