#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353, maxn = 200030;
int aabs(int x) {
  if (x < 0) return -x;
  return x;
}
long long pow(long long x, long long a) {
  long long now = 1;
  while (a) {
    if (a % 2) now = now * x % mod;
    x = x * x % mod;
    a >>= 1;
  }
  return now;
}
int t;
void solve() {
  int n, m;
  scanf("%d%d", &n, &m);
  int a[n];
  for (int i = 0; i < n; i++) scanf("%d", &a[i]);
  sort(a, a + n);
  vector<pair<int, int> > seg[n + 1];
  for (int i = 1; i <= m; i++) {
    int l, r;
    scanf("%d%d", &l, &r);
    int xh = lower_bound(a, a + n, l) - a;
    if (xh < n && a[xh] <= r) continue;
    seg[xh].emplace_back(l, r);
  }
  for (int i = 0; i <= n; i++) {
    sort(seg[i].begin(), seg[i].end());
    vector<pair<int, int> > temp;
    for (auto [l, r] : seg[i]) {
      if (!temp.empty() && temp.back().first == l) continue;
      while (!temp.empty() && temp.back().second >= r) temp.pop_back();
      temp.emplace_back(l, r);
    }
    seg[i] = temp;
  }
  for (int i = 0; i < n; i++) {
    seg[i].emplace_back(a[i], a[i]);
    seg[i + 1].insert(seg[i + 1].begin(), make_pair(a[i], a[i]));
  }
  long long dp[2];
  dp[0] = a[0] - seg[0][0].second;
  dp[1] = dp[0] * 2;
  for (int i = 1; i < n; i++) {
    long long temp[2];
    temp[0] = temp[1] = 1e18;
    for (int j = 0; j < seg[i].size() - 1; j++) {
      for (int x = 0; x < 2; x++)
        for (int y = 0; y < 2; y++)
          temp[x] =
              min(temp[x], dp[y] + (2LL - y) * (seg[i][j].first - a[i - 1]) +
                               (1LL + x) * (a[i] - seg[i][j + 1].second));
    }
    dp[0] = temp[0];
    dp[1] = temp[1];
  }
  long long ans = 1e18;
  for (int i = 0; i < 2; i++)
    ans = min(ans, dp[i] + (seg[n].back().first - a[n - 1]) * (2LL - i));
  printf("%lld\n", ans);
  return;
}
int main() {
  scanf("%d", &t);
  int a[t];
  while (t--) {
    solve();
  }
  return 0;
}
