#include using namespace std; int t, n; pair x[1005]; int main() { scanf("%d", &t); while (t--) { int ta, tb, ans = 0; scanf("%d", &n); scanf("%d%d", &ta, &tb); for (int i = 1; i < n; i++) { int tc, td; scanf("%d%d", &tc, &td); if (max(ta, tc) <= min(tb, td)) { ta = max(ta, tc); tb = min(tb, td); } else if (tb < tc) { int tmp = (tc - tb + 1) / 2; ans += tmp; tb += tmp * 2; ta = max(tb - 1, tc); tb = min(tb, td); } else { int tmp = (ta - td + 1) / 2; ans += tmp; ta -= tmp * 2; tb = min(ta + 1, td); ta = max(ta, tc); } } printf("%d\n", ans); } }