spoj biased
#include<iostream>
#include<cstring>
#define ll long long
using namespace std;
int abss(ll i,ll j)
{
if(i-j>0)
{
return i-j;
}
else
{
return j-i;
}
}
int main()
{ ll t;
cin>>t;
while(t--)
{ ll n,rank;
cin>>n;
ll arr[n+1]={0};
string name;
for(ll i=1;i<=n;i++)
{
cin>>name>>rank;
arr[rank]++;
}
ll actual_rank=1;
ll sum=0;
for(ll i=1;i<=n;i++)
{
while(arr[i])
{
sum+=abss(actual_rank,i);
arr[i]--;
actual_rank++;
}
}
cout<<sum<<endl;
}
return 0;
}