Sorting
Aizhan has a sequence of n integers S[0]
, S[1]
, ..., S[n-1]
. The sequence consists of distinct numbers from 0 to n - 1. She is trying to sort this sequence in ascending order by swapping some pairs of elements. Her friend Ermek is also going to swap some pairs of elements - not necessarily in a helpful way.
Ermek and Aizhan are going to modify the sequence in a series of rounds. In each round, first Ermek makes a swap and then Aizhan makes another swap. More precisely, the person making a swap chooses two valid indices and swaps the elements at those indices. Note that the two indices do not have to be distinct. If they are equal, the current person swaps an element with itself, which does not change the sequence.
Aizhan knows that Ermek does not actually care about sorting the sequence S. She also knows the exact indices Ermek is going to choose. Ermek plans to take part in m rounds of swapping. We number these rounds from 0 to m - 1. For each i between 0 and m - 1 inclusive, Ermek will choose the indices x[i]
and y[i]
in round i.
Aizhan wants to sort the sequence S. Before each round, if Aizhan sees that the sequence is alreadysorted in ascending order, she will terminate the entire process. Given the original sequence S and theindices Ermek is going to choose, your task is to find a sequence of swaps, which Aizhan can use tosort the sequence S. In addition, in some subtasks you are required to find a sequence of swaps that isas short as possible. You may assume that it is possible to sort the sequence S in m or fewer rounds.
Note that if Aizhan sees that the sequence S is sorted after Ermek’s swap, she can choose to swap two equal indices (e.g. 0 and 0). As a result the sequence S is also sorted after the entire round, so Aizhan reaches her goal. Also note that if the initial sequence S is already sorted, the minimal number of rounds needed to sort it is 0.
Sample 1
Suppose that:
The initial sequence is: S = 4, 3, 2, 1, 0.
Ermek is willing to make m = 6 swaps.
The sequences X and Y, that describe the indices Ermek is going to choose are: X = 0, 1, 2, 3, 0, 1 and Y = 1, 2, 3, 4, 1, 2. In other words, the pairs of indices that Ermek plans to choose are (0, 1), (1, 2), (2, 3), (3, 4), (0, 1) and (1, 2).
In this setting Aizhan can sort the sequence S into the order 0, 1, 2, 3, 4 in three rounds. She can do so by choosing the indices (0, 4), (1, 3) and then (3, 4). The following table shows how Ermek and Aizhan modify the sequence S.
Sample 2
Suppose that:
The initial sequence is: S = 3, 0, 4, 2, 1.
Ermek is willing to make m = 5 swaps.
The pairs of indices that Ermek plans to choose are (1, 1), (4, 0), (2, 3), (1, 4) and (0, 4).
In this setting Aizhan can sort the sequence S in three rounds, for example by choosing the pairs ofindices (1, 4), (4, 2) and then (2, 2). The following table shows how Ermek and Aizhan modify the sequence S.
Input
First line contains the length of the sequence S. Second line contains the initial sequence S[0]
, S[1]
, ..., S[n-1]
(n ≤ 200000). Next line contains the number of swaps m (m ≤ 3 * n) Ermek plans to make. Each of the next m lines contains the pair of numbers x[i]
and y[i]
. They means that in i-th (0 ≤ i ≤ m - 1) round Ermek plans to swap numbers of indices x[i]
и y[i]
.
Output
In the first line print the number R of swaps Aizhan can make to sort the sequence S. In the next R lines print the sequence of swaps of minimal length, that Aizhan can use to sort S. In each line print the pair of integers p[i]
and q[i]
representing the indices Aizhan should choose in round i.