#include <bits/stdc++.h>   const int N = 200010;   const long long INF = 1ll << 60;   const int inf = 0x3f3f3f3f;   class SegmentTree {    public:     SegmentTree() {}     void Init(int n, int v[]) {       n_ = n;       Build(1, n, 1, v);     }     void ChangeSingle(int l, int r, int rt, int id, int x) {       if (l == r) {         min_[rt] = std::pair<int, int>(x, l);         return;       }       int m = (l + r) >> 1;       if (id <= m) {         ChangeSingle(l, m, rt << 1, id, x);       } else {         ChangeSingle(m + 1, r, rt << 1 | 1, id, x);       }       Up(rt);     }     std::pair<int, int> MinInterval(int l, int r, int rt, int L, int R) {       if (L > R) {         return std::pair<int, int>(inf, inf);       }       if (L <= l && r <= R) {         return min_[rt];       }       int m = (l + r) >> 1;       std::pair<int, int> ans(inf, inf);       if (L <= m) {         ans = std::min(ans, MinInterval(l, m, rt << 1, L, R));       }       if (R > m) {         ans = std::min(ans, MinInterval(m + 1, r, rt << 1 | 1, L, R));       }       return ans;     }       private:     void Build(int l, int r, int rt, int v[]) {       if (l == r) {         min_[rt].first = v[l];         min_[rt].second = l;         return;       }       int m = (l + r) >> 1;       Build(l, m, rt << 1, v);       Build(m + 1, r, rt << 1 | 1, v);       Up(rt);     }     void Up(int rt) { min_[rt] = std::min(min_[rt << 1], min_[rt << 1 | 1]); }     int n_;     std::pair<int, int> min_[N << 2 | 1];   } seg;   int t;   int n, m;   int a[N];   int b[N];   std::multiset<int> se[N];   int pre[N];   int v[N];   int main() {     scanf( %d , &t);     while (t--) {       scanf( %d , &n);       for (int i = 0; i < n; ++i) {         se[i].clear();         pre[i] = 0;       }       for (int i = 1; i <= n; ++i) {         scanf( %d%d , &a[i], &b[i]);         se[a[i]].insert(b[i]);       }       if (n == 1) {         printf( 0 n );         continue;       }       for (int i = 0; i < n; ++i) {         pre[i] = (i == 0 ? se[i].size() : pre[i - 1] + se[i].size());         v[i] = (se[i].size() ? (*se[i].begin()) : inf);       }       seg.Init(n - 1, v);       int id_st = n - 1;       while (id_st >= 0 && pre[id_st] > id_st) {         --id_st;       }       int num = 0;       int num_last = 0;       int num2 = 0;       long long ans = 0ll;       while (num < n) {         int inc = 0;         for (int i = num_last; i <= num; ++i) {           if (se[i].size()) {             inc += se[i].size();             se[i].clear();           }         }         num_last = num + 1;         if (inc) {           num += inc;           continue;         } else {           while (id_st >= num + 1 && pre[id_st] + num2 > id_st) {             --id_st;           }           std::pair<int, int> x = seg.MinInterval(1, n - 1, 1, id_st + 1, n - 1);           ans += x.first;           int id = x.second;           se[id].erase(se[id].begin());           if (se[id].size()) {             seg.ChangeSingle(1, n - 1, 1, id, *se[id].begin());           } else {             seg.ChangeSingle(1, n - 1, 1, id, inf);           }           ++num2;           ++num;         }       }       printf( %lld n , ans);     }     return 0;   }