#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") //#pragma comment(linker, "/stack:200000000") #include using namespace std; typedef long long ll; typedef unsigned long long ull; typedef double ld; typedef pair pii; typedef pair pll; typedef pair pdd; #define X first #define Y second //#include //using namespace boost; /* #include #include using namespace __gnu_pbds; typedef tree, rb_tree_tag, tree_order_statistics_node_update> rbtree; rbtree T; */ using i32 = int; using i64 = long long; using u8 = unsigned char; using u32 = unsigned; using u64 = unsigned long long; using f64 = double; using f80 = long double; //using i128 = __int128_t; //using u128 = __uint128_t; //using i128 = i64; //using u128 = u64; ll power(ll a, ll b, ll p) { if (!b) return 1; ll t = power(a, b/2, p); t = t*t%p; if (b&1) t = t*a%p; return t; } ll exgcd(ll a, ll b, ll &x, ll &y) { if (b == 0) { x = 1; y = 0; return a; } ll px, py; ll d = exgcd(b, a%b, px, py); x = py; y = px-a/b*py; return d; } template inline void freshmin(T &a, const T &b) { if (a > b) a = b; } template inline void freshmax(T &a, const T &b) { if (a < b) a = b; } template void print(const T &a) { for (auto x : a) printf("%d ", x); puts(""); } const int MAXN = 1010; const int INF = 1000000000; //const int MOD = 998244353; const int MOD = 2520; const int INV2 = (MOD+1)/2; int n; int a[MAXN], b[MAXN]; int F[MAXN][4]; int main() { int T; scanf("%d", &T); while (T --) { scanf("%d", &n); for (int i = 1; i <= n; ++ i) { scanf("%d%d", &a[i], &b[i]); for (int j = 0; j < 4; ++ j) F[i][j] = INF; } int L = a[1], R = b[1]; int pos = n+1; for (int i = 2; i <= n; ++ i) { int tL = max(L, a[i]); int tR = min(R, b[i]); if (tL > tR) { pos = i; break; } L = tL; R = tR; } int ans = 0, x = 0; for (int i = pos; i <= n; ++ i) { if (i == pos && b[i] < L) x = L; if (i == pos && a[i] > R) x = R; if (a[i] <= x && x <= b[i]) { } else if (b[i] < x) { int go = abs(x-b[i]); if (go%2 == 1) { int j = i; while (j <= n) { if (!(a[j] <= b[i] && b[i] <= b[j])) break; ++ j; } if (j <= n && b[i]-1 >= a[i] && b[i]-1 >= b[j]) { go ++; } } ans += (go+1)/2; x -= go; } else { int go = abs(x-a[i]); if (go%2 == 1) { int j = i; while (j <= n) { if (!(a[j] <= a[i] && a[i] <= b[j])) break; ++ j; } if (j <= n && a[i]+1 <= b[i] && a[j] >= a[i]+1) { go ++; } } ans += (go+1)/2; x += go; } } printf("%d\n", ans); } return 0; }