/* * Problem link * http://yukicoder.me/problems/940 */ #include using namespace std; struct INIT{INIT(){cin.tie(0);ios_base::sync_with_stdio(false);} }init; const int checklist[8][3] = { {0,1,2} ,{ 3,4,5 },{ 6,7,8 } ,{0,3,6},{1,4,7},{2,5,8},{0,4,8},{2,4,6} }; typedef long long LL; typedef pair P; #define rep(i,n) for(auto i=(n)*0;ic) || (a > b&&b < c)); } bool checkAllKadomatsu(vector a) { rep(i, 8)if (!checkKadomatsu(a[checklist[i][0]], a[checklist[i][1]], a[checklist[i][2]]))return false; return true; } int main() { #ifdef INPUT_FROM_FILE ifstream cin("sample.in"); ofstream cout("sample.out"); #endif int T; while(cin >> T) while (T--) { LL L; vector a(9); cin >> L; rep(i, 9)cin >> a[i]; set wall; wall.insert(1); wall.insert(L); vector pos; rep(i, 9) { if (a[i]== 0)pos.push_back(i); else if (a[i] < L) { wall.insert(a[i]); wall.insert(L-a[i]); if (a[i] < L - 1) { wall.insert(a[i] + 1); wall.insert(L - a[i] - 1); } if (a[i] > 1) { wall.insert(a[i] - 1); wall.insert(L - a[i] + 1); } } } vector seg; range(it, wall)seg.push_back(it); int m = seg.size(); m--; LL res = 0; rep(i, m) { a[pos[0]] = seg[i]; a[pos[1]] = L - seg[i]; if (checkAllKadomatsu(a)) { if (L - seg[i] != seg[i + 1])res += (seg[i + 1] - seg[i]); else res += (seg[i + 1] - seg[i]+1) / 2; } } cout << res << endl; } return 0; }