//determined #include #include #include #include #include #include #include #include #define set0(x) memset(x,0,sizeof(x)) #define debug(x) cerr<<#x<<'='< void read(T &x){ x = 0;char ch = getchar();int f = 1; while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}; while(isdigit(ch)){x = x*10+ch-48;ch=getchar();} x*=f; } ll gcd(ll a,ll b){ if(b == 0)return a; return gcd(b,a%b); } inline void ded(vector& c) { sort(c.begin(), c.end()); vector::iterator new_end = unique(c.begin(), c.end()); c.erase(new_end, c.end()); } int T,n,a[1010],b[1010],dp[2][10010]; const int inf = 1e9; int main(){ read(T); while(T--){ read(n); for(int i=1;i<=n;i++){ read(a[i]); read(b[i]); } vector dpv; for(int i=1;i<=n;i++){ dpv.push_back(a[i]-1); dpv.push_back(a[i]); dpv.push_back(a[i]+1); dpv.push_back(b[i]-1); dpv.push_back(b[i]); dpv.push_back(b[i]+1); } ded(dpv); set0(dp); int m = dpv.size(); //for(auto ct:dpv) cout<b[i] || dpv[j]=0;j--){ cmia[dpv[j]%2] = min(cmia[dpv[j]%2],dp[cu][j]+dpv[j]/2); if(dpv[j]%2 == 0)dp[cu^1][j] = min(dp[cu^1][j],min(cmia[dpv[j]%2]-dpv[j]/2,cmia[(dpv[j]%2)^1]-dpv[j]/2+1)); if(dpv[j]%2 == 1)dp[cu^1][j] = min(dp[cu^1][j],min(cmia[dpv[j]%2]-dpv[j]/2,cmia[(dpv[j]%2)^1]-dpv[j]/2)); } cu^=1; } int ans = inf; for(int i=0;i