/* Problem URL: https://codeforces.com/problemset/problem/2096/C */ #include #include #include using namespace std; using namespace __gnu_pbds; template > using ordered_set = tree; #define V vector #define rmin(a, b) a = min(a, b) #define rmax(a, b) a = max(a, b) #define rep(i, lim) for (int i = 0; i < (lim); i++) #define nrep(i, s, lim) for (int i = s; i < (lim); i++) #define repv(i, v) for (auto &i : (v)) #define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } #define sortv(v) sort(v.begin(), v.end()) #define all(v) (v).begin(), (v).end() using vi = vector; using vvi = vector; using vvvi = vector; using vvvvi = vector; using ll = long long; using vl = vector; using vvl = vector; using vvvl = vector; using vvvvl = vector; template auto operator<<(ostream &os, const vector &vec)->ostream& { os << vec[0]; for (size_t i = 1; i < vec.size(); i++) { os << ' ' << vec[i]; } os << '\n'; return os; } template auto operator>>(istream &is, vector &vec)->istream& { for (auto &i : vec) { is >> i; } return is; } template auto operator<<(ostream &os, const vector> &vec)->ostream& { for (auto &i : vec) { os << i[0]; for (size_t j = 1; j < i.size(); j++) { os << ' ' << i[j]; } os << '\n'; } return os; } template auto operator>>(istream &is, vector> &vec)->istream& { for (auto &i : vec) { for (auto &j : i) { is >> j; } } return is; } const int oo = INT32_MAX >> 1; const ll OO = INT64_MAX >> 1; void pre() { } #define TEST 1 void solve() { int n; cin >> n; vvl h(n, vl(n)); cin >> h; V>> graph(n * 2); auto addedge = [&](int u, int v, ll c) { graph[u].emplace_back(v, c); }; vl a(n); vl b(n); cin >> a >> b; vl memo(n * 2, -1); function dfs = [&](int i) { if (i >= (n - 1) * 2) { return 0LL; } ll &ans = memo[i]; if (ans != -1) { return ans; } ans = OO; repv(j, graph[i]) { ll tmp = dfs(j.first); if (tmp == OO) { continue; } rmin(ans, tmp + j.second); } return ans; }; rep(j, n - 1) { int pos = (1 << 4) - 1; int msk = (1 << 5) - 1; rep(i, n) { if (h[i][j] == h[i][j + 1]) { pos &= msk ^ 1; pos &= msk ^ (1 << 3); continue; } if (h[i][j] == h[i][j + 1] - 1) { pos &= msk ^ (1 << 2); continue; } if (h[i][j] - 1 == h[i][j + 1]) { pos &= msk ^ (1 << 1); } } if (pos == 0) { cout << "-1\n"; return; } if (pos & 1) { addedge(j * 2, (j + 1) * 2, 0); } if (pos & 2) { addedge(j * 2, (j + 1) * 2 + 1, b[j + 1]); } if (pos & 4) { addedge(j * 2 + 1, (j + 1) * 2, 0); } if (pos & 8) { addedge(j * 2 + 1, (j + 1) * 2 + 1, b[j + 1]); } } ll ans = min(dfs(0), dfs(1) + b[0]); if (ans >= OO) { cout << "-1\n"; return; } graph.assign(n * 2, V>()); fill(all(memo), -1); rep(i, n - 1) { int pos = (1 << 4) - 1; int msk = (1 << 5) - 1; rep(j, n) { if (h[i][j] == h[i + 1][j]) { pos &= msk ^ 1; pos &= msk ^ (1 << 3); continue; } if (h[i][j] == h[i + 1][j] - 1) { pos &= msk ^ (1 << 2); continue; } if (h[i][j] - 1 == h[i + 1][j]) { pos &= msk ^ (1 << 1); } } if (pos == 0) { cout << "-1\n"; return; } if (pos & 1) { addedge(i * 2, (i + 1) * 2, 0); } if (pos & 2) { addedge(i * 2, (i + 1) * 2 + 1, a[i + 1]); } if (pos & 4) { addedge(i * 2 + 1, (i + 1) * 2, 0); } if (pos & 8) { addedge(i * 2 + 1, (i + 1) * 2 + 1, a[i + 1]); } } ll tmp = min(dfs(0), dfs(1) + a[0]); if (tmp >= OO) { cout << "-1\n"; return; } cout << ans + tmp << '\n'; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); pre(); int t; (TEST && cin >> t) || (t = 1); while (t--) { solve(); } }