import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } long L; long[] A; void main() { try { for (; ; ) { const numCases = readInt(); foreach (caseId; 0 .. numCases) { L = readLong(); A = new long[9]; foreach (i; 0 .. 9) { A[i] = readLong(); } auto p = new long[9]; auto q = new long[9]; int cnt; foreach (i; 0 .. 9) { if (A[i] == 0) { if (cnt++ == 0) { p[i] = 0; q[i] = 1; } else { p[i] = L; q[i] = -1; } } else { p[i] = A[i]; q[i] = 0; } } assert(cnt == 2); long[] ts; ts ~= 1; ts ~= L; foreach (i; 0 .. 9) foreach (j; i + 1 .. 9) { // p[i] + q[i] t = p[j] + p[j] t long dp = -(p[j] - p[i]); long dq = q[j] - q[i]; if (dq != 0) { if (dq < 0) { dp *= -1; dq *= -1; } if (dp % dq == 0) { ts ~= dp / dq; ts ~= dp / dq + 1; } else { ts ~= dp / dq + ((dp % dq > 0) ? 1 : 0); } } } ts = ts.sort.uniq.array; bool check(long t, int i, int j, int k) { const a = p[i] + q[i] * t; const b = p[j] + q[j] * t; const c = p[k] + q[k] * t; return (a != b && a != c && b != c && ((a < b && b > c) || (a > b && b < c))); } long ans; foreach (j; 1 .. ts.length) { const t = ts[j - 1]; bool ok = true; ok = ok && (0 < t && t < L); ok = ok && check(t, 0, 1, 2); ok = ok && check(t, 3, 4, 5); ok = ok && check(t, 6, 7, 8); ok = ok && check(t, 0, 3, 6); ok = ok && check(t, 1, 4, 7); ok = ok && check(t, 2, 5, 8); ok = ok && check(t, 2, 4, 6); ok = ok && check(t, 0, 4, 8); if (ok) { ans += (ts[j] - ts[j - 1]); } } writeln(ans); } } } catch (EOFException e) { } }