#include using namespace std; bool isPermutation(const vector& values) { int n = static_cast(values.size()); vector seen(n + 1, 0); for (int value : values) { if (value < 1 || value > n || seen[value] != 0) { return false; } seen[value] = 1; } return true; } bool solveSubCase(int n, const vector& r, const vector& c, vector>& answer) { if (!isPermutation(c)) { return false; } if (n == 1) { answer.assign(1, vector(1, 1)); return true; } if (n == 2) { return false; } vector rowOfValue(n + 1); for (int row = 0; row < n; row++) { rowOfValue[r[row]] = row; } answer.assign(n, vector(n)); for (int row = 0; row < n; row++) { fill(answer[row].begin(), answer[row].end(), r[row]); } for (int col = 0; col < n; col++) { int row = (rowOfValue[c[col]] + 1) % n; answer[row][col] = c[col]; } return true; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while (t--) { int n; cin >> n; vector r(n), c(n); for (int i = 0; i < n; i++) { cin >> r[i]; } for (int i = 0; i < n; i++) { cin >> c[i]; } vector> answer; if (!solveSubCase(n, r, c, answer)) { cout << -1 << '\n'; continue; } for (int row = 0; row < n; row++) { for (int col = 0; col < n; col++) { if (col > 0) { cout << ' '; } cout << answer[row][col]; } cout << '\n'; } } return 0; }