#include <bits/stdc++.h>
using namespace std;
void solve();
int main() {
  srand(time(0));
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}
const long long N = 2e5 + 100, M = 6e5 + 100, Q = 5e5 + 100, K = 40, INF = 1e18,
                MOD = 998244353;
long long t, n, m, dp[N][2];
vector<pair<long long, long long>> b;
vector<long long> nx[N], a;
bool del[N];
long long f(long long indp, long long inds) {
  return min(abs(a[indp] - b[inds].first), abs(a[indp] - b[inds].second));
}
void solve() {
  cin >> t;
  while (t--) {
    cin >> n >> m;
    b.clear();
    for (long long i = 0; i <= n + 1; i++) {
      nx[i].clear();
    }
    a.resize(n);
    for (long long i = 0; i < n; i++) {
      cin >> a[i];
    }
    a.push_back(-INF);
    a.push_back(INF);
    sort(a.begin(), a.end());
    for (long long i = 0; i < m; i++) {
      long long l, r;
      ;
      cin >> l >> r;
      long long ll = -1, rr = n;
      while (ll + 1 < rr) {
        long long md = (rr + ll) / 2;
        if (a[md] < l) {
          ll = md;
        } else {
          rr = md;
        }
      }
      if (rr == -1 || a[rr] < l || a[rr] > r) {
        b.emplace_back(l, r);
      }
    }
    m = b.size();
    sort(b.begin(), b.end());
    fill(del, del + m, false);
    long long mnr = INF;
    for (long long i = m - 1; i >= 0; i--) {
      if (mnr <= b[i].second) {
        del[i] = true;
      }
      mnr = min(mnr, b[i].second);
    }
    vector<pair<long long, long long>> e;
    for (long long i = 0; i < m; i++) {
      if (!del[i]) {
        e.push_back(b[i]);
      }
    }
    b = e;
    m = b.size();
    long long r = 0;
    for (long long i = 0; i < m; i++) {
      while (a[r + 1] <= b[i].first) r++;
      nx[r].push_back(i);
    }
    for (long long i = 1; i <= n + 2; i++) {
      dp[i][0] = INF;
      dp[i][1] = INF;
      if (nx[i - 1].empty()) {
        dp[i][0] = min(dp[i - 1][0], dp[i - 1][1]);
      } else {
        dp[i][0] = min(dp[i - 1][0] + f(i - 1, nx[i - 1].back()),
                       dp[i - 1][1] + 2 * f(i - 1, nx[i - 1].back()));
        dp[i][0] = min(dp[i][0], min(dp[i - 1][0] + 2 * f(i, nx[i - 1][0]),
                                     dp[i - 1][1] + 2 * f(i, nx[i - 1][0])));
        dp[i][1] = min(dp[i - 1][0] + f(i, nx[i - 1][0]),
                       dp[i - 1][1] + f(i, nx[i - 1][0]));
      }
      for (long long j = 0; j < (long long)nx[i - 1].size() - 1; j++) {
        if (j + 1 < nx[i - 1].size()) {
          dp[i][1] =
              min(dp[i][1], min(dp[i - 1][0] + f(i - 1, nx[i - 1][j]) +
                                    f(i, nx[i - 1][j + 1]),
                                dp[i - 1][1] + 2 * f(i - 1, nx[i - 1][j]) +
                                    f(i, nx[i - 1][j + 1])));
          dp[i][0] =
              min(dp[i][0], min(dp[i - 1][0] + f(i - 1, nx[i - 1][j]) +
                                    2 * f(i, nx[i - 1][j + 1]),
                                dp[i - 1][1] + 2 * f(i - 1, nx[i - 1][j]) +
                                    2 * f(i, nx[i - 1][j + 1])));
        }
      }
      dp[i][1] = min(dp[i][1], dp[i][0]);
    }
    cout << min(dp[n + 1][0], dp[n + 1][1]) << '\n';
  }
}
