#include <bits/stdc++.h>
using namespace std;
bool compare(int x, int y) { return x > y; }
int main() {
  int t, n, x;
  cin >> t;
  while (t--) {
    cin >> n;
    vector<int> arr;
    for (int i = 0; i < n; i++) {
      cin >> x;
      arr.push_back(x);
    }
    sort(arr.begin(), arr.end(), compare);
    int a, b;
    a = arr[0];
    b = arr[1];
    x = 0;
    for (int i = 2; i < n && i - 2 < b - 1 && arr[i] > 0; i++) {
      x++;
    }
    cout << x << "\n";
  }
}
