#include <bits/stdc++.h>
using namespace std;
 
#define all(x) (x).begin(),(x).end()
#define print(x) cout << (x) << endl
typedef long long ll;
using P = pair<int,int>;
using Graph = vector<vector<int>>;
const ll MOD = 1000000007;
//const ll MOD = 998244353;

void solve() {
    ll l, r;   cin >> l >> r;
    ll ma = r, mi = l + l;
    if(ma < mi){
        print(0);
        return;
    }
    print((ma - mi + 1) * (ma - mi + 2) / 2);
    return;
}

int main(){
    int t;  cin >> t;
    while(t--) solve();
    return 0;
}
#include <bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;

// #define int long long
#define rep(i, n) for (int i = (int)(0); i < (int)(n); ++i)
#define reps(i, n) for (int i = (int)(1); i <= (int)(n); ++i)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; i--)
#define irep(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define ireps(i, m, n) for (int i = (int)(m); i <= (int)(n); ++i)
#define irreps(i, m, n) for (int i = ((int)(n)-1); i > (int)(m); ++i)
#define SORT(v, n) sort(v, v + n);
#define REVERSE(v, n) reverse(v, v+n);
#define vsort(v) sort(v.begin(), v.end());
#define all(v) v.begin(), v.end()
#define mp(n, m) make_pair(n, m);
#define cinline(n) getline(cin,n);
#define replace_all(s, b, a) replace(s.begin(),s.end(), b, a);
#define PI (acos(-1))
#define FILL(v, n, x) fill(v, v + n, x);
#define sz(x) (int)(x.size())

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vs = vector<string>;
using vpll = vector<pair<ll, ll>>;
using vtp = vector<tuple<ll,ll,ll>>;
using vb = vector<bool>;
using ld = long double;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;

const ll INF = 1e9+10;
const ll MOD = 1e9+7;
// const ll MOD = 998244353;
const ll LINF = 1e18;



signed main()
{
  cin.tie( 0 ); ios::sync_with_stdio( false );
  
  int n; cin>>n;
  
  vpll v(n);
  rep(i,n){
    ll x,c; cin>>x>>c;
    v[i]={c,x};
  }
  vsort(v);
  set<ll> s;
  map<ll,ll> ma,mi;
  for(auto e: v){
    ll c,x; tie(c,x)=e;
    if(!s.count(c)) ma[c]=x;
    else chmax(ma[c],x);
    if(!s.count(c)) mi[c]=x;
    else chmin(mi[c],x);
    s.insert(c);
  }
  
  vc<ll> x;
  for(auto e: s) x.push_back(e);
  
  int m=s.size();
  vc<vc<ll>> dp(m+1,vc<ll>(2));
  
  rep(i,m){
    ll L=LINF, R=LINF;
    ll now=x[i];
    if(i==0){
      R=abs(mi[now])+abs(ma[now]-mi[now]); // 左行ってから右
      L=abs(ma[now])+abs(ma[now]-mi[now]);
      dp[i+1][1]=R;
      dp[i+1][0]=L;
    }else{
      ll pre=x[i-1];
      R=dp[i][1]+abs(ma[now]-mi[now])+abs(ma[pre]-mi[now]);
      chmin(R,dp[i][0]+abs(ma[now]-mi[now])+abs(mi[pre]-mi[now]));
      L=dp[i][1]+abs(ma[now]-mi[now])+abs(ma[pre]-ma[now]);
      chmin(L,dp[i][0]+abs(ma[now]-mi[now])+abs(mi[pre]-ma[now]));
      dp[i+1][1]=R;
      dp[i+1][0]=L;
    }
  }
  
  // cout<<mi[x[m-1]]<<' '<<ma[x[m-1]]<<endl;
  
  ll ans=LINF;
  chmin(ans, dp[m][0]+abs(mi[x[m-1]]));
  chmin(ans, dp[m][1]+abs(ma[x[m-1]]));
  cout<<ans<<endl;
}