#include <bits/stdc++.h>
using namespace std;
class DinicMaxFlow {
 private:
  static const long long INF = 999999999;
  static const long long NOTHING = -999999999;
  unordered_set<long long> nodeList;
  unordered_map<long long, unordered_map<long long, long long>> fedge;

 public:
  void addEdge(long long u, long long v, long long w) {
    nodeList.insert(u);
    nodeList.insert(v);
    fedge[u][v] += w;
  }
  void addInfiniteEdge(long long u, long long v) {
    nodeList.insert(u);
    nodeList.insert(v);
    fedge[u][v] = INF;
  }

 private:
  bool buildLevelGraph(
      long long s, long long t, unordered_map<long long, long long> &lvl,
      unordered_map<long long, queue<long long>> &eligibleEdge) {
    lvl.clear();
    eligibleEdge.clear();
    lvl[s] = 0;
    queue<long long> waitList;
    waitList.push(s);
    while (!waitList.empty()) {
      long long u = waitList.front();
      waitList.pop();
      for (unordered_map<long long, long long>::iterator it = fedge[u].begin();
           it != fedge[u].end(); it++) {
        long long v = it->first;
        long long w = it->second;
        if (w <= 0) continue;
        if (lvl.find(v) != lvl.end()) {
          if (lvl[v] == lvl[u] + 1) eligibleEdge[u].push(v);
        } else {
          waitList.push(v);
          lvl[v] = lvl[u] + 1;
          eligibleEdge[u].push(v);
        }
      }
    }
    if (lvl.find(t) == lvl.end()) return false;
    return true;
  }
  long long firstChoiceFinder(
      long long s, long long t, unordered_map<long long, long long> &lvl,
      unordered_map<long long, queue<long long>> &eligibleEdge,
      unordered_map<long long, bool> &isOut) {
    long long bottleneck = INF;
    vector<long long> pathDetail;
    pathDetail.push_back(s);
    long long cur = s;
    while (true) {
      while (!eligibleEdge[cur].empty()) {
        long long v = eligibleEdge[cur].front();
        if (isOut[v]) {
          eligibleEdge[cur].pop();
          continue;
        }
        if (fedge[cur][v] <= 0) {
          eligibleEdge[cur].pop();
          continue;
        }
        break;
      }
      if (eligibleEdge[cur].empty()) break;
      long long z = eligibleEdge[cur].front();
      bottleneck = min(bottleneck, fedge[cur][z]);
      cur = z;
      pathDetail.push_back(cur);
    }
    if (cur != t) {
      isOut[cur] = true;
      return 0;
    }
    for (long long i = 0; i < pathDetail.size() - 1; i++) {
      long long u = pathDetail[i];
      long long v = pathDetail[i + 1];
      fedge[u][v] -= bottleneck;
      fedge[v][u] += bottleneck;
      if (fedge[u][v] <= 0) eligibleEdge[u].pop();
    }
    return bottleneck;
  }
  long long pushEverything(
      long long s, long long t, unordered_map<long long, long long> &lvl,
      unordered_map<long long, queue<long long>> &eligibleEdge) {
    unordered_map<long long, bool> isOut;
    long long answer = 0;
    while (!eligibleEdge[s].empty()) {
      answer += firstChoiceFinder(s, t, lvl, eligibleEdge, isOut);
    }
    return answer;
  }

 public:
  long long getMaxFlow(long long s, long long t) {
    long long answer = 0;
    while (true) {
      unordered_map<long long, long long> lvl;
      unordered_map<long long, queue<long long>> eligibleEdge;
      bool possible = buildLevelGraph(s, t, lvl, eligibleEdge);
      if (!possible) return answer;
      long long thisRound = pushEverything(s, t, lvl, eligibleEdge);
      answer += thisRound;
    }
  }
};
long long buyElectionBF(int n, vector<pair<long long, long long>> &people) {
  const int DUMMY_SOURCE = -1;
  const int DUMMY_SINK = -2;
  long long TOTAL_COST = 0;
  DinicMaxFlow helper;
  for (pair<long long, long long> p : people) {
    TOTAL_COST += p.second;
    for (int i = p.first; i < n; i++) helper.addEdge(DUMMY_SOURCE, i, p.second);
  }
  for (int i = 0; i < n; i++) helper.addEdge(i, DUMMY_SINK, 1);
  long long SAVING = helper.getMaxFlow(DUMMY_SOURCE, DUMMY_SINK);
  return TOTAL_COST - SAVING;
}
long long buyElection(int n, vector<pair<long long, long long>> people) {
  priority_queue<long long> bank;
  sort(people.begin(), people.end());
  queue<pair<long long, long long>> waitlist;
  for (pair<long long, long long> p : people) waitlist.push(p);
  long long saving = 0;
  long long totalCost = 0;
  for (pair<long long, long long> p : people) totalCost += p.second;
  for (int i = 0; i < n; i++) {
    while (!waitlist.empty() && waitlist.front().first == i) {
      bank.push(waitlist.front().second);
      waitlist.pop();
    }
    if (!bank.empty()) {
      saving += bank.top();
      bank.pop();
    }
  }
  return totalCost - saving;
}
int main() {
  int tc;
  scanf("%d", &tc);
  while (tc--) {
    int n;
    scanf("%d", &n);
    vector<pair<long long, long long>> people(n);
    for (int i = 0; i < n; i++)
      scanf("%lld%lld", &people[i].first, &people[i].second);
    long long ans = buyElection(n, people);
    printf("%lld\n", ans);
  }
  return 0;
}
