#include #include #include #include #define REP(i, cnt) for (int i = 0; i < (cnt); i++) #define N 19 using namespace std; typedef long long int64; int ldist[1000004]; int rdist[1000004]; int main() { int inputcount; scanf("%d", &inputcount); REP(inp, inputcount) { int rows; scanf("%d", &rows); REP(i, 1000004) { ldist[i] = -1; rdist[i] = -1; } int best = 999999999; int prevb = 999999999; REP(row, rows) { int a, b; scanf("%d%d", &a, &b); a++, b++; for (int x = 0; x < a; x++) ldist[x] = 0; ldist[a] = 1; for (int x = a+1; x <= b; x++) { if (x > 0 && ldist[x-1] >= 0 && (ldist[x-1] < ldist[x] || ldist[x] < 0)) ldist[x] = ldist[x-1]; if (ldist[x] >= 0) ldist[x]++; } if (ldist[b]-1 < best) best = ldist[b]-1; rdist[b] = 0; for (int x = b-1; x >= a-1; x--) { if (x >= prevb) rdist[x] = 1; else if (rdist[x+1] >= 0 && (rdist[x+1] < rdist[x] || rdist[x] < 0)) rdist[x] = rdist[x+1]; if (rdist[x] >= 0) rdist[x]++; } if (rdist[a-1]-1 < best) best = rdist[a-1]-1; prevb = b; } printf("K prechodu reky je treba %d pontonu.\n", best); } return 0; }