#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int maxn = 11234567;
typedef struct Voter {
  int m, p;
  bool operator<(const struct Voter a) const { return m < a.m; }
} Voter;
Voter vt[212345];
long long dp[2][212345];
int pos[212345];
int main() {
  ios::sync_with_stdio(false);
  int t;
  int n;
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
      scanf("%d%d", &vt[i].m, &vt[i].p);
    }
    sort(vt + 1, vt + 1 + n);
    memset(pos, -1, sizeof(pos));
    int en = 0;
    for (int i = 1; i <= n; ++i) {
      while (en < n && en < vt[i].m) en++;
      if (en < n)
        pos[i] = en++;
      else
        pos[i] = pos[i - 1];
    }
    pos[0] = 0;
    int pre = 0;
    dp[pre][n] = 0;
    int best_pos = n - 1;
    for (int i = n; i > 0; --i) {
      pre ^= 1;
      memset(dp[pre], -1, sizeof(long long) * n);
      dp[pre][n] = dp[pre ^ 1][n] + vt[i].p;
      int st = pos[i - 1];
      int now_best = best_pos;
      for (int j = max(best_pos, st); j >= st; --j) {
        if (dp[pre ^ 1][j + 1] != -1)
          if (vt[i].m <= j) {
            dp[pre][j] = dp[pre ^ 1][j + 1];
          } else
            break;
        if (dp[pre ^ 1][j] != -1)
          if (dp[pre][j] == -1 || dp[pre][j] > dp[pre ^ 1][j] + vt[i].p)
            dp[pre][j] = dp[pre ^ 1][j] + vt[i].p;
        if (dp[pre][now_best] == -1 ||
            (dp[pre][now_best] > dp[pre][j] && dp[pre][j] != -1))
          now_best = j;
      }
      best_pos = now_best;
    }
    long long ans = -1;
    for (int i = n; i >= 0; --i)
      if (dp[pre][i] == -1)
        continue;
      else if (ans == -1 || ans > dp[pre][i])
        ans = dp[pre][i];
    printf("%lld\n", ans);
  }
  return 0;
}
