#include <bits/stdc++.h>
using namespace std;
int main() {
  int t, i, n, j, k, a[100000], temp, x, b, c;
  cin >> t;
  for (i = 0; i < t; i++) {
    cin >> n;
    for (j = 0; j < n; j++) {
      cin >> a[j];
    }
    for (j = 0; j < 2; j++) {
      for (k = j + 1; k < n; k++) {
        if (a[j] < a[k]) {
          temp = a[j];
          a[j] = a[k];
          a[k] = temp;
        }
      }
    }
    x = a[1];
    b = x - 1;
    c = n - 2;
    if (b < c) {
      cout << b << endl;
    } else {
      cout << c << endl;
    }
  }
  return 0;
}
