#include <bits/stdc++.h>
using namespace std;
const int dkm = 1e5 + 5;
long long j, big, a[dkm], n, f[dkm], dem, chachacha, t;
int main() {
  cin >> t;
  for (int t2 = 1; t2 <= t; t2++) {
    chachacha = 0;
    memset(f, 0, sizeof f);
    big = -1e9;
    cin >> n;
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
      big = max(big, a[i]);
      f[a[i]]++;
    }
    f[big]--;
    if (f[big] > 0) {
      j = big;
      f[j]--;
    } else {
      for (int i = big - 1; i >= 1; i--)
        if (f[i] > 0) {
          j = i;
          f[j]--;
          break;
        }
    }
    for (int i = j; i >= 1; i--)
      if (f[i] > 0) chachacha = min(chachacha + f[i], j - 1);
    cout << chachacha << endl;
  }
}
