#include #include using namespace std; const int N = 1000 + 10; int n; int l[N], r[N]; int ll, rr; int main(){ int T; scanf("%d", &T); while(T --){ scanf("%d", &n); for(int i = 0; i < n; ++ i){ scanf("%d%d", &l[i], &r[i]); } ll = l[0]; rr = r[0]; int f = 0; int ans = 0; if(ll == rr){ f = 1; } for(int i = 1; i < n; ++ i){ if(f == 0 && ll != rr){ if(rr >= l[i] && r[i] >= ll){ ll = max(ll, l[i]); rr = min(rr, r[i]); if(ll == rr){ f = 1; } continue; } else{ if(ll - r[i] >= l[i] - rr){ f = 1; } else{ ll = rr; f = 1; } } } if(l[i] - ll > 0){ if((l[i] - ll) % 2 == 0){ ans += (l[i] - ll) / 2; ll = l[i]; } else{ ans += (l[i] - ll) / 2; ll = l[i]; ans += (l[i] - ll) / 2 + 1; ll = l[i]; if(r[i] != l[i]){ rr = ll+1; f = 0; } } } else if(ll - r[i] > 0){ if((ll - r[i]) % 2 == 0){ ans += (ll - r[i]) / 2; ll = r[i]; } else{ ans += (ll - r[i]) / 2 + 1; ll = r[i]; if(r[i] != l[i]){ rr = ll; ll = rr - 1; f = 0; } } } else if(l[i] == r[i]){ ll = l[i]; f = 1; } } printf("%d\n", ans); } }