inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
inlineintgn(){ char c = getchar(); for (; !isdigit(c); c = getchar()); return c - '0'; }
#define N 107 #define fr first #define sc second #define mp make_pair #define pb push_back #define pii pair<int, int>
int a[N][N];
vector<pair<pii,pii>> s;
inlinevoidwork(){ s.clear(); int n = rd(), m = rd(); for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) a[i][j] = gn(); if (a[1][1]) {puts("-1"); return;} for (int i = 1; i <= n; ++i) for (int j = m; j > 1; --j) if (a[i][j]) s.pb(mp(mp(i,j - 1), mp(i, j))); for (int i = n; i > 1; --i) if (a[i][1]) s.pb(mp(mp(i - 1, 1), mp(i, 1))); printf("%d\n", (int)s.size()); for (auto x : s) printf("%d %d %d %d\n", x.fr.fr, x.fr.sc, x.sc.fr, x.sc.sc); }
intmain(){ for (int t = rd(); t; --t) work(); return0; }
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
#define N 107
inlinevoidwork(){ int x = rd(), d = rd(); if (x % d != 0) {puts("NO"); return;} if (x / d % d != 0) {puts("NO"); return;} int cnt = 0; for (; x % d == 0; x /= d, ++cnt); int lim = sqrt(x); for (int i = 2; i <= lim; ++i) if (x % i == 0) {puts("YES"); return;} if (cnt == 2) {puts("NO"); return;} lim = sqrt(d); for (int i = 2; i <= lim; ++i) if (d % i == 0) { if (cnt > 3) {puts("YES"); return;} if (1ll * i * x % d != 0 || 1ll * d / i * x % d != 0) {puts("YES"); return;} } puts("NO"); }
intmain(){ for (int t = rd(); t; --t) work(); return0; }
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
#define N 100007
int f[N][40], a[N], res[N];
bool vis[N];
queue<int> s[N];
set<int> S;
inlinevoidwork(){
int n = rd(); for (int i = 1; i <= n; ++i) { f[i][0] = rd(); vis[f[i][0]] = 1; } for (int j = 1; j < 40; ++j) for (int i = 1; i <= n; ++i) f[i][j] = f[f[i][j - 1]][j - 1];
int mx = 0, cnt = 0; for (int i = 1; i <= n; ++i) { if (!vis[i]) ++cnt; a[i] = rd(); mx = max(mx, a[i]); } cnt = (mx - n) / cnt;
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= n; ++i) { int u = i; for (int k = 30; k >= 0; --k) if (cnt & (1 << k)) u = f[u][k]; res[i] = a[u]; s[res[i]].push(i); vis[res[i]] = 1; //Sx不空 }
for (int i = 1; i <= n; ++i) if (vis[i]) s[i].pop(); //把i放到Si最靠前的位置
int nw = 0; for (int i = 1; i <= n; ++i) if (!vis[i]) { while (nw <= i) { while (s[nw].size()) { S.insert(s[nw].front()); s[nw].pop(); } ++nw; } res[*S.begin()] = i; S.erase(S.begin()); } for (int i = 1; i <= n; ++i) printf("%d ", res[i]); }
intmain(){ for (int t = 1; t; --t) work(); return0; }
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
#define N 500007 #define inf 1e9 + 7
int n, a[N], f[N], g[N][2];
inlineintcalc(){
int p = 0; //maxpos for (int i = 1; i <= n; ++i) { f[i] = g[i][0] = inf; g[i][1] = -1; if (a[i] > a[p]) p = i; }
for (int i = 1; i <= p; ++i) { if (a[i] > a[i - 1]) f[i] = min(f[i], f[i - 1]); if (a[i] > f[i - 1]) f[i] = min(f[i], a[i - 1]); }
g[p][0] = f[p]; //承接第一阶段的最宽松约束 for (int i = p + 1; i <= n; ++i) { if (a[i] < a[i - 1]) g[i][0] = min(g[i][0], g[i - 1][0]); if (a[i] < g[i - 1][1]) g[i][0] = min(g[i][0], a[i - 1]); if (a[i] > a[i - 1]) g[i][1] = max(g[i][1], g[i - 1][1]); if (a[i] > g[i - 1][0]) g[i][1] = max(g[i][1], a[i - 1]); }
int ans = 0; for (int i = n; i > p; --i) { if (a[i] > a[i + 1]) f[i] = min(f[i], f[i + 1]); if (a[i] > f[i + 1]) f[i] = min(f[i], a[i + 1]); if (g[i][1] > f[i]) ++ans; }
return ans; }
intmain(){ n = rd(); for (int i = 1; i <= n; ++i) a[i] = rd(); int ans = calc(); reverse(a + 1, a + 1 + n); printf("%d\n", ans + calc()); return0; }