#include <bits/stdc++.h>
using namespace std;
int secondmax(int arr[], int n) {
  int first_max = arr[1];
  int second_max = arr[2];
  if (second_max > first_max) {
    first_max = arr[2];
    second_max = arr[1];
  }
  for (int i = 1; i <= n; i++) {
    if (arr[i] > first_max) {
      second_max = first_max;
      first_max = arr[i];
    } else if (arr[i] > second_max && arr[i] < first_max) {
      second_max = arr[i];
    }
  }
  return second_max;
}
int get(int arr[], int n, int maximum) {
  int counting = 0;
  for (int i = 1; i <= n; i++) {
    if (arr[i] == maximum) counting++;
  }
  return counting;
}
int main() {
  int t;
  cin >> t;
  while (t > 0) {
    int n;
    cin >> n;
    int arr[n + 1];
    for (int i = 1; i <= n; i++) {
      cin >> arr[i];
    }
    int maximum = arr[1];
    for (int i = 1; i <= n; i++) {
      if (arr[i] > maximum) {
        maximum = arr[i];
      }
    }
    int x = get(arr, n, maximum);
    if (x > 1) {
      int y = maximum - 1;
      cout << min(n - 2, y) << endl;
    } else {
      int z = secondmax(arr, n);
      int l = z - 1;
      cout << min(n - 2, l) << endl;
    }
    t--;
  }
}
