#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pi = pair<int, int>;
using vi = vector<int>;
const int nax = 200 * 1000 + 10;
const ll INF = 1e18;
int t, n, m;
pi seg[nax];
ll pt[nax];
vi ind;
vi occ[nax];
ll dp[nax][2];
void test_case() {
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    cin >> pt[i];
  }
  for (int i = 1; i <= m; ++i) {
    cin >> seg[i].first >> seg[i].second;
  }
  for (int i = 0; i <= n; ++i) {
    occ[i].clear();
  }
  ind.clear();
  sort(pt + 1, pt + n + 1);
  sort(seg + 1, seg + m + 1);
  for (int i = 1; i <= m; ++i) {
    while ((int)ind.size() > 0 && seg[ind.back()].second >= seg[i].second) {
      ind.pop_back();
    }
    ind.push_back(i);
  }
  int ptr = 0;
  m = (int)ind.size();
  for (int i = 1; i <= n; ++i) {
    while (ptr < m && seg[ind[ptr]].second < pt[i]) {
      occ[i - 1].push_back(ind[ptr]);
      ptr++;
    }
    while (ptr < m && seg[ind[ptr]].first <= pt[i] &&
           pt[i] <= seg[ind[ptr]].second) {
      ptr++;
    }
  }
  for (int i = ptr; i < m; ++i) {
    occ[n].push_back(ind[i]);
  }
  pt[0] = -INF;
  pt[n + 1] = INF;
  for (int i = 1; i <= n + 1; ++i) {
    dp[i][0] = dp[i - 1][0] + ((int)occ[i - 1].size() > 0
                                   ? abs(pt[i] - seg[occ[i - 1][0]].second)
                                   : 0);
    dp[i][1] = dp[i - 1][0] + 2 * ((int)occ[i - 1].size() > 0
                                       ? abs(pt[i] - seg[occ[i - 1][0]].second)
                                       : 0);
    int nxt = 0;
    for (auto id : occ[i - 1]) {
      ll tmp = min(dp[i - 1][1] + seg[id].first - pt[i - 1],
                   dp[i - 1][0] + 2 * (seg[id].first - pt[i - 1]));
      ll d = (nxt == (int)occ[i - 1].size() - 1
                  ? 0
                  : pt[i] - seg[occ[i - 1][nxt + 1]].second);
      dp[i][0] = min(dp[i][0], tmp + d);
      dp[i][1] = min(dp[i][1], tmp + 2 * d);
      nxt++;
    }
  }
  cout << min(dp[n + 1][0], dp[n + 1][1]) << "\n";
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> t;
  while (t--) {
    test_case();
  }
}
