#include <bits/stdc++.h>
using namespace std;
template <typename T>
using vector1D = vector<T>;
template <typename T>
using vector2D = vector<vector1D<T>>;
template <typename T>
using vector3D = vector<vector2D<T>>;
template <typename T>
using uset = unordered_set<T>;
template <typename K, typename V>
using umap = unordered_map<K, V>;
template <typename T>
ostream& operator<<(ostream& out, const vector<T>& v) {
  if (v.size() == 0) {
    return out << "[]";
  }
  out << '[' << v[0];
  for (int i = 1; i < v.size(); ++i) {
    out << ' ' << v[i];
  }
  return out << ']';
}
template <typename T>
ostream& operator<<(ostream& out, const vector<vector<T>>& m) {
  if (m.size() == 0) {
    return out << "[]";
  }
  for (const auto& row : m) {
    out << row << endl;
  }
  return out;
}
template <typename T>
ostream& operator<<(ostream& out, const deque<T>& d) {
  if (d.size() == 0) {
    return out << "[]";
  }
  out << '[' << d[0];
  for (int i = 1; i < d.size(); ++i) {
    out << ' ' << d[i];
  }
  return out << ']';
}
template <typename T>
ostream& operator<<(ostream& out, const set<T>& s) {
  if (s.size() == 0) {
    return out << "{}";
  }
  out << '{' << *s.begin();
  for (auto it = ++s.begin(); it != s.end(); ++it) {
    out << ' ' << *it;
  }
  return out << '}';
}
template <typename T>
ostream& operator<<(ostream& out, const multiset<T>& s) {
  return out << vector<T>(s.begin(), s.end());
}
template <typename T>
ostream& operator<<(ostream& out, const unordered_set<T>& s) {
  if (s.size() == 0) {
    return out << "{}";
  }
  vector<T> v(s.begin(), s.end());
  sort(v.begin(), v.end());
  out << '{' << v[0];
  for (int i = 1; i < v.size(); ++i) {
    out << ' ' << v[i];
  }
  return out << '}';
}
template <typename K, typename V>
ostream& operator<<(ostream& out, const map<K, V>& m) {
  if (m.size() == 0) {
    return out << "{}";
  }
  vector<K> keys;
  for (const auto& p : m) {
    keys.push_back(p.first);
  }
  out << "{" << keys[0] << ": " << m.at(keys[0]);
  for (int i = 1; i < keys.size(); ++i) {
    const auto& key = keys[i];
    out << ", " << key << ": " << m.at(key);
  }
  return out << '}';
}
template <typename K, typename V>
ostream& operator<<(ostream& out, const unordered_map<K, V>& m) {
  if (m.size() == 0) {
    return out << "{}";
  }
  vector<K> keys;
  for (const auto& p : m) {
    keys.push_back(p.first);
  }
  sort(keys.begin(), keys.end());
  out << "{" << keys[0] << ": " << m.at(keys[0]);
  for (int i = 1; i < keys.size(); ++i) {
    const auto& key = keys[i];
    out << ", " << key << ": " << m.at(key);
  }
  return out << '}';
}
template <typename F, typename S>
ostream& operator<<(ostream& out, const pair<F, S>& p) {
  return out << '(' << p.first << ", " << p.second << ')';
}
namespace std {
template <typename T, typename U>
struct hash<pair<T, U>> {
  size_t operator()(const pair<T, U>& p) const noexcept {
    return hash<T>{}(p.first) ^ (hash<U>{}(p.second) << 1);
  }
};
}  // namespace std
void solution();
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  solution();
  return 0;
}
long long MOD = pow(10, 9) + 7;
long long T, N, M, Q, L, R, X, Y;
void solution() {
  cin >> T;
  while (T-- > 0) {
    cin >> N >> Q;
    vector<long long> a(N);
    for (long long n = 0; n < N; n++) cin >> a[n];
    long long ans = a[0];
    for (long long n = 1; n < N; n++) ans += max((long long)0, a[n] - a[n - 1]);
    cout << ans << endl;
    for (long long q = 0; q < Q; q++) {
      cin >> L >> R;
      L--;
      R--;
      long long temp = a[R];
      ans -= a[0];
      if (R > 0) ans -= max((long long)0, a[R] - a[R - 1]);
      if (R < N - 1) ans -= max((long long)0, a[R + 1] - a[R]);
      a[R] = a[L];
      if (R > 0) ans += max((long long)0, a[R] - a[R - 1]);
      if (R < N - 1) ans += max((long long)0, a[R + 1] - a[R]);
      if (L > 0) ans -= max((long long)0, a[L] - a[L - 1]);
      if (L < N - 1) ans -= max((long long)0, a[L + 1] - a[L]);
      a[L] = temp;
      if (L > 0) ans += max((long long)0, a[L] - a[L - 1]);
      if (L < N - 1) ans += max((long long)0, a[L + 1] - a[L]);
      ans += a[0];
      cout << ans << endl;
    }
  }
}
