#include using namespace std; using ll = long long; #define rep(i, n) for (int i = 0; i < (int)(n); i++) int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); int n; cin >> n; vector a(n), b(n); rep(i, n) cin >> a[i]; rep(i, n) cin >> b[i]; vector> v(n + 1, vector(n + 1)); rep(i, n) rep(j, n) { if (i > j) continue; v[b[i]][b[j]] = true; } rep(i, n) cout << a[i] << " "; cout << endl; vector p; deque q; rep(i, n - 1) q.push_back(i); while (true) { p.push_back(q.front()); q.pop_front(); if (q.empty()) break; p.push_back(q.back()); q.pop_back(); if (q.empty()) break; } rep(_, n) { set st; for (int j : p) { if (!v[a[j]][a[j + 1]]) { if (st.contains(j - 1)) continue; if (st.contains(j + 1)) continue; swap(a[j], a[j + 1]); st.insert(j); } } rep(i, n) cout << a[i] << " "; cout << endl; } return 0; }