#include "bits/stdc++.h" using namespace std; #define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i)) #define rep(i,j) FOR(i,0,j) #define each(x,y) for(auto &(x):(y)) #define mp make_pair #define all(x) (x).begin(),(x).end() #define debug(x) cout<<#x<<": "<<(x)< pii; typedef vector vi; typedef vector vll; int TT, L, A[3][3], ay, ax, by, bx; vector> pat; vi range; vi makeRange(){ vi aa{0,L,L / 2,L / 2 + 1}; rep(y, 3)rep(x, 3){ if(A[y][x]){ if(L - A[y][x] > 0)aa.push_back(L - A[y][x]); if(A[y][x] < L)aa.push_back(A[y][x]); } else{ if(ay == -1){ ay = y; ax = x; } else{ by = y; bx = x; } } } sort(all(aa)); aa.erase(unique(all(aa)), aa.end()); return aa; } bool kado(int a, int b, int c){ return a != b&&b != c&&c != a && ((ac) || (a>b&&b < c)); } bool check(){ int vals[3]; each(d, pat){ rep(i, 3)vals[i] = A[d[i].first][d[i].second]; if(!kado(vals[0], vals[1], vals[2]))return 0; } return 1; } void solve(){ ay = ax = by = bx = -1; range = makeRange(); ll ans = 0; rep(i, sz(range)-1){ int l = range[i], r = range[i + 1]; int len = r - l - 1; if(len <= 0)continue; int x = l + 1; A[ay][ax] = x; A[by][bx] = L - x; if(check()) ans += len; } FOR(i, 1, sz(range) - 1){ int x = range[i]; A[ay][ax] = x; A[by][bx] = L - x; if(check())++ans; } cout << ans << endl; } void init(){ vector c(3), d(3); rep(i, 3){ vector a(3), b(3); rep(j, 3){ a[j] = {i,j}; b[j] = {j,i}; } pat.push_back(a); pat.push_back(b); c[i] = {i,i}; d[i] = {i,2 - i}; } pat.push_back(c); pat.push_back(d); } int main(){ init(); ios::sync_with_stdio(0); cin.tie(0); cin >> TT; while(TT--){ cin >> L; rep(i, 3)rep(j, 3)cin >> A[i][j]; solve(); } }