#include using namespace std; const int MAX_N=5+1e5; vector a,b,key; int n; int go(int pos,int pos1){ int ret=abs(pos-pos1)+1>>1; // printf("(%d %d %d)",pos,pos1,ret); return ret; } vector > f[MAX_N]; void updata(int i,int x,int y){ for(int j=0;j >::iterator it=f[i-1].begin() ;it!=f[i-1].end();++it){ if(it->first>=a[i]&&it->first<=b[i]){ updata(i,it->first,it->second); } else if(it->firstsecond+go(it->first,a[i])); if(a[i]+1<=b[i]) updata(i,a[i]+1,it->second+go(it->first,a[i]+1)); }else{ updata(i,b[i],it->second+go(it->first,b[i])); if(b[i]-1>=a[i]) updata(i,b[i]-1,it->second+go(it->first,b[i]-1)); } } // cerr< >::iterator it=f[n].begin(); it!=f[n].end();++it){ // printf("[%d %d]",it->first,it->second); ans=min(ans,it->second); } // exit(0); return ans; } int main(){ int T; scanf("%d",&T); while(T--){ scanf("%d",&n); a.resize(n+1); b.resize(n+1); key.clear(); for(int i=1;i<=n;++i){ scanf("%d%d",&a[i],&b[i]); key.push_back(a[i]); key.push_back(b[i]); } sort(key.begin(),key.end()); key.erase(unique(key.begin(),key.end()),key.end()); int ans=1e9; // printf("|%d|",dfs(20)); for(int i=0;i