#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e12 + 7;
const long long MOD = 998244353;
long long dp[200005][2];
vector<long long> arr;
vector<pair<long long, long long> > segs;
long long solve(long long idx, long long j) {
  if (idx == (long long)arr.size()) {
    return 0;
  }
  if (dp[idx][j] != -1) {
    return dp[idx][j];
  }
  long long ans = INF;
  vector<pair<long long, long long> > ex;
  long long pos = lower_bound((segs).begin(), (segs).end(),
                              pair<long long, long long>(arr[idx - 1], 0)) -
                  segs.begin();
  while (pos < segs.size() && segs[pos].second <= arr[idx]) {
    ex.push_back(segs[pos]);
    pos++;
  }
  multiset<long long> s;
  s.insert(arr[idx]);
  for (auto x : ex) {
    s.insert(x.second);
  }
  long long cost2 = 0;
  for (long long i = 0; i < (long long)ex.size() + 1; i++) {
    auto it = s.begin();
    long long cost = arr[idx] - (*it);
    ans = min(ans, solve(idx + 1, 0) + cost + (2 - j) * cost2);
    ans = min(ans, solve(idx + 1, 1) + 2 * cost + (2 - j) * cost2);
    if (i != (long long)ex.size()) {
      cost2 = ex[i].first - arr[idx - 1];
      s.erase(s.find(ex[i].second));
    }
  }
  return dp[idx][j] = ans;
}
int main(void) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    long long n, m;
    cin >> n >> m;
    arr.clear();
    arr.push_back(-INF);
    for (long long i = 0; i < n; i++) {
      long long a;
      cin >> a;
      arr.push_back(a);
    }
    arr.push_back(INF);
    sort((arr).begin(), (arr).end());
    segs.clear();
    for (long long i = 0; i < m; i++) {
      long long l, r;
      cin >> l >> r;
      long long pos = lower_bound((arr).begin(), (arr).end(), l) - arr.begin();
      if (arr[pos] > r) {
        segs.push_back(pair<long long, long long>(l, r));
      }
    }
    sort((segs).begin(), (segs).end());
    for (long long i = 0; i < n + 5; i++) {
      for (long long j = 0; j < 2; j++) {
        dp[i][j] = -1;
      }
    }
    cout << solve(1, 0) << "\n";
  }
}
