# frozen_string_literal: true in_n, in_q, in_s, in_t, in_c = gets.chomp.split.map(&:to_i) in_q.times do q_u, q_v = gets.chomp.split.map(&:to_i) if q_u == q_v puts 0 next end puts [ (q_v - q_u) % in_n, (in_s - q_u) % in_n + in_c + (q_v - in_t) % in_n ].min end