class Yukicoder def initialize n = gets.to_i k = gets.to_i array = (1..n).to_a k.times do i, j = gets.split.map(&:to_i) array[i-1], array[j-1] = array[j-1], array[i-1] end list = gets.split.map{|i| n-i.to_i} array = array.map{|i| n-i} swap_list = [] 0.upto(n-1) do |i| j = array.index(list[i]) while list[i] != array[i] array[j-1], array[j] = array[j], array[j-1] swap_list << "#{j+1} #{j}" j -= 1 end end puts swap_list.size puts swap_list end end Yukicoder.new