#define _CRT_SECURE_NO_WARNINGS // #define _GLIBCXX_DEBUG #include using namespace std; template ostream &operator << (ostream &os, const vector &); template typename enable_if<(n>=sizeof...(T))>::type _ot(ostream &, const tuple &){} template typename enable_if<(n< sizeof...(T))>::type _ot(ostream &os, const tuple &t){ os << (n == 0 ? "" : " ") << get(t); _ot(os, t); } template ostream &operator << (ostream &os, const tuple &t){ _ot<0>(os, t); return os; } template ostream & operator << (ostream &os, const pair &p){ return os << "(" << p.first << ", " << p.second << ") "; } template ostream &operator << (ostream &os, const vector &v){ for(size_t i = 0; i < v.size(); i++) os << v[i] << (i + 1 == v.size() ? "" : ", "); return os; } #ifdef DEBUG #define dump(...) (cerr << #__VA_ARGS__ << " = " << make_tuple(__VA_ARGS__) \ << " (L : " << __LINE__ << ")" << endl) #else #define dump(...) #endif #define all(c) begin(c), end(c) #define range(i,a,b) for(int i = a; i < (int)(b); i++) #define rep(i,b) range(i,0,b) #define pb push_back #define eb emplace_back #define mp make_pair #define mt make_tuple using ll = long long; // #define int ll using vi = vector; using vvi = vector; using pii = pair; void fastios(){ ios_base::sync_with_stdio(0); cin.tie(0); } int const mod = 1000000007; auto const inf = numeric_limits::max()/8; void shellsort(int *l, int *r, function f){ int n = r - l, h = 1; while(3*h + 1 < n) h = 3*h + 1; for(; h != 0; h = (h-1)/3) for(int i = 0; i < h; i++) for(int j = i+h; j < n; j += h) for(int k = j; k != i && f(l[k], l[k-h]); k -= h){ swap(l[k], l[k-h]); cout << k+1 << ' ' << k-h+1 << endl; } } int l; int main(){ int n; while(cin >> n && n){ int cur[6010]; rep(i,n) cur[i] = i; int l; cin >> l; rep(i,l){ int x,y; cin >> x >> y; --x, --y; swap(cur[x], cur[y]); } int w[6010]; int dst[6010]; rep(i,n){ cin >> dst[i]; w[dst[i]-1] = i; } shellsort(cur, cur+n, [&](int a, int b){ return w[a] < w[b]; }); } }