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;
  struct chash {
    static inline uint64_t splitmix64(uint64_t x) {
      x += 0x9e3779b97f4a7c15;
      x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
      x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
      return x ^ (x >> 31);
    }
    inline size_t operator()(uint64_t x) const {
      static const uint64_t FIXED_RANDOM =
          chrono::steady_clock::now().time_since_epoch().count();
      return splitmix64(x + FIXED_RANDOM);
    }
    inline size_t operator()(const pair<uint64_t, uint64_t> &v) const {
      chash int_hasher;
      return int_hasher(v.first) * 31 + int_hasher(v.second);
    }
  };
  const char nl = '\n';
  const long long inf = 1e9 + 10;
  const long long inf2 = 1e18 + 99LL;
  const long double inf3 = 1e17;
  const long long mod = 1e9 + 7, mod2 = 998244353;
  const long double eps = 1e-9;
  const bool local = false;
  const int logn = 21, maxn = 600005, maxm = 1, maxn2 = 3;
  int tn = 0;
  int t2[maxn * 2];
  void build2() {
    for (int i = tn - 1; i; i--) t2[i] = t2[i << 1] + t2[i << 1 | 1];
  }
  int query2(int l, int r) {
    int res = 0;
    for (l += tn, r += tn; l < r; l >>= 1, r >>= 1) {
      if (l & 1) res += t2[l++];
      if (r & 1) res += t2[--r];
    }
    return res;
  }
  void solve() {
    int n, m;
    cin >> n >> m;
    vector<long long> a(n + 1);
    a[0] = -inf2;
    for (int i = 0; i < n; i++) cin >> a[i + 1];
    a.push_back(inf2);
    n += 2;
    sort((a).begin(), (a).end());
    vector<pair<long long, long long>> tb(m);
    for (int i = 0; i < m; i++) cin >> tb[i].first >> tb[i].second;
    tb.emplace_back(inf2 + 1, inf2 + 2);
    tb.emplace_back(-inf2 + 1, -inf2 + 2);
    map<int, long long> tm;
    vector<long long> tm2;
    tn = 0;
    {
      set<long long> ts;
      for (long long x : a) ts.insert(x);
      for (auto p : tb) {
        ts.insert(p.first);
        ts.insert(p.second);
      }
      for (long long x : ts) {
        tm2.push_back(x);
        tm[x] = tn++;
      }
    }
    for (int i = 0; i < tn; i++) t2[i + tn] = 0;
    for (int i = 0; i < (int)((a).size()); i++) {
      a[i] = tm[a[i]];
      t2[a[i] + tn] = 1;
    }
    build2();
    vector<pair<int, int>> b;
    for (auto p : tb) {
      int l = p.first, r = p.second;
      l = tm[l];
      r = tm[r];
      if (query2(l, r + 1) == 0) {
        b.emplace_back(l, r);
      }
    }
    tb.clear();
    sort((b).begin(), (b).end());
    m = (int)((b).size());
    vector<long long> dp(m + 1, inf2);
    dp[0] = 0;
    vector<long long> c(m + 1);
    set<pair<int, int>> ts;
    for (int i = 0; i < m; i++) {
      ts.emplace(b[i].second, i);
    }
    for (int i = 0; i < m; i++) {
      c[i] = tm2[ts.begin()->first];
      ts.erase({b[i].second, i});
    }
    int j = 0;
    long long v1, v2;
    v1 = v2 = 1;
    for (int i = 0; i < m; i++) {
      bool change = false;
      while (j + 1 < n && a[j + 1] <= b[i].first) {
        j++;
        int tk = i;
        while (tk && b[tk - 1].first >= a[j - 1]) {
          tk--;
        }
        v1 = v2 = dp[i] + tm2[b[i].first] - tm2[a[j]];
        for (int k = tk; k < i; k++) {
          dp[i] = min(dp[i], dp[k] + tm2[a[j]] - c[k]);
        }
        for (int k = tk; k < i; k++) {
          long long le = tm2[b[i].first] - c[k];
          v1 = min(v1, dp[k] + le + tm2[a[j]] - c[k]);
          v2 = min(v2, dp[k] + le + tm2[b[i].first] - tm2[a[j]]);
        }
        change = true;
      }
      if (i && !change) {
        long long dx = tm2[b[i].first] - tm2[b[i - 1].first];
        v1 += dx;
        v2 += 2 * dx;
      }
      dp[i + 1] = min(v1, v2);
    }
    cout << dp[m] - 2 << nl;
  }
  int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    ;
    int T;
    cin >> T;
    while (T--) solve();
  }
  