description: There are n points and m segments on the coordinate line. The initial coordinate of the i-th point is a_i. The endpoints of the j-th segment are l_j and r_j — left and right endpoints, respectively.
  
  You can move the points. In one move you can move any point from its current coordinate x to the coordinate x - 1 or the coordinate x + 1. The cost of this move is 1.
  
  You should move the points in such a way that each segment is visited by at least one point. A point visits the segment [l, r] if there is a moment when its coordinate was on the segment [l, r] (including endpoints).
  
  You should find the minimal possible total cost of all moves such that all segments are visited.
  
  Input
  
  The input consists of multiple test cases. The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases. Description of the test cases follows.
  
  The first line of each test case contains two integers n and m (1 ≤ n, m ≤ 2 ⋅ 10^5) — the number of points and segments respectively.
  
  The next line contains n distinct integers a_1, a_2, …, a_n (-10^9 ≤ a_i ≤ 10^9) — the initial coordinates of the points.
  
  Each of the next m lines contains two integers l_j, r_j (-10^9 ≤ l_j ≤ r_j ≤ 10^9) — the left and the right endpoints of the j-th segment.
  
  It's guaranteed that the sum of n and the sum of m over all test cases does not exceed 2 ⋅ 10^5.
  
  Output
  
  For each test case print a single integer — the minimal total cost of all moves such that all segments are visited.
  
  Example
  
  Input
  
  
  2
  4 11
  2 6 14 18
  0 3
  4 5
  11 15
  3 5
  10 13
  16 16
  1 4
  8 12
  17 19
  7 13
  14 19
  4 12
  -9 -16 12 3
  -20 -18
  -14 -13
  -10 -7
  -3 -1
  0 4
  6 11
  7 9
  8 10
  13 15
  14 18
  16 17
  18 19
  
  
  Output
  
  
  5
  22
  
  Note
  
  In the first test case the points can be moved as follows:
  
    * Move the second point from the coordinate 6 to the coordinate 5. 
    * Move the third point from the coordinate 14 to the coordinate 13. 
    * Move the fourth point from the coordinate 18 to the coordinate 17. 
    * Move the third point from the coordinate 13 to the coordinate 12. 
    * Move the fourth point from the coordinate 17 to the coordinate 16. 
  
  
  
  The total cost of moves is 5. It is easy to see, that all segments are visited by these movements. For example, the tenth segment ([7, 13]) is visited after the second move by the third point.
  
  Here is the image that describes the first test case:
  
  <image>
solution: #include <bits/stdc++.h>
  using namespace std;
  const long long maxN = 2e5 + 10;
  long long n, m, a[maxN], ptr;
  pair<long long, long long> baz[maxN];
  long long pt_min[maxN];
  const long long oo = 2LL * 1000 * 1000 * 10000;
  vector<pair<long long, long long> > get_pairs(long long l, long long r) {
    vector<pair<long long, long long> > vc;
    while (ptr < m and baz[ptr].first <= l) ptr++;
    while (ptr < m and baz[ptr].first < r) {
      if (baz[ptr].second < r) vc.push_back(baz[ptr]);
      ptr++;
    }
    if (vc.size() == 0) {
      vector<pair<long long, long long> > res;
      res.push_back({0, 0});
      return res;
    }
    long long len = vc.size();
    pt_min[len] = r;
    for (long long i = len - 1; i >= 0; i--)
      pt_min[i] = min(pt_min[i + 1], vc[i].second);
    vector<pair<long long, long long> > res;
    res.push_back({0, r - pt_min[0]});
    for (long long i = 0; i < len; i++)
      res.push_back({vc[i].first - l, r - pt_min[i + 1]});
    return res;
  }
  vector<long long> get_pref(vector<long long> v) {
    vector<long long> res;
    res.push_back(oo);
    for (auto x : v) res.push_back(min(res.back(), x));
    return res;
  }
  vector<long long> get_suff(vector<long long> v) {
    vector<long long> res;
    res.push_back(oo);
    reverse(v.begin(), v.end());
    for (auto x : v) res.push_back(min(res.back(), x));
    reverse(res.begin(), res.end());
    return res;
  }
  void solve() {
    cin >> n >> m;
    for (long long i = 0; i < n; i++) cin >> a[i];
    a[n] = -oo;
    a[n + 1] = oo;
    sort(a, a + n + 2);
    for (long long i = 0; i < m; i++) cin >> baz[i].first >> baz[i].second;
    sort(baz, baz + m);
    ptr = 0;
    vector<vector<pair<long long, long long> > > is;
    for (long long i = 0; i + 1 < n + 2; i++) {
      is.push_back(get_pairs(a[i], a[i + 1]));
    }
    vector<vector<long long> > dp, pd;
    vector<vector<long long> > pref, suff;
    for (long long i = 0; i <= n; i++) {
      vector<long long> curr, rucc;
      long long len = is[i].size();
      for (long long j = 0; j < len; j++) {
        long long vl = is[i][j].first;
        if (i) {
          long long lo = -1, hi = is[i - 1].size();
          while (hi - lo > 1) {
            long long mid = (lo + hi) / 2;
            if (is[i - 1][mid].second >= vl)
              lo = mid;
            else
              hi = mid;
          }
          vl += min(pref[i - 1][hi] + is[i][j].first, suff[i - 1][hi]);
          if (i != n) vl += is[i][j].second;
          curr.push_back(vl);
          if (i != n) vl += is[i][j].second;
          rucc.push_back(vl);
        } else {
          curr.push_back(is[i][j].first + is[i][j].second);
          rucc.push_back(is[i][j].first + 2LL * is[i][j].second);
        }
      }
      dp.push_back(curr);
      pd.push_back(rucc);
      pref.push_back(get_pref(curr));
      suff.push_back(get_suff(rucc));
      if (i == n) {
        cout << curr.back() << '\n';
        return;
      }
    }
  }
  int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    long long t;
    cin >> t;
    while (t--) {
      solve();
    }
  }
  