#include <bits/stdc++.h>
using namespace std;
vector<int> bubbleSteps(vector<int> planks, int steps, int np) {
  for (int k = 0; k < steps; k++) {
    for (int kk = 0; kk < (np - k - 1); kk++) {
      if (planks[kk] > planks[kk + 1]) {
        int tmp = planks[kk];
        planks[kk] = planks[kk + 1];
        planks[kk + 1] = tmp;
      }
    }
  }
  return planks;
}
int main() {
  int times;
  cin >> times;
  for (int i = 0; i < times; i++) {
    int np;
    cin >> np;
    vector<int> planks;
    for (int i = 0; i < np; i++) {
      int x;
      cin >> x;
      planks.push_back(x);
    }
    planks = bubbleSteps(planks, 2, np);
    int count = 0;
    for (int i = 0; i < planks.size() - 2; i++) {
      if (count < planks[planks.size() - 2] - 1) count++;
    }
    cout << count << endl;
  }
}
