Under construction

Still being built. Things may change

Finding a Home

Interview

Time limit1sMemory limit1024 MB

Summary
For each query L R, people L to R may swap houses among themselves. Print the sequence that maximizes the sum of house index times person number.
Level

Easy3 of 10

Topics
Greedy, Sorting, Implementation
Solved
No attempts yet

Problem

Seogang University has NN empty houses. The university wants to assign NN people so that each person moves into exactly one house. For each person yy who moves into house xx, the university reduces their tax by xyxy.

The assignment is given as a sequence A1,A2,…,ANA_1, A_2, \dots, A_N of integers from 11 to NN, where each value appears once. This means house ii is assigned to person AiA_i.

Write a program that solves the following query.

  • LL RR: Starting from the initial assignment, the people numbered LL through RR may swap the houses currently assigned to them with each other as many times as the university wants. After reassigning, the total tax reduction is maximized. Output the sequence AA.

Every query is independent. After each query, the sequence AA returns to its initial state.

Input

The first line contains the length NN of the sequence. (1≤N≤300)(1 \le N \le 300)

The second line contains the sequence A1,A2,…,ANA_1, A_2, \dots, A_N, separated by spaces. (1≤Ai≤N)(1 \le A_i \le N)

The third line contains the number of queries MM. (1≤M≤300)(1 \le M \le 300)

The next MM lines each contain two integers LL and RR, separated by a space. (1≤L≤R≤N)(1 \le L \le R \le N)

Output

For each query, print the answer on its own line.

Each line contains the sequence A1,A2,…,ANA_1, A_2, \dots, A_N after the query is finished, separated by spaces.

If there are several valid answers, print any one of them.

Examples1

  1. Example 1

    Input
    5
    1 3 4 2 5
    3
    2 3
    3 5
    1 5
    
    Expected output
    1 2 4 3 5
    1 3 4 2 5
    1 2 3 4 5