#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
ostream& operator<<(ostream& i, pair<T1, T2> j) {
  return i << j.first << ' ' << j.second;
}
template <typename T>
ostream& operator<<(ostream& i, vector<T> j) {
  i << '{' << j.size() << ':';
  for (T ii : j) i << ' ' << ii;
  return i << '}';
}
const int inf = 0x3f3f3f3f;
const long long mod = 1e9 + 7, INF = 0x3f3f3f3f3f3f3f3f;
signed main() {
  ios::sync_with_stdio(0), cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    vector<pair<long long, long long> > v;
    for (int i = 0, x; i < n; ++i) cin >> x, v.emplace_back(x, -inf);
    for (int j = 0, x, y; j < m; ++j) cin >> x >> y, v.emplace_back(x, y);
    sort((v).begin(), (v).end());
    vector<pair<long long, long long> > _v;
    _v.push_back(v[0]);
    for (int i = 1; i < n + m; ++i) {
      if (v[i].second == -inf) {
        while (!_v.empty() && _v.back().second != -inf &&
               v[i].first <= _v.back().second)
          _v.pop_back();
        _v.push_back(v[i]);
      } else if (_v.back().second == -inf) {
        if (v[i].first != _v.back().first) _v.push_back(v[i]);
      } else {
        while (!_v.empty() && _v.back().second != -inf &&
               v[i].second <= _v.back().second)
          _v.pop_back();
        _v.push_back(v[i]);
      }
    }
    v.swap(_v);
    auto nxt = [&](int i) {
      return (i + 1 == v.size()         ? 0
              : v[i + 1].second == -inf ? v[i + 1].first
                                        : v[i + 1].second);
    };
    long long tmp = nxt(-1), best[2]{-tmp * 2, -tmp}, ans;
    int p = 0;
    while (v[p].second != -inf) ++p;
    while (p < v.size()) {
      int r = p + 1;
      while (r < v.size() && v[r].second != -inf) ++r;
      long long _best[2]{INF, INF};
      for (int i = p; i < r; ++i) {
        long long res = min(best[0] + v[i].first + v[p].first,
                            best[1] + v[i].first * 2 - v[p].first);
        ans = res;
        _best[0] = min(_best[0], res - nxt(i) * 2);
        _best[1] = min(_best[1], res - nxt(i));
      }
      best[0] = _best[0], best[1] = _best[1];
      p = r;
    }
    cout << ans << '\n';
  }
}
