// Просто решаю.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
//KiruxaLight
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
#define _USE_MATH_DEFINES
#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <algorithm>
#include <utility>
#include <cmath>
#include <iomanip>
#include <stack>
#include <deque>
#include <queue>
#include <cstdio>
#include <unordered_map>
#include <unordered_set>
#include <numeric>
#include <cassert>
#include <bitset>
#include <ext/rope>
using namespace std;
using namespace __gnu_cxx;
#define int long long
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
const int INF = 1e9 + 123, MAXN = 3e5, MEGAINF = 1e18 + 228;
template <class T>
inline istream& operator >> (istream& in, vector <T>& a)
{
  for (auto& i : a)
    in >> i;
  return in;
}
template <class T>
inline ostream& operator << (ostream& out, vector <T>& a)
{
  for (auto i : a)
    out << i << " ";
  return out;
}
template <class T, class U>
inline istream& operator >> (istream& in, vector <pair <T, U>>& a)
{
  for (auto& i : a)
    in >> i.first >> i.second;
  return in;
}
template <class T, class U>
inline ostream& operator << (ostream& out, vector <pair <T, U>>& a)
{
  for (auto& i : a)
    out << i.first << " " << i.second << "\n";
  return out;
}
struct node
{
  int dp[2][2];
  node()
  {
    for (int i = 0; i < 2; ++i)
      for (int j = 0; j < 2; ++j)
        dp[i][j] = -MEGAINF;
  }
};
node operator + (node& a, node& b)
{
  node cur;
  for (int i = 0; i < 2; ++i)
    for (int j = 0; j < 2; ++j)
    {
      cur.dp[i][j] = max(a.dp[i][j], b.dp[i][j]);
      for (int k = 0; k < 2; ++k)
        cur.dp[i][j] = max(cur.dp[i][j], a.dp[i][k] + b.dp[k ^ 1][j]);
    }
  return cur;
}
struct segment_tree
{
  vector <node> t;
  int n;
  segment_tree()
  {

  }
  segment_tree(int N)
  {
    t.resize(N * 4);
    n = N;
  }
  node make(int val)
  {
    node res;
    res.dp[0][0] = -val;
    res.dp[1][1] = val;
    res.dp[0][1] = -MEGAINF;
    res.dp[1][0] = -MEGAINF;
    return res;
  }
  void build(vector <int>& a, int l, int r, int v = 1)
  {
    if (l == r)
    {
      t[v] = make(a[l]);
      return;
    }
    int mid = l + r >> 1;
    build(a, l, mid, v * 2);
    build(a, mid + 1, r, v * 2 + 1);
    t[v] = t[v * 2] + t[v * 2 + 1];
    /*t[v].dp[0][0] = max({t[v * 2].dp[0][0], t[v * 2 + 1].dp[0][0], t[v * 2].dp[0][1] + t[v * 2 + 1].dp[0][0], t[v * 2].dp[0][0] + t[v * 2 + 1].dp[1][0]});
    t[v].dp[0][1] =
    t[v].dp[1][0] =
    t[v].dp[1][1] =*/
  }
  void update(int pos, int val, int tl, int tr, int v = 1)
  {
    if (tl > pos || tr < pos)
      return;
    if (tl == pos && tr == pos)
    {
      t[v] = make(val);
      return;
    }
    int mid = tl + tr >> 1;
    update(pos, val, tl, mid, v * 2);
    update(pos, val, mid + 1, tr, v * 2 + 1);
    t[v] = t[v * 2] + t[v * 2 + 1];
  }
  void update(int pos, int val)
  {
    update(pos, val, 0, n - 1);
  }
};
//segment_tree tree(MAXN);
signed main()
{
  setlocale(LC_ALL, "rus");

  /*freopen("treemaster.in", "r", stdin);
  freopen("treemaster.out", "w", stdout);*/

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int t;
  cin >> t;
  while (t--)
  {
    int n, q;
    cin >> n >> q;
    vector <int> a(n);
    cin >> a;
    segment_tree tree(n);
    tree.build(a, 0, n - 1);
    node cur = tree.t[1];
    int ans = -MEGAINF;
    for (int i = 0; i < 2; ++i)
      for (int j = 0; j < 2; ++j)
        ans = max(ans, cur.dp[i][j]);
    cout << ans << "\n";
    while (q--)
    {
      int l, r;
      cin >> l >> r;
      --l, --r;
      tree.update(l, a[r]);
      tree.update(r, a[l]);
      swap(a[l], a[r]);
      ans = -MEGAINF;
      cur = tree.t[1];
      for (int i = 0; i < 2; ++i)
        for (int j = 0; j < 2; ++j)
          ans = max(ans, cur.dp[i][j]);
      cout << ans << "\n";
    }
  }
}
/*
1
50 0
1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10
*/
// Запуск программыd: CTRL+F5 или меню "Отладка" > "Запуск без отладки"
// Отладка программы: F5 или меню "Отладка" > "Запустить отладку"

// Советы по началу работы
//   1. В окне обозревателя решений можно добавлять файлы и управлять ими.
//   2. В окне Team Explorer можно подключиться к системе управления версиями.
//   3. В окне "Выходные данные" можно просматривать выходные данные сборки и другие сообщения.
//   4. В окне "Список ошибок" можно просматривать ошибки.
//   5. Последовательно выберите пункты меню "Проект" > "Добавить новый элемент", чтобы создать файлы кода, или "Проект" > "Добавить существующий элемент", чтобы добавить в проект существующие файлы кода.
//   6. Чтобы снова открыть этот проект позже, выберите пункты меню "Файл" > "Открыть" > "Проект" и выберите SLN-файл.


// Просто решаю.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
//KiruxaLight
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
#define _USE_MATH_DEFINES
#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <algorithm>
#include <utility>
#include <cmath>
#include <iomanip>
#include <stack>
#include <deque>
#include <queue>
#include <cstdio>
#include <unordered_map>
#include <unordered_set>
#include <numeric>
#include <cassert>
#include <bitset>
#include <ext/rope>
using namespace std;
using namespace __gnu_cxx;
#define int long long
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
const int INF = 1e9 + 123, MAXN = 5e5 + 47, MEGAINF = 1e18 + 228;
template <class T>
inline istream& operator >> (istream& in, vector <T>& a)
{
  for (auto& i : a)
    in >> i;
  return in;
}
template <class T>
inline ostream& operator << (ostream& out, vector <T>& a)
{
  for (auto i : a)
    out << i << " ";
  return out;
}
template <class T, class U>
inline istream& operator >> (istream& in, vector <pair <T, U>>& a)
{
  for (auto& i : a)
    in >> i.first >> i.second;
  return in;
}
template <class T, class U>
inline ostream& operator << (ostream& out, vector <pair <T, U>>& a)
{
  for (auto& i : a)
    out << i.first << " " << i.second << "\n";
  return out;
}
struct node
{
  vector <vector <int>> dp;
  node()
  {
    dp.resize(2, vector <int>(2));
    dp[0][0] = -MEGAINF;
    dp[0][1] = -MEGAINF;
    dp[1][0] = -MEGAINF;
    dp[1][1] = -MEGAINF;
  }
};
node operator + (node& a, node& b)
{
  node cur;
  for (int i = 0; i < 2; ++i)
    for (int j = 0; j < 2; ++j)
    {
      cur.dp[i][j] = max(a.dp[i][j], b.dp[i][j]);
      for (int k = 0; k < 2; ++k)
        cur.dp[i][j] = max(cur.dp[i][j], a.dp[i][k] + b.dp[k ^ 1][j]);
    }
  return cur;
}
struct segment_tree
{
  vector <node> t;
  int n;
  segment_tree()
  {

  }
  segment_tree(int N)
  {
    t.resize(N * 4);
    n = N;
  }
  node make(int val)
  {
    node res;
    res.dp[0][0] = -val;
    res.dp[1][1] = val;
    res.dp[0][1] = -MEGAINF;
    res.dp[1][0] = -MEGAINF;
    return res;
  }
  void build(vector <int>& a, int l, int r, int v = 1)
  {
    if (l == r)
    {
      t[v] = make(a[l]);
      return;
    }
    int mid = l + r >> 1;
    build(a, l, mid, v * 2);
    build(a, mid + 1, r, v * 2 + 1);
    t[v] = t[v * 2] + t[v * 2 + 1];
    /*t[v].dp[0][0] = max({t[v * 2].dp[0][0], t[v * 2 + 1].dp[0][0], t[v * 2].dp[0][1] + t[v * 2 + 1].dp[0][0], t[v * 2].dp[0][0] + t[v * 2 + 1].dp[1][0]});
    t[v].dp[0][1] =
    t[v].dp[1][0] =
    t[v].dp[1][1] =*/
  }
  void update(int pos, int val, int tl, int tr, int v = 1)
  {
    if (tl > pos || tr < pos)
      return;
    if (tl == pos && tr == pos)
    {
      t[v] = make(val);
      return;
    }
    int mid = tl + tr >> 1;
    update(pos, val, tl, mid, v * 2);
    update(pos, val, mid + 1, tr, v * 2 + 1);
    t[v] = t[v * 2] + t[v * 2 + 1];
  }
  void update(int pos, int val)
  {
    update(pos, val, 0, n - 1);
  }
};
signed main()
{
  setlocale(LC_ALL, "rus");

  /*freopen("treemaster.in", "r", stdin);
  freopen("treemaster.out", "w", stdout);*/

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int t;
  cin >> t;
  while (t--)
  {
    int n, q;
    cin >> n >> q;
    vector <int> a(n);
    cin >> a;
    segment_tree t(n);
    t.build(a, 0, n - 1);
    node cur = t.t[1];
    int ans = -MEGAINF;
    for (int i = 0; i < 2; ++i)
      for (int j = 0; j < 2; ++j)
        ans = max(ans, cur.dp[i][j]);
    cout << ans << "\n";
    while (q--)
    {
      int l, r;
      cin >> l >> r;
      --l, --r;
      t.update(l, a[r]);
      t.update(r, a[l]);
      swap(a[l], a[r]);
      ans = -MEGAINF;
      cur = t.t[1];
      for (int i = 0; i < 2; ++i)
        for (int j = 0; j < 2; ++j)
          ans = max(ans, cur.dp[i][j]);
      cout << ans << "\n";
    }
  }
}
/*
1
50 0
1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10
*/
// Запуск программыd: CTRL+F5 или меню "Отладка" > "Запуск без отладки"
// Отладка программы: F5 или меню "Отладка" > "Запустить отладку"

// Советы по началу работы
//   1. В окне обозревателя решений можно добавлять файлы и управлять ими.
//   2. В окне Team Explorer можно подключиться к системе управления версиями.
//   3. В окне "Выходные данные" можно просматривать выходные данные сборки и другие сообщения.
//   4. В окне "Список ошибок" можно просматривать ошибки.
//   5. Последовательно выберите пункты меню "Проект" > "Добавить новый элемент", чтобы создать файлы кода, или "Проект" > "Добавить существующий элемент", чтобы добавить в проект существующие файлы кода.
//   6. Чтобы снова открыть этот проект позже, выберите пункты меню "Файл" > "Открыть" > "Проект" и выберите SLN-файл.


// Просто решаю.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
//KiruxaLight
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
#define _USE_MATH_DEFINES
#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <algorithm>
#include <utility>
#include <cmath>
#include <iomanip>
#include <stack>
#include <deque>
#include <queue>
#include <cstdio>
#include <unordered_map>
#include <unordered_set>
#include <numeric>
#include <cassert>
#include <bitset>
#include <ext/rope>
using namespace std;
using namespace __gnu_cxx;
#define int long long
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
const int INF = 1e9 + 123, MAXN = 5e5 + 47, MEGAINF = 1e18 + 228;
template <class T>
inline istream& operator >> (istream& in, vector <T>& a)
{
  for (auto& i : a)
    in >> i;
  return in;
}
template <class T>
inline ostream& operator << (ostream& out, vector <T>& a)
{
  for (auto i : a)
    out << i << " ";
  return out;
}
template <class T, class U>
inline istream& operator >> (istream& in, vector <pair <T, U>>& a)
{
  for (auto& i : a)
    in >> i.first >> i.second;
  return in;
}
template <class T, class U>
inline ostream& operator << (ostream& out, vector <pair <T, U>>& a)
{
  for (auto& i : a)
    out << i.first << " " << i.second << "\n";
  return out;
}
struct node
{
  vector <vector <int>> dp;
  node()
  {
    dp.resize(2, vector <int>(2));
    dp[0][0] = -MEGAINF;
    dp[0][1] = -MEGAINF;
    dp[1][0] = -MEGAINF;
    dp[1][1] = -MEGAINF;
  }
};
node operator + (node a, node b)
{
  node cur;
  for (int i = 0; i < 2; ++i)
    for (int j = 0; j < 2; ++j)
    {
      cur.dp[i][j] = max(a.dp[i][j], b.dp[i][j]);
      for (int k = 0; k < 2; ++k)
        cur.dp[i][j] = max(cur.dp[i][j], a.dp[i][k] + b.dp[k ^ 1][j]);
    }
  return cur;
}
struct segment_tree
{
  vector <node> t;
  int n;
  segment_tree()
  {

  }
  segment_tree(int N)
  {
    t.resize(N * 4);
    n = N;
  }
  node make(int val)
  {
    node res;
    res.dp[0][0] = -val;
    res.dp[1][1] = val;
    res.dp[0][1] = -INF;
    res.dp[1][0] = -INF;
    return res;
  }
  void build(vector <int>& a, int l, int r, int v = 1)
  {
    if (l == r)
    {
      t[v] = make(a[l]);
      return;
    }
    int mid = l + r >> 1;
    build(a, l, mid, v * 2);
    build(a, mid + 1, r, v * 2 + 1);
    t[v] = t[v * 2] + t[v * 2 + 1];
    /*t[v].dp[0][0] = max({t[v * 2].dp[0][0], t[v * 2 + 1].dp[0][0], t[v * 2].dp[0][1] + t[v * 2 + 1].dp[0][0], t[v * 2].dp[0][0] + t[v * 2 + 1].dp[1][0]});
    t[v].dp[0][1] =
    t[v].dp[1][0] =
    t[v].dp[1][1] =*/
  }
  void update(int pos, int val, int tl, int tr, int v = 1)
  {
    if (tl > pos || tr < pos)
      return;
    if (tl == pos && tr == pos)
    {
      t[v] = make(val);
      return;
    }
    int mid = tl + tr >> 1;
    update(pos, val, tl, mid, v * 2);
    update(pos, val, mid + 1, tr, v * 2 + 1);
    t[v] = t[v * 2] + t[v * 2 + 1];
  }
  void update(int pos, int val)
  {
    update(pos, val, 0, n - 1);
  }
};
signed main()
{
  setlocale(LC_ALL, "rus");

  /*freopen("treemaster.in", "r", stdin);
  freopen("treemaster.out", "w", stdout);*/

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int t;
  cin >> t;
  while (t--)
  {
    int n, q;
    cin >> n >> q;
    vector <int> a(n);
    cin >> a;
    segment_tree t(n);
    t.build(a, 0, n - 1);
    node cur = t.t[1];
    int ans = -MEGAINF;
    for (int i = 0; i < 2; ++i)
      for (int j = 0; j < 2; ++j)
        ans = max(ans, cur.dp[i][j]);
    cout << ans << "\n";
    while (q--)
    {
      int l, r;
      cin >> l >> r;
      --l, --r;
      t.update(l, a[r]);
      t.update(r, a[l]);
      swap(a[l], a[r]);
      int res = -INF;
      cur = t.t[1];
      for (int i = 0; i < 2; ++i)
        for (int j = 0; j < 2; ++j)
          res = max(res, cur.dp[i][j]);
      cout << res << "\n";
    }
  }
}
// Запуск программыd: CTRL+F5 или меню "Отладка" > "Запуск без отладки"
// Отладка программы: F5 или меню "Отладка" > "Запустить отладку"

// Советы по началу работы
//   1. В окне обозревателя решений можно добавлять файлы и управлять ими.
//   2. В окне Team Explorer можно подключиться к системе управления версиями.
//   3. В окне "Выходные данные" можно просматривать выходные данные сборки и другие сообщения.
//   4. В окне "Список ошибок" можно просматривать ошибки.
//   5. Последовательно выберите пункты меню "Проект" > "Добавить новый элемент", чтобы создать файлы кода, или "Проект" > "Добавить существующий элемент", чтобы добавить в проект существующие файлы кода.
//   6. Чтобы снова открыть этот проект позже, выберите пункты меню "Файл" > "Открыть" > "Проект" и выберите SLN-файл.


// Просто решаю.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
//KiruxaLight
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
#define _USE_MATH_DEFINES
#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <algorithm>
#include <utility>
#include <cmath>
#include <iomanip>
#include <stack>
#include <deque>
#include <queue>
#include <cstdio>
#include <unordered_map>
#include <unordered_set>
#include <numeric>
#include <cassert>
#include <bitset>
#include <ext/rope>
using namespace std;
using namespace __gnu_cxx;
#define int long long
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
const int INF = 1e9 + 123, MAXN = 5e5 + 47, MEGAINF = 1e18 + 228;
template <class T>
inline istream& operator >> (istream& in, vector <T>& a)
{
  for (auto& i : a)
    in >> i;
  return in;
}
template <class T>
inline ostream& operator << (ostream& out, vector <T>& a)
{
  for (auto i : a)
    out << i << " ";
  return out;
}
template <class T, class U>
inline istream& operator >> (istream& in, vector <pair <T, U>>& a)
{
  for (auto& i : a)
    in >> i.first >> i.second;
  return in;
}
template <class T, class U>
inline ostream& operator << (ostream& out, vector <pair <T, U>>& a)
{
  for (auto& i : a)
    out << i.first << " " << i.second << "\n";
  return out;
}
signed main()
{
  setlocale(LC_ALL, "rus");

  /*freopen("treemaster.in", "r", stdin);
  freopen("treemaster.out", "w", stdout);*/

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int t;
  cin >> t;
  while (t--)
  {
    int n, q;
    cin >> n >> q;
    vector <int> a(n + 2);
    a[0] = -1;
    for (int i = 1; i <= n; ++i)
      cin >> a[i];
    a[n + 1] = -1;
    int ans = 0;
    auto make = [&](int i, int sign)
    {
      if (i == 0 || i == n + 1)
        return;
      if (a[i - 1] < a[i] && a[i] > a[i + 1])
        ans += a[i] * sign;
      else if (a[i - 1] > a[i] && a[i] < a[i + 1])
        ans -= a[i] * sign;
    };
    for (int i = 1; i <= n; ++i)
      make(i, 1);
    cout << ans << "\n";
    while (q--)
    {
      int l, r;
      cin >> l >> r;
      if (l == r)
      {
        cout << ans << "\n";
        continue;
      }
      set <int> need = {l - 1, l, l + 1, r - 1, r, r + 1};
      for (auto& i : need)
        make(i, -1);
      swap(a[l], a[r]);
      for (auto& i : need)
        make(i, 1);
      cout << ans << "\n";
    }
  }
}
// Запуск программыd: CTRL+F5 или меню "Отладка" > "Запуск без отладки"
// Отладка программы: F5 или меню "Отладка" > "Запустить отладку"

// Советы по началу работы
//   1. В окне обозревателя решений можно добавлять файлы и управлять ими.
//   2. В окне Team Explorer можно подключиться к системе управления версиями.
//   3. В окне "Выходные данные" можно просматривать выходные данные сборки и другие сообщения.
//   4. В окне "Список ошибок" можно просматривать ошибки.
//   5. Последовательно выберите пункты меню "Проект" > "Добавить новый элемент", чтобы создать файлы кода, или "Проект" > "Добавить существующий элемент", чтобы добавить в проект существующие файлы кода.
//   6. Чтобы снова открыть этот проект позже, выберите пункты меню "Файл" > "Открыть" > "Проект" и выберите SLN-файл.


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0)
#define mp make_pair
#define xx first
#define yy second
#define dif(x) ((x).yy-(x).xx)
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define all(x) (x).begin(),(x).end()
#define exist(a,x) find(all(a),x)!=a.end()
#define ub(a,x) upper_bound(all(a),x)
#define lb(a,x) lower_bound(all(a),x)
#define inv(n) power((n), mod - 2)
#define ff(i,a,b) for (int (i) = (a); (i) < (b); (i)++)
#define fff(i,a,b) for (int (i) = (a); (i) <= b; (i)++)
#define bff(i,a,b) for (int (i) = (b)-1; (i) >= (a); (i)--)
#define bfff(i,a,b) for (int (i) = (b); (i) >= (a); (i)--)

using namespace std;
long double typedef ld;
unsigned int typedef ui;
long long int typedef li;
pair<int,int> typedef pii;
pair<li,li> typedef pli;
vector<vector<int>> typedef graph;
unsigned long long int typedef ull;
const ld PI = 3.14159265359;
//const int mod = 998244353;
const int mod = 1000000007;
const li inf = 9223372036854775807ll;

using namespace __gnu_pbds;
template <class T> using oset = tree<T, null_type,greater<T>, rb_tree_tag,tree_order_statistics_node_update>;
template <class T> using omset = tree<T, null_type,less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>;

inline int power(li _a, int _n)
{
        li _c = 1;
        while (_n>0)
        {
                if (_n % 2) _c = _c * _a%mod;
                _a = _a * _a%mod;
                _n/=2;
        }
        return _c;
}

inline int geom(li _a, int _n) //suma 1 + a + a^2 + ... + a^(n-1)
{
    if (_a == 0) return 0;
        if (_a == 1) return _n;
        return ((li)power(_a, _n) - 1) * inv(_a - 1) % mod;
}

pli _r;

li bigmod(li _a, li _b, li _m) //ogromni mod
{
    if (_a==0) return 0;
    if (_a%2==1) return (_b + 2*bigmod(_a/2,_b,_m))%_m;
    return 2*bigmod(_a/2,_b,_m)%_m;
}

void euklid(li _a, li _b) //resava ax+by=1. a i b MORAJU biti POZITIVNI (analogno se nalaze resenja za negativne...)
{
    if (_b==1)
    {
        _r = mp(1,-_a+1);
        return;
    }

    euklid(_b,_a%_b); //cout<<r.xx<<" "<<r.yy<<endl;
    _r=mp(_r.yy,_r.xx-_r.yy*(_a/_b));
}

li kinez(li _x, li _a, li _y, li _b)
{
    if (_x==_y) return _x;
    if (_a>_b) {swap(_a,_b); swap(_x,_y);}

    euklid(_b,_a); (_r.xx*=(_x-_y))%=(_a*_b);
    return (bigmod(_b,_r.xx,_a*_b)+_y)%(_a*_b);
}

inline li sum(int _n) {return ((li)_n*(_n+1))/2;}

inline li kv(int _n) {return ((li)_n*(_n+1)*(2*_n+1))/6;}

//Note to self: Check for overflow

int niz[300003];

int main()
{
    #define high(i) niz[i]>niz[i-1] && niz[i]>niz[i+1]
    #define low(i) niz[i]<niz[i-1] && niz[i]<niz[i+1]

    FAST;

    int t; cin>>t;
    while (t--)
    {
        int n,q; cin>>n>>q;
        fff(i,1,n) cin>>niz[i];
        niz[0]=-mod; niz[n+1]=-mod;
        niz[-1]=-mod; niz[n+2]=-mod;

        li sumhigh=0,sumlow=0;
        fff(i,1,n)
        {
            if (high(i)) sumhigh+=niz[i];
            if (low(i)) sumlow+=niz[i];
        }

        cout<<sumhigh-sumlow<<endl;

        int l,r;
        while (q--)
        {
            cin>>l>>r;
            set<int> promene;
            promene.insert(l-1);
            promene.insert(l);
            promene.insert(l+1);
            promene.insert(r-1);
            promene.insert(r);
            promene.insert(r+1);

            for (auto i : promene)
            {
                if (high(i)) sumhigh-=niz[i];
                if (low(i)) sumlow-=niz[i];
            }

            swap(niz[l],niz[r]);

            for (auto i : promene)
            {
                if (high(i)) sumhigh+=niz[i];
                if (low(i)) sumlow+=niz[i];
            }

            cout<<sumhigh-sumlow<<endl;
        }
    }
}

//Note to self: Check for overflow


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0)
#define mp make_pair
#define xx first
#define yy second
#define dif(x) ((x).yy-(x).xx)
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define all(x) (x).begin(),(x).end()
#define exist(a,x) find(all(a),x)!=a.end()
#define ub(a,x) upper_bound(all(a),x)
#define lb(a,x) lower_bound(all(a),x)
#define inv(n) power((n), mod - 2)
#define ff(i,a,b) for (int (i) = (a); (i) < (b); (i)++)
#define fff(i,a,b) for (int (i) = (a); (i) <= b; (i)++)
#define bff(i,a,b) for (int (i) = (b)-1; (i) >= (a); (i)--)
#define bfff(i,a,b) for (int (i) = (b); (i) >= (a); (i)--)

using namespace std;
long double typedef ld;
unsigned int typedef ui;
long long int typedef li;
pair<int,int> typedef pii;
pair<li,li> typedef pli;
vector<vector<int>> typedef graph;
unsigned long long int typedef ull;
const ld PI = 3.14159265359;
//const int mod = 998244353;
const int mod = 1000000007;
const li inf = 9223372036854775807ll;

using namespace __gnu_pbds;
template <class T> using oset = tree<T, null_type,greater<T>, rb_tree_tag,tree_order_statistics_node_update>;
template <class T> using omset = tree<T, null_type,less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>;

inline int power(li _a, int _n)
{
        li _c = 1;
        while (_n>0)
        {
                if (_n % 2) _c = _c * _a%mod;
                _a = _a * _a%mod;
                _n/=2;
        }
        return _c;
}

inline int geom(li _a, int _n) //suma 1 + a + a^2 + ... + a^(n-1)
{
    if (_a == 0) return 0;
        if (_a == 1) return _n;
        return ((li)power(_a, _n) - 1) * inv(_a - 1) % mod;
}

pli _r;

li bigmod(li _a, li _b, li _m) //ogromni mod
{
    if (_a==0) return 0;
    if (_a%2==1) return (_b + 2*bigmod(_a/2,_b,_m))%_m;
    return 2*bigmod(_a/2,_b,_m)%_m;
}

void euklid(li _a, li _b) //resava ax+by=1. a i b MORAJU biti POZITIVNI (analogno se nalaze resenja za negativne...)
{
    if (_b==1)
    {
        _r = mp(1,-_a+1);
        return;
    }

    euklid(_b,_a%_b); //cout<<r.xx<<" "<<r.yy<<endl;
    _r=mp(_r.yy,_r.xx-_r.yy*(_a/_b));
}

li kinez(li _x, li _a, li _y, li _b)
{
    if (_x==_y) return _x;
    if (_a>_b) {swap(_a,_b); swap(_x,_y);}

    euklid(_b,_a); (_r.xx*=(_x-_y))%=(_a*_b);
    return (bigmod(_b,_r.xx,_a*_b)+_y)%(_a*_b);
}

inline li sum(int _n) {return ((li)_n*(_n+1))/2;}

inline li kv(int _n) {return ((li)_n*(_n+1)*(2*_n+1))/6;}

//Note to self: Check for overflow

int niz[100003];

int main()
{
    #define high(i) niz[i]>niz[i-1] && niz[i]>niz[i+1]
    #define low(i) niz[i]<niz[i-1] && niz[i]<niz[i+1]

    FAST;

    int t; cin>>t;
    while (t--)
    {
        int n,q; cin>>n>>q;
        fff(i,1,n) cin>>niz[i];
        niz[0]=-mod; niz[n+1]=-mod;
        niz[-1]=-mod; niz[n+2]=-mod;

        int sumhigh=0,sumlow=0;
        fff(i,1,n)
        {
            if (high(i)) sumhigh+=niz[i];
            if (low(i)) sumlow+=niz[i];
        }

        cout<<sumhigh-sumlow<<endl;

        int l,r;
        while (q--)
        {
            cin>>l>>r;
            set<int> promene;
            promene.insert(l-1);
            promene.insert(l);
            promene.insert(l+1);
            promene.insert(r-1);
            promene.insert(r);
            promene.insert(r+1);

            for (auto i : promene)
            {
                if (high(i)) sumhigh-=niz[i];
                if (low(i)) sumlow-=niz[i];
            }

            swap(niz[l],niz[r]);

            for (auto i : promene)
            {
                if (high(i)) sumhigh+=niz[i];
                if (low(i)) sumlow+=niz[i];
            }

            cout<<sumhigh-sumlow<<endl;
        }
    }
}

//Note to self: Check for overflow


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0)
#define mp make_pair
#define xx first
#define yy second
#define dif(x) ((x).yy-(x).xx)
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define all(x) (x).begin(),(x).end()
#define exist(a,x) find(all(a),x)!=a.end()
#define ub(a,x) upper_bound(all(a),x)
#define lb(a,x) lower_bound(all(a),x)
#define inv(n) power((n), mod - 2)
#define ff(i,a,b) for (int (i) = (a); (i) < (b); (i)++)
#define fff(i,a,b) for (int (i) = (a); (i) <= b; (i)++)
#define bff(i,a,b) for (int (i) = (b)-1; (i) >= (a); (i)--)
#define bfff(i,a,b) for (int (i) = (b); (i) >= (a); (i)--)

using namespace std;
long double typedef ld;
unsigned int typedef ui;
long long int typedef li;
pair<int,int> typedef pii;
pair<li,li> typedef pli;
vector<vector<int>> typedef graph;
unsigned long long int typedef ull;
const ld PI = 3.14159265359;
//const int mod = 998244353;
const int mod = 1000000007;
const li inf = 9223372036854775807ll;

using namespace __gnu_pbds;
template <class T> using oset = tree<T, null_type,greater<T>, rb_tree_tag,tree_order_statistics_node_update>;
template <class T> using omset = tree<T, null_type,less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>;

inline int power(li _a, int _n)
{
        li _c = 1;
        while (_n>0)
        {
                if (_n % 2) _c = _c * _a%mod;
                _a = _a * _a%mod;
                _n/=2;
        }
        return _c;
}

inline int geom(li _a, int _n) //suma 1 + a + a^2 + ... + a^(n-1)
{
    if (_a == 0) return 0;
        if (_a == 1) return _n;
        return ((li)power(_a, _n) - 1) * inv(_a - 1) % mod;
}

pli _r;

li bigmod(li _a, li _b, li _m) //ogromni mod
{
    if (_a==0) return 0;
    if (_a%2==1) return (_b + 2*bigmod(_a/2,_b,_m))%_m;
    return 2*bigmod(_a/2,_b,_m)%_m;
}

void euklid(li _a, li _b) //resava ax+by=1. a i b MORAJU biti POZITIVNI (analogno se nalaze resenja za negativne...)
{
    if (_b==1)
    {
        _r = mp(1,-_a+1);
        return;
    }

    euklid(_b,_a%_b); //cout<<r.xx<<" "<<r.yy<<endl;
    _r=mp(_r.yy,_r.xx-_r.yy*(_a/_b));
}

li kinez(li _x, li _a, li _y, li _b)
{
    if (_x==_y) return _x;
    if (_a>_b) {swap(_a,_b); swap(_x,_y);}

    euklid(_b,_a); (_r.xx*=(_x-_y))%=(_a*_b);
    return (bigmod(_b,_r.xx,_a*_b)+_y)%(_a*_b);
}

inline li sum(int _n) {return ((li)_n*(_n+1))/2;}

inline li kv(int _n) {return ((li)_n*(_n+1)*(2*_n+1))/6;}

//Note to self: Check for overflow

int n,k;
int niz[103503];
int sector[1002][2];
int cringe[1002][2];

void calcsec(int p)
{
    li par=0,npr=-mod;
    li expar=0,exnpr=-mod;

    ff(i,p*k,p*k+k)
    {
        par=max(par,exnpr-niz[i]);
        npr=max(npr,expar+niz[i]);
        expar=par; exnpr=npr;
    }

    sector[p][0]=par;
    sector[p][1]=npr;
}

void calcringe(int p)
{
    li par=0,npr=-niz[p*k];
    li expar=0,exnpr=npr;

    ff(i,p*k+1,p*k+k)
    {
        par=max(par,exnpr+niz[i]);
        npr=max(npr,expar-niz[i]);
        expar=par; exnpr=npr;
    }

    cringe[p][0]=par;
    cringe[p][1]=npr;
}

li query()
{
    li par=0,npr=-mod;
    li expar=0,exnpr=-mod;

    ff(i,0,n/k+1)
    {
        par=max(par,expar+sector[i][0]);
        par=max(par,exnpr+cringe[i][1]);
        npr=max(npr,expar+sector[i][1]);
        npr=max(npr,exnpr+cringe[i][0]);
        expar=par; exnpr=npr;
    }

    return npr;
}

int main()
{
    FAST;

    int t; cin>>t;
    while (t--)
    {
        int q; cin>>n>>q;
        ff(i,0,n) cin>>niz[i];
        k=sqrt(n);

        ff(i,n,n+2*k) niz[i]=0;
        ff(i,0,n/k+1) calcsec(i);
        ff(i,0,n/k+1) calcringe(i);

        //ff(i,0,n/k+1) cout<<i<<": "<<sector[i][0]<<" "<<sector[i][1]<<" - "<<cringe[i][0]<<" "<<cringe[i][1]<<endl;

        cout<<query()<<endl;

        while (q--)
        {
            int i,j; cin>>i>>j;
            swap(niz[i-1],niz[j-1]);

            //cout<<": "; ff(i,0,n) cout<<niz[i]<<" "; cout<<endl;
            //ff(i,0,n/k+1) cout<<i<<": "<<sector[i][0]<<" "<<sector[i][1]<<" - "<<cringe[i][0]<<" "<<cringe[i][1]<<endl;

            calcsec((i-1)/k); calcringe((i-1)/k);
            calcsec((j-1)/k); calcringe((j-1)/k);

            cout<<query()<<endl;
        }
    }
}

//Note to self: Check for overflow


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0)
#define mp make_pair
#define xx first
#define yy second
#define dif(x) ((x).yy-(x).xx)
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define all(x) (x).begin(),(x).end()
#define exist(a,x) find(all(a),x)!=a.end()
#define ub(a,x) upper_bound(all(a),x)
#define lb(a,x) lower_bound(all(a),x)
#define inv(n) power((n), mod - 2)
#define ff(i,a,b) for (int (i) = (a); (i) < (b); (i)++)
#define fff(i,a,b) for (int (i) = (a); (i) <= b; (i)++)
#define bff(i,a,b) for (int (i) = (b)-1; (i) >= (a); (i)--)
#define bfff(i,a,b) for (int (i) = (b); (i) >= (a); (i)--)

using namespace std;
long double typedef ld;
unsigned int typedef ui;
long long int typedef li;
pair<int,int> typedef pii;
pair<li,li> typedef pli;
vector<vector<int>> typedef graph;
unsigned long long int typedef ull;
const ld PI = 3.14159265359;
//const int mod = 998244353;
const int mod = 1000000007;
const li inf = 9223372036854775807ll;

using namespace __gnu_pbds;
template <class T> using oset = tree<T, null_type,greater<T>, rb_tree_tag,tree_order_statistics_node_update>;
template <class T> using omset = tree<T, null_type,less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>;

inline int power(li _a, int _n)
{
        li _c = 1;
        while (_n>0)
        {
                if (_n % 2) _c = _c * _a%mod;
                _a = _a * _a%mod;
                _n/=2;
        }
        return _c;
}

inline int geom(li _a, int _n) //suma 1 + a + a^2 + ... + a^(n-1)
{
    if (_a == 0) return 0;
        if (_a == 1) return _n;
        return ((li)power(_a, _n) - 1) * inv(_a - 1) % mod;
}

pli _r;

li bigmod(li _a, li _b, li _m) //ogromni mod
{
    if (_a==0) return 0;
    if (_a%2==1) return (_b + 2*bigmod(_a/2,_b,_m))%_m;
    return 2*bigmod(_a/2,_b,_m)%_m;
}

void euklid(li _a, li _b) //resava ax+by=1. a i b MORAJU biti POZITIVNI (analogno se nalaze resenja za negativne...)
{
    if (_b==1)
    {
        _r = mp(1,-_a+1);
        return;
    }

    euklid(_b,_a%_b); //cout<<r.xx<<" "<<r.yy<<endl;
    _r=mp(_r.yy,_r.xx-_r.yy*(_a/_b));
}

li kinez(li _x, li _a, li _y, li _b)
{
    if (_x==_y) return _x;
    if (_a>_b) {swap(_a,_b); swap(_x,_y);}

    euklid(_b,_a); (_r.xx*=(_x-_y))%=(_a*_b);
    return (bigmod(_b,_r.xx,_a*_b)+_y)%(_a*_b);
}

inline li sum(int _n) {return ((li)_n*(_n+1))/2;}

inline li kv(int _n) {return ((li)_n*(_n+1)*(2*_n+1))/6;}

//Note to self: Check for overflow

int n,k;
int niz[103503];
int sector[1002][2];
int cringe[1002][2];

void calcsec(int p)
{
    li par=0,npr=-mod;
    li expar=0,exnpr=-mod;

    ff(i,p*k,p*k+k)
    {
        par=max(par,exnpr-niz[i]);
        npr=max(npr,expar+niz[i]);
        expar=par; exnpr=npr;
    }

    sector[p][0]=par;
    sector[p][1]=npr;
}

void calcringe(int p)
{
    li par=0,npr=-niz[p*k];
    li expar=0,exnpr=npr;

    ff(i,p*k+1,p*k+k)
    {
        par=max(par,exnpr+niz[i]);
        npr=max(npr,expar-niz[i]);
        expar=par; exnpr=npr;
    }

    cringe[p][0]=par;
    cringe[p][1]=npr;
}

li query()
{
    li par=0,npr=-mod;
    li expar=0,exnpr=-mod;

    ff(i,0,n/k+1)
    {
        par=max(par,expar+sector[i][0]);
        par=max(par,exnpr+cringe[i][1]);
        npr=max(npr,expar+sector[i][1]);
        npr=max(npr,exnpr+cringe[i][0]);
        expar=par; exnpr=npr;
    }

    return npr;
}

int main()
{
    FAST;

    int t; cin>>t;
    while (t--)
    {
        int q; cin>>n>>q;
        ff(i,0,n) cin>>niz[i];
        k=sqrt(n);

        ff(i,n,n+2*k) niz[i]=0;
        ff(i,0,n/k+1) calcsec(i);
        ff(i,0,n/k+1) calcringe(i);

        //ff(i,0,n/k+1) cout<<i<<": "<<sector[i][0]<<" "<<sector[i][1]<<" - "<<cringe[i][0]<<" "<<cringe[i][1]<<endl;

        cout<<query()<<endl;

        while (q--)
        {
            int i,j; cin>>i>>j;
            swap(niz[i-1],niz[j-1]);

            //cout<<": "; ff(i,0,n) cout<<niz[i]<<" "; cout<<endl;
            //ff(i,0,n/k+1) cout<<i<<": "<<sector[i][0]<<" "<<sector[i][1]<<" - "<<cringe[i][0]<<" "<<cringe[i][1]<<endl;

            ff(p,0,n/k+1) calcsec(p);
            ff(p,0,n/k+1) calcringe(p);

            calcsec((i-1)/k); calcringe((i-1)/k);
            calcringe((j-1)/k); calcringe((j-1)/k);

            cout<<query()<<endl;
        }
    }
}

//Note to self: Check for overflow


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0)
#define mp make_pair
#define xx first
#define yy second
#define dif(x) ((x).yy-(x).xx)
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define all(x) (x).begin(),(x).end()
#define exist(a,x) find(all(a),x)!=a.end()
#define ub(a,x) upper_bound(all(a),x)
#define lb(a,x) lower_bound(all(a),x)
#define inv(n) power((n), mod - 2)
#define ff(i,a,b) for (int (i) = (a); (i) < (b); (i)++)
#define fff(i,a,b) for (int (i) = (a); (i) <= b; (i)++)
#define bff(i,a,b) for (int (i) = (b)-1; (i) >= (a); (i)--)
#define bfff(i,a,b) for (int (i) = (b); (i) >= (a); (i)--)

using namespace std;
long double typedef ld;
unsigned int typedef ui;
long long int typedef li;
pair<int,int> typedef pii;
pair<li,li> typedef pli;
vector<vector<int>> typedef graph;
unsigned long long int typedef ull;
const ld PI = 3.14159265359;
//const int mod = 998244353;
const int mod = 1000000007;
const li inf = 9223372036854775807ll;

using namespace __gnu_pbds;
template <class T> using oset = tree<T, null_type,greater<T>, rb_tree_tag,tree_order_statistics_node_update>;
template <class T> using omset = tree<T, null_type,less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>;

inline int power(li _a, int _n)
{
        li _c = 1;
        while (_n>0)
        {
                if (_n % 2) _c = _c * _a%mod;
                _a = _a * _a%mod;
                _n/=2;
        }
        return _c;
}

inline int geom(li _a, int _n) //suma 1 + a + a^2 + ... + a^(n-1)
{
    if (_a == 0) return 0;
        if (_a == 1) return _n;
        return ((li)power(_a, _n) - 1) * inv(_a - 1) % mod;
}

pli _r;

li bigmod(li _a, li _b, li _m) //ogromni mod
{
    if (_a==0) return 0;
    if (_a%2==1) return (_b + 2*bigmod(_a/2,_b,_m))%_m;
    return 2*bigmod(_a/2,_b,_m)%_m;
}

void euklid(li _a, li _b) //resava ax+by=1. a i b MORAJU biti POZITIVNI (analogno se nalaze resenja za negativne...)
{
    if (_b==1)
    {
        _r = mp(1,-_a+1);
        return;
    }

    euklid(_b,_a%_b); //cout<<r.xx<<" "<<r.yy<<endl;
    _r=mp(_r.yy,_r.xx-_r.yy*(_a/_b));
}

li kinez(li _x, li _a, li _y, li _b)
{
    if (_x==_y) return _x;
    if (_a>_b) {swap(_a,_b); swap(_x,_y);}

    euklid(_b,_a); (_r.xx*=(_x-_y))%=(_a*_b);
    return (bigmod(_b,_r.xx,_a*_b)+_y)%(_a*_b);
}

inline li sum(int _n) {return ((li)_n*(_n+1))/2;}

inline li kv(int _n) {return ((li)_n*(_n+1)*(2*_n+1))/6;}

//Note to self: Check for overflow

int n,k;
int niz[103503];
int sector[1002][2];
int cringe[1002][2];

void calcsec(int p)
{
    li par=0,npr=-mod;
    li expar=0,exnpr=-mod;

    ff(i,p*k,p*k+k)
    {
        par=max(par,exnpr-niz[i]);
        npr=max(npr,expar+niz[i]);
        expar=par; exnpr=npr;
    }

    sector[p][0]=par;
    sector[p][1]=npr;
}

void calcringe(int p)
{
    li par=0,npr=-niz[p*k];
    li expar=0,exnpr=npr;

    ff(i,p*k+1,p*k+k)
    {
        par=max(par,exnpr+niz[i]);
        npr=max(npr,expar-niz[i]);
        expar=par; exnpr=npr;
    }

    cringe[p][0]=par;
    cringe[p][1]=npr;
}

li query()
{
    li par=0,npr=-mod;
    li expar=0,exnpr=-mod;

    ff(i,0,n/k+1)
    {
        par=max(par,expar+sector[i][0]);
        par=max(par,exnpr+cringe[i][1]);
        npr=max(npr,expar+sector[i][1]);
        npr=max(npr,exnpr+cringe[i][0]);
        expar=par; exnpr=npr;
    }

    return npr;
}

int main()
{
    FAST;

    int t; cin>>t;
    while (t--)
    {
        int q; cin>>n>>q;
        ff(i,0,n) cin>>niz[i];
        k=sqrt(n);

        ff(i,n,n+2*k) niz[i]=0;
        ff(i,0,n/k+1) calcsec(i);
        ff(i,0,n/k+1) calcringe(i);

        //ff(i,0,n/k+1) cout<<i<<": "<<sector[i][0]<<" "<<sector[i][1]<<" - "<<cringe[i][0]<<" "<<cringe[i][1]<<endl;

        cout<<query()<<endl;

        while (q--)
        {
            int i,j; cin>>i>>j;
            swap(niz[i-1],niz[j-1]);

            //cout<<": "; ff(i,0,n) cout<<niz[i]<<" "; cout<<endl;
            //ff(i,0,n/k+1) cout<<i<<": "<<sector[i][0]<<" "<<sector[i][1]<<" - "<<cringe[i][0]<<" "<<cringe[i][1]<<endl;

            calcsec((i-1)/k); calcringe((i-1)/k);
            calcringe((j-1)/k); calcringe((j-1)/k);

            cout<<query()<<endl;
        }
    }
}

//Note to self: Check for overflow


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0)
#define mp make_pair
#define xx first
#define yy second
#define dif(x) ((x).yy-(x).xx)
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define all(x) (x).begin(),(x).end()
#define exist(a,x) find(all(a),x)!=a.end()
#define ub(a,x) upper_bound(all(a),x)
#define lb(a,x) lower_bound(all(a),x)
#define inv(n) power((n), mod - 2)
#define ff(i,a,b) for (int (i) = (a); (i) < (b); (i)++)
#define fff(i,a,b) for (int (i) = (a); (i) <= b; (i)++)
#define bff(i,a,b) for (int (i) = (b)-1; (i) >= (a); (i)--)
#define bfff(i,a,b) for (int (i) = (b); (i) >= (a); (i)--)

using namespace std;
long double typedef ld;
unsigned int typedef ui;
long long int typedef li;
pair<int,int> typedef pii;
pair<li,li> typedef pli;
vector<vector<int>> typedef graph;
unsigned long long int typedef ull;
const ld PI = 3.14159265359;
//const int mod = 998244353;
const int mod = 1000000007;
const li inf = 9223372036854775807ll;

using namespace __gnu_pbds;
template <class T> using oset = tree<T, null_type,greater<T>, rb_tree_tag,tree_order_statistics_node_update>;
template <class T> using omset = tree<T, null_type,less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>;

inline int power(li _a, int _n)
{
        li _c = 1;
        while (_n>0)
        {
                if (_n % 2) _c = _c * _a%mod;
                _a = _a * _a%mod;
                _n/=2;
        }
        return _c;
}

inline int geom(li _a, int _n) //suma 1 + a + a^2 + ... + a^(n-1)
{
    if (_a == 0) return 0;
        if (_a == 1) return _n;
        return ((li)power(_a, _n) - 1) * inv(_a - 1) % mod;
}

pli _r;

li bigmod(li _a, li _b, li _m) //ogromni mod
{
    if (_a==0) return 0;
    if (_a%2==1) return (_b + 2*bigmod(_a/2,_b,_m))%_m;
    return 2*bigmod(_a/2,_b,_m)%_m;
}

void euklid(li _a, li _b) //resava ax+by=1. a i b MORAJU biti POZITIVNI (analogno se nalaze resenja za negativne...)
{
    if (_b==1)
    {
        _r = mp(1,-_a+1);
        return;
    }

    euklid(_b,_a%_b); //cout<<r.xx<<" "<<r.yy<<endl;
    _r=mp(_r.yy,_r.xx-_r.yy*(_a/_b));
}

li kinez(li _x, li _a, li _y, li _b)
{
    if (_x==_y) return _x;
    if (_a>_b) {swap(_a,_b); swap(_x,_y);}

    euklid(_b,_a); (_r.xx*=(_x-_y))%=(_a*_b);
    return (bigmod(_b,_r.xx,_a*_b)+_y)%(_a*_b);
}

inline li sum(int _n) {return ((li)_n*(_n+1))/2;}

inline li kv(int _n) {return ((li)_n*(_n+1)*(2*_n+1))/6;}

//Note to self: Check for overflow

int niz[300003];

int main()
{
    #define high(i) niz[i]>niz[i-1] && niz[i]>niz[i+1]
    #define low(i) niz[i]<niz[i-1] && niz[i]<niz[i+1]

    FAST;

    int t; cin>>t;
    while (t--)
    {
        int n,q; cin>>n>>q;
        fff(i,1,n) cin>>niz[i];
        niz[0]=-mod; niz[n+1]=-mod;
        niz[-1]=-mod; niz[n+2]=-mod;

        int sumhigh=0,sumlow=0;
        fff(i,1,n)
        {
            if (high(i)) sumhigh+=niz[i];
            if (low(i)) sumlow+=niz[i];
        }

        cout<<sumhigh-sumlow<<endl;

        int l,r;
        while (q--)
        {
            cin>>l>>r;
            set<int> promene;
            promene.insert(l-1);
            promene.insert(l);
            promene.insert(l+1);
            promene.insert(r-1);
            promene.insert(r);
            promene.insert(r+1);

            for (auto i : promene)
            {
                if (high(i)) sumhigh-=niz[i];
                if (low(i)) sumlow-=niz[i];
            }

            swap(niz[l],niz[r]);

            for (auto i : promene)
            {
                if (high(i)) sumhigh+=niz[i];
                if (low(i)) sumlow+=niz[i];
            }

            cout<<sumhigh-sumlow<<endl;
        }
    }
}

//Note to self: Check for overflow


// Smile please :)

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cassert>
#include <cctype>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <string>
#include <iomanip>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <random>
#include <deque>
#include <set>
#include <map>
#include <list>
#include <bitset>
#include <climits>
#include <unordered_map>
#include <unordered_set>
#include <fstream>
using namespace std;

#ifdef KVARK161
#define gdb(x) cerr << #x << " = " << x << endl;
#define gdb2(x, y) cerr << #x << " = " << x << "; " << #y << " = " << y << endl;
#else
#define gdb(...) (void(1));
#define gdb2(...) (void(1));
#endif

using i8 = char;
using ui8 = unsigned char;
using i16 = short;
using u16 = unsigned short;
using i32 = int;
using ui32 = unsigned int;
using i64 = long long int;
using ui64 = unsigned long long int;

#define ALL(v) (v.begin()), (v.end())
#define SZ(v) ((int)((v).size()))

const i64 INF = 1e18;

struct S {
    i64 endPos;
    i64 w;
    i64 h;
    friend bool operator<(const S& a, const S& b) {
        return a.endPos < b.endPos;
    }
};

int v[int(3e5 + 100)];
struct TTask {
    i64 n;
    i64 q;
    set<pair<int, int>> allPeaks;
    set<int> allPeaks2;
    i64 ans = 0;

    bool isMin(int pos) {
        return (pos == 1 || v[pos] < v[pos - 1]) && (pos == n || v[pos] < v[pos + 1]);
    }

    bool isMax(int pos) {
        return (pos == 1 || v[pos] > v[pos - 1]) && (pos == n || v[pos] > v[pos + 1]);
    }

    void recalcPeak(int pos) {
        if (pos < 1 || pos > n) {
            return;
        }
        auto it = allPeaks.upper_bound(pair<int, int>(pos, -1e9));
        if (it != allPeaks.end() && it->first == pos) {
            ans -= it->second;
            allPeaks.erase(it);
            allPeaks2.erase(-pos);
        }
        if (isMin(pos) && pos != 1 && pos != n) {
            ans -= v[pos];
            allPeaks.insert({pos, -v[pos]});
            allPeaks2.insert(-pos);
        } else if (isMax(pos)) {
            ans += v[pos];
            allPeaks.insert({pos, v[pos]});
            allPeaks2.insert(-pos);
        }
    }

    set<int> xx;
    void recalc(int pos) {
        xx.insert(pos);
        xx.insert(pos + 1);
        xx.insert(pos - 1);
        auto it = allPeaks.upper_bound(pair<int, int>(pos, 1e9));
        if (it != allPeaks.end()) {
            xx.insert(it->first);
        }
        it = allPeaks.upper_bound(pair<int, int>(pos, -1e9));
        if (it != allPeaks.end()) {
            xx.insert(it->first);
        }
        auto it2 = allPeaks2.upper_bound(-pos);
        if (it2 != allPeaks2.end()) {
            xx.insert(*it2);
        }
    }

    void doRecalc() {
        for (int x: xx) {
            recalcPeak(x);
        }
        xx.clear();
    }

    void Solve() {
        cin >> n >> q;
        v[0] = 0;
        v[n + 1] = 0;
       for (int i = 1; i <= n; ++i) {
           cin >> v[i];
       }
       for (int i = 1; i <= n; ++i) {
            if (isMin(i) && i != 1 && i != n) {
                allPeaks.insert({i, -v[i]});
                allPeaks2.insert(-i);
                ans -= v[i];
            }
            if (isMax(i)) {
                allPeaks.insert({i, v[i]});
                allPeaks2.insert(-i);
                ans += v[i];
            }
       }
       cout << ans << "\n";
       while (q--) {
           int l, r;
           cin >> l >> r;
           swap(v[l], v[r]);
           recalc(l);
           recalc(r);
           doRecalc();
           cout << ans << "\n";
       }
    }
};

const int N = 8e5 + 10;

int main(){
#ifdef KVARK161
    freopen("input.txt", "r", stdin);
    /*
    freopen("capastaty_input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
    //*/
#else
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    srand(time(0));
#endif
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout.precision(15); cout.setf(ios::fixed); cerr.precision(15); cerr.setf(ios::fixed);

    int t = 1;
    cin >> t;
    for (int i = 0; i < t; ++i) {
        TTask task = {};
        task.Solve();
    }

    return 0;
}


// Smile please :)

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cassert>
#include <cctype>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <string>
#include <iomanip>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <random>
#include <deque>
#include <set>
#include <map>
#include <list>
#include <bitset>
#include <climits>
#include <unordered_map>
#include <unordered_set>
#include <fstream>
using namespace std;

#ifdef KVARK161
#define gdb(x) cerr << #x << " = " << x << endl;
#define gdb2(x, y) cerr << #x << " = " << x << "; " << #y << " = " << y << endl;
#else
#define gdb(...) (void(1));
#define gdb2(...) (void(1));
#endif

using i8 = char;
using ui8 = unsigned char;
using i16 = short;
using u16 = unsigned short;
using i32 = int;
using ui32 = unsigned int;
using i64 = long long int;
using ui64 = unsigned long long int;

#define ALL(v) (v.begin()), (v.end())
#define SZ(v) ((int)((v).size()))

const i64 INF = 1e18;

struct S {
    i64 endPos;
    i64 w;
    i64 h;
    friend bool operator<(const S& a, const S& b) {
        return a.endPos < b.endPos;
    }
};

int v[int(3e5 + 100)];
struct TTask {
    i64 n;
    i64 q;
    set<pair<int, int>> allPeaks;
    set<int> allPeaks2;
    i64 ans = 0;

    bool isMin(int pos) {
        return (pos == 1 || v[pos] < v[pos - 1]) && (pos == n || v[pos] < v[pos + 1]);
    }

    bool isMax(int pos) {
        return (pos == 1 || v[pos] > v[pos - 1]) && (pos == n || v[pos] > v[pos + 1]);
    }

    void recalcPeak(int pos) {
        if (pos < 1 || pos > n) {
            return;
        }
        auto it = allPeaks.lower_bound(pair<int, int>(pos, -1e9));
        if (it != allPeaks.end() && it->first == pos) {
            ans -= it->second;
            allPeaks.erase(it);
        }
        if (isMin(pos) && pos != 1 && pos != n) {
            ans -= v[pos];
            allPeaks.insert({pos, -v[pos]});
        } else if (isMax(pos)) {
            ans += v[pos];
            allPeaks.insert({pos, v[pos]});
        }
    }

    set<int> xx;
    void recalc(int pos) {
        xx.insert(pos);
        xx.insert(pos + 1);
        xx.insert(pos - 1);
    }

    void doRecalc() {
        for (int x: xx) {
            recalcPeak(x);
        }
        xx.clear();
    }

    void Solve() {
        cin >> n >> q;
        v[0] = 0;
        v[n + 1] = 0;
       for (int i = 1; i <= n; ++i) {
           cin >> v[i];
       }
       for (int i = 1; i <= n; ++i) {
            if (isMin(i) && i != 1 && i != n) {
                allPeaks.insert({i, -v[i]});
                ans -= v[i];
            }
            if (isMax(i)) {
                allPeaks.insert({i, v[i]});
                ans += v[i];
            }
       }
       stringstream ss;
        ss << ans << "\n";
       while (q--) {
           int l, r;
           cin >> l >> r;
           swap(v[l], v[r]);
           recalc(l);
           recalc(r);
           doRecalc();
           ss << ans << "\n";
       }
       cout << ss.str();
    }
};

const int N = 8e5 + 10;

int main(){
#ifdef KVARK161
    auto start = chrono::steady_clock::now();
    freopen("input.txt", "r", stdin);
    /*
    freopen("capastaty_input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
    //*/
#else
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    srand(time(0));
#endif
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout.precision(15); cout.setf(ios::fixed); cerr.precision(15); cerr.setf(ios::fixed);

    int t = 1;
    cin >> t;
    for (int i = 0; i < t; ++i) {
        TTask task = {};
        task.Solve();
    }

#ifdef KVARK161
    auto end = chrono::steady_clock::now();
    auto diff = end - start;
    cout << "\n====================\n";
    cout << chrono::duration <double, milli> (diff).count() << " ms" << endl;
#endif
    return 0;
}


// Smile please :)

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cassert>
#include <cctype>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <string>
#include <iomanip>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <random>
#include <deque>
#include <set>
#include <map>
#include <list>
#include <bitset>
#include <climits>
#include <unordered_map>
#include <unordered_set>
#include <fstream>
using namespace std;

#ifdef KVARK161
#define gdb(x) cerr << #x << " = " << x << endl;
#define gdb2(x, y) cerr << #x << " = " << x << "; " << #y << " = " << y << endl;
#else
#define gdb(...) (void(1));
#define gdb2(...) (void(1));
#endif

using i8 = char;
using ui8 = unsigned char;
using i16 = short;
using u16 = unsigned short;
using i32 = int;
using ui32 = unsigned int;
using i64 = long long int;
using ui64 = unsigned long long int;

#define ALL(v) (v.begin()), (v.end())
#define SZ(v) ((int)((v).size()))

const i64 INF = 1e18;

struct S {
    i64 endPos;
    i64 w;
    i64 h;
    friend bool operator<(const S& a, const S& b) {
        return a.endPos < b.endPos;
    }
};

int v[int(3e5 + 100)];
struct TTask {
    i64 n;
    i64 q;
    set<pair<int, int>> allPeaks;
    set<int> allPeaks2;
    i64 ans = 0;

    bool isMin(int pos) {
        return (pos == 1 || v[pos] < v[pos - 1]) && (pos == n || v[pos] < v[pos + 1]);
    }

    bool isMax(int pos) {
        return (pos == 1 || v[pos] > v[pos - 1]) && (pos == n || v[pos] > v[pos + 1]);
    }

    void recalcPeak(int pos) {
        if (pos < 1 || pos > n) {
            return;
        }
        auto it = allPeaks.upper_bound(pair<int, int>(pos, -1e9));
        if (it != allPeaks.end() && it->first == pos) {
            ans -= it->second;
            allPeaks.erase(it);
            allPeaks2.erase(-pos);
        }
        if (isMin(pos) && pos != 1 && pos != n) {
            ans -= v[pos];
            allPeaks.insert({pos, -v[pos]});
            allPeaks2.insert(-pos);
        } else if (isMax(pos)) {
            ans += v[pos];
            allPeaks.insert({pos, v[pos]});
            allPeaks2.insert(-pos);
        }
    }

    set<int> xx;
    void recalc(int pos) {
        xx.insert(pos);
        xx.insert(pos + 1);
        xx.insert(pos - 1);
        auto it = allPeaks.upper_bound(pair<int, int>(pos, 1e9));
        if (it != allPeaks.end()) {
            xx.insert(it->first);
        }
        it = allPeaks.upper_bound(pair<int, int>(pos, -1e9));
        if (it != allPeaks.end()) {
            xx.insert(it->first);
        }
        auto it2 = allPeaks2.upper_bound(-pos);
        if (it2 != allPeaks2.end()) {
            xx.insert(*it2);
        }
    }

    void doRecalc() {
        for (int x: xx) {
            recalcPeak(x);
        }
        xx.clear();
    }

    void Solve() {
        cin >> n >> q;
        v[0] = 0;
        v[n + 1] = 0;
       for (int i = 1; i <= n; ++i) {
           cin >> v[i];
       }
       for (int i = 1; i <= n; ++i) {
            if (isMin(i) && i != 1 && i != n) {
                allPeaks.insert({i, -v[i]});
                allPeaks2.insert(-i);
                ans -= v[i];
            }
            if (isMax(i)) {
                allPeaks.insert({i, v[i]});
                allPeaks2.insert(-i);
                ans += v[i];
            }
       }
       stringstream ss;
        ss << ans << "\n";
       while (q--) {
           int l, r;
           cin >> l >> r;
           swap(v[l], v[r]);
           recalc(l);
           recalc(r);
           doRecalc();
           ss << ans << "\n";
       }
       cout << ss.str();
    }
};

const int N = 8e5 + 10;

int main(){
#ifdef KVARK161
    freopen("input.txt", "r", stdin);
    /*
    freopen("capastaty_input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
    //*/
#else
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    srand(time(0));
#endif
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout.precision(15); cout.setf(ios::fixed); cerr.precision(15); cerr.setf(ios::fixed);

    int t = 1;
    cin >> t;
    for (int i = 0; i < t; ++i) {
        TTask task = {};
        task.Solve();
    }

    return 0;
}


// Smile please :)

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cassert>
#include <cctype>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <string>
#include <iomanip>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <random>
#include <deque>
#include <set>
#include <map>
#include <list>
#include <bitset>
#include <climits>
#include <unordered_map>
#include <unordered_set>
#include <fstream>
using namespace std;

#ifdef KVARK161
#define gdb(x) cerr << #x << " = " << x << endl;
#define gdb2(x, y) cerr << #x << " = " << x << "; " << #y << " = " << y << endl;
#else
#define gdb(...) (void(1));
#define gdb2(...) (void(1));
#endif

using i8 = char;
using ui8 = unsigned char;
using i16 = short;
using u16 = unsigned short;
using i32 = int;
using ui32 = unsigned int;
using i64 = long long int;
using ui64 = unsigned long long int;

#define ALL(v) (v.begin()), (v.end())
#define SZ(v) ((int)((v).size()))

const i64 INF = 1e18;

struct S {
    i64 endPos;
    i64 w;
    i64 h;
    friend bool operator<(const S& a, const S& b) {
        return a.endPos < b.endPos;
    }
};

int v[int(3e5 + 100)];
struct TTask {
    i64 n;
    i64 q;
    set<pair<int, int>> allPeaks;
    set<int> allPeaks2;
    i64 ans = 0;

    bool isMin(int pos) {
        return (pos == 1 || v[pos] < v[pos - 1]) && (pos == n || v[pos] < v[pos + 1]);
    }

    bool isMax(int pos) {
        return (pos == 1 || v[pos] > v[pos - 1]) && (pos == n || v[pos] > v[pos + 1]);
    }

    void recalcPeak(int pos) {
        if (pos < 1 || pos > n) {
            return;
        }
        auto it = allPeaks.upper_bound(pair<int, int>(pos, -1e9));
        if (it != allPeaks.end() && it->first == pos) {
            ans -= it->second;
            allPeaks.erase(it);
            allPeaks2.erase(-pos);
        }
        if (isMin(pos) && pos != 1 && pos != n) {
            ans -= v[pos];
            allPeaks.insert({pos, -v[pos]});
            allPeaks2.insert(-pos);
        } else if (isMax(pos)) {
            ans += v[pos];
            allPeaks.insert({pos, v[pos]});
            allPeaks2.insert(-pos);
        }
    }

    set<int> xx;
    void recalc(int pos) {
        xx.insert(pos);
        xx.insert(pos + 1);
        xx.insert(pos - 1);
        auto it = allPeaks.upper_bound(pair<int, int>(pos, 1e9));
        if (it != allPeaks.end()) {
            xx.insert(it->first);
        }
        it = allPeaks.upper_bound(pair<int, int>(pos, -1e9));
        if (it != allPeaks.end()) {
            xx.insert(it->first);
        }
        auto it2 = allPeaks2.upper_bound(-pos);
        if (it2 != allPeaks2.end()) {
            xx.insert(*it2);
        }
    }

    void doRecalc() {
        for (int x: xx) {
            recalcPeak(x);
        }
        xx.clear();
    }

    void Solve() {
        cin >> n >> q;
        v[0] = 0;
        v[n + 1] = 0;
       for (int i = 1; i <= n; ++i) {
           cin >> v[i];
       }
       for (int i = 1; i <= n; ++i) {
            if (isMin(i) && i != 1 && i != n) {
                allPeaks.insert({i, -v[i]});
                allPeaks2.insert(-i);
                ans -= v[i];
            }
            if (isMax(i)) {
                allPeaks.insert({i, v[i]});
                allPeaks2.insert(-i);
                ans += v[i];
            }
       }
       cout << ans << "\n";
       while (q--) {
           int l, r;
           cin >> l >> r;
           swap(v[l], v[r]);
           recalc(l);
           recalc(r);
           doRecalc();
           cout << ans << "\n";
       }
    }
};

const int N = 8e5 + 10;

int main(){
#ifdef KVARK161
    freopen("input.txt", "r", stdin);
    /*
    freopen("capastaty_input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
    //*/
#else
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    srand(time(0));
#endif
    ios_base::sync_with_stdio(0);

    int t = 1;
    cin >> t;
    for (int i = 0; i < t; ++i) {
        TTask task = {};
        task.Solve();
    }

    return 0;
}


// Smile please :)

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cassert>
#include <cctype>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <string>
#include <iomanip>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <random>
#include <deque>
#include <set>
#include <map>
#include <list>
#include <bitset>
#include <climits>
#include <unordered_map>
#include <unordered_set>
#include <fstream>
using namespace std;

#ifdef KVARK161
#define gdb(x) cerr << #x << " = " << x << endl;
#define gdb2(x, y) cerr << #x << " = " << x << "; " << #y << " = " << y << endl;
#else
#define gdb(...) (void(1));
#define gdb2(...) (void(1));
#endif

using i8 = char;
using ui8 = unsigned char;
using i16 = short;
using u16 = unsigned short;
using i32 = int;
using ui32 = unsigned int;
using i64 = long long int;
using ui64 = unsigned long long int;

#define ALL(v) (v.begin()), (v.end())
#define SZ(v) ((int)((v).size()))

const i64 INF = 1e18;

struct S {
    i64 endPos;
    i64 w;
    i64 h;
    friend bool operator<(const S& a, const S& b) {
        return a.endPos < b.endPos;
    }
};

struct TTask {
    i64 n;
    i64 q;
    vector<i64> v;
    set<pair<int, i64>> allPeaks;
    i64 ans = 0;

    bool isMin(int pos) {
        return (pos == 1 || v[pos] < v[pos - 1]) && (pos == n || v[pos] < v[pos + 1]);
    }

    bool isMax(int pos) {
        return (pos == 1 || v[pos] > v[pos - 1]) && (pos == n || v[pos] > v[pos + 1]);
    }

    void recalcPeak(int pos) {
        if (pos < 1 || pos > n) {
            return;
        }
        auto it = allPeaks.upper_bound(pair<int, i64>(pos, -1e18));
        if (it != allPeaks.end() && it->first == pos) {
            ans -= it->second;
            allPeaks.erase(it);
        }
        if (isMin(pos) && pos != 1 && pos != n) {
            ans -= v[pos];
            allPeaks.insert({pos, -v[pos]});
        } else if (isMax(pos)) {
            ans += v[pos];
            allPeaks.insert({pos, v[pos]});
        }
    }

    void recalc(int pos) {
        vector<int> xx;
        xx.push_back(pos);
        xx.push_back(pos + 1);
        xx.push_back(pos - 1);
        auto it = allPeaks.upper_bound(pair<int, i64>(pos, 1e18));
        if (it != allPeaks.end()) {
            xx.push_back(it->first);
        }
        if (it != allPeaks.begin()) {
            --it;
            xx.push_back(it->first);
        }
        if (it != allPeaks.begin()) {
            --it;
            xx.push_back(it->first);
        }
        for (int x: xx) {
            recalcPeak(x);
        }
    }

    void Solve() {
       cin >> n >> q;
       v.resize(n + 2, 0);
       for (int i = 1; i <= n; ++i) {
           cin >> v[i];
       }
       for (int i = 1; i <= n; ++i) {
            if (isMin(i) && i != 1 && i != n) {
                allPeaks.insert({i, -v[i]});
                ans -= v[i];
            }
            if (isMax(i)) {
                allPeaks.insert({i, v[i]});
                ans += v[i];
            }
       }
        cout << ans << endl;
       while (q--) {
           int l, r;
           cin >> l >> r;
           swap(v[l], v[r]);
           recalc(l);
           recalc(r);
           cout << ans << endl;
       }
    }
};

const int N = 8e5 + 10;

int main(){
#ifdef KVARK161
    freopen("input.txt", "r", stdin);
    /*
    freopen("capastaty_input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
    //*/
#else
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    srand(time(0));
#endif
    ios_base::sync_with_stdio(0);

    int t = 1;
    cin >> t;
    for (int i = 0; i < t; ++i) {
        TTask task = {};
        task.Solve();
    }

    return 0;
}


// Smile please :)

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cassert>
#include <cctype>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <string>
#include <iomanip>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <random>
#include <deque>
#include <set>
#include <map>
#include <list>
#include <bitset>
#include <climits>
#include <unordered_map>
#include <unordered_set>
#include <fstream>
using namespace std;

#ifdef KVARK161
#define gdb(x) cerr << #x << " = " << x << endl;
#define gdb2(x, y) cerr << #x << " = " << x << "; " << #y << " = " << y << endl;
#else
#define gdb(...) (void(1));
#define gdb2(...) (void(1));
#endif

using i8 = char;
using ui8 = unsigned char;
using i16 = short;
using u16 = unsigned short;
using i32 = int;
using ui32 = unsigned int;
using i64 = long long int;
using ui64 = unsigned long long int;

#define ALL(v) (v.begin()), (v.end())
#define SZ(v) ((int)((v).size()))

const i64 INF = 1e18;

struct S {
    i64 endPos;
    i64 w;
    i64 h;
    friend bool operator<(const S& a, const S& b) {
        return a.endPos < b.endPos;
    }
};

struct TTask {
    i64 n;
    i64 q;
    vector<i64> v;
    set<pair<int, i64>> allPeaks;
    i64 ans = 0;

    bool isMin(int pos) {
        return (pos == 1 || v[pos] < v[pos - 1]) && (pos == n || v[pos] < v[pos + 1]);
    }

    bool isMax(int pos) {
        return (pos == 1 || v[pos] > v[pos - 1]) && (pos == n || v[pos] > v[pos + 1]);
    }

    void recalcPeak(int pos) {
        if (pos < 1 || pos > n) {
            return;
        }
        auto it = allPeaks.upper_bound(pair<int, i64>(pos, -1e18));
        if (it != allPeaks.end() && it->first == pos) {
            ans -= it->second;
            allPeaks.erase(it);
        }
        if (isMin(pos) && pos != 1 && pos != n) {
            ans -= v[pos];
            allPeaks.insert({pos, -v[pos]});
        } else if (isMax(pos)) {
            ans += v[pos];
            allPeaks.insert({pos, v[pos]});
        }
    }

    set<int> xx;
    void recalc(int pos) {
        xx.insert(pos);
        xx.insert(pos + 1);
        xx.insert(pos - 1);
        auto it = allPeaks.upper_bound(pair<int, i64>(pos, 1e18));
        if (it != allPeaks.end()) {
            xx.insert(it->first);
        }
        if (it != allPeaks.begin()) {
            --it;
            xx.insert(it->first);
        }
        if (it != allPeaks.begin()) {
            --it;
            xx.insert(it->first);
        }
    }

    void doRecalc() {
        for (int x: xx) {
            recalcPeak(x);
        }
        xx.clear();
    }

    void Solve() {
        scanf("%ld %ld", &n, &q);
       v.resize(n + 2, 0);
       for (int i = 1; i <= n; ++i) {
           scanf("%ld", &v[i]);
       }
       for (int i = 1; i <= n; ++i) {
            if (isMin(i) && i != 1 && i != n) {
                allPeaks.insert({i, -v[i]});
                ans -= v[i];
            }
            if (isMax(i)) {
                allPeaks.insert({i, v[i]});
                ans += v[i];
            }
       }
        printf("%ld\n", ans);
       while (q--) {
           int l, r;
           cin >> l >> r;
           swap(v[l], v[r]);
           recalc(l);
           recalc(r);
           doRecalc();
           printf("%ld\n", ans);
       }
    }
};

const int N = 8e5 + 10;

int main(){
#ifdef KVARK161
    freopen("input.txt", "r", stdin);
    /*
    freopen("capastaty_input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
    //*/
#else
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    srand(time(0));
#endif
//    ios_base::sync_with_stdio(0);

    int t = 1;
    scanf("%d", &t);
//    cin >> t;
    for (int i = 0; i < t; ++i) {
        TTask task = {};
        task.Solve();
    }

    return 0;
}


// Smile please :)

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cassert>
#include <cctype>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <string>
#include <iomanip>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <random>
#include <deque>
#include <set>
#include <map>
#include <list>
#include <bitset>
#include <climits>
#include <unordered_map>
#include <unordered_set>
#include <fstream>
using namespace std;

#ifdef KVARK161
#define gdb(x) cerr << #x << " = " << x << endl;
#define gdb2(x, y) cerr << #x << " = " << x << "; " << #y << " = " << y << endl;
#else
#define gdb(...) (void(1));
#define gdb2(...) (void(1));
#endif

using i8 = char;
using ui8 = unsigned char;
using i16 = short;
using u16 = unsigned short;
using i32 = int;
using ui32 = unsigned int;
using i64 = long long int;
using ui64 = unsigned long long int;

#define ALL(v) (v.begin()), (v.end())
#define SZ(v) ((int)((v).size()))

const i64 INF = 1e18;

struct S {
    i64 endPos;
    i64 w;
    i64 h;
    friend bool operator<(const S& a, const S& b) {
        return a.endPos < b.endPos;
    }
};

int v[int(3e5 + 100)];
struct TTask {
    i64 n;
    i64 q;
    set<pair<int, int>> allPeaks;
    set<int> allPeaks2;
    i64 ans = 0;

    bool isMin(int pos) {
        return (pos == 1 || v[pos] < v[pos - 1]) && (pos == n || v[pos] < v[pos + 1]);
    }

    bool isMax(int pos) {
        return (pos == 1 || v[pos] > v[pos - 1]) && (pos == n || v[pos] > v[pos + 1]);
    }

    void recalcPeak(int pos) {
        if (pos < 1 || pos > n) {
            return;
        }
        auto it = allPeaks.upper_bound(pair<int, int>(pos, -1e9));
        if (it != allPeaks.end() && it->first == pos) {
            ans -= it->second;
            allPeaks.erase(it);
            allPeaks2.erase(-pos);
        }
        if (isMin(pos) && pos != 1 && pos != n) {
            ans -= v[pos];
            allPeaks.insert({pos, -v[pos]});
            allPeaks2.insert(-pos);
        } else if (isMax(pos)) {
            ans += v[pos];
            allPeaks.insert({pos, v[pos]});
            allPeaks2.insert(-pos);
        }
    }

    set<int> xx;
    void recalc(int pos) {
        xx.insert(pos);
        xx.insert(pos + 1);
        xx.insert(pos - 1);
        auto it = allPeaks.upper_bound(pair<int, int>(pos, 1e9));
        if (it != allPeaks.end()) {
            xx.insert(it->first);
        }
        it = allPeaks.upper_bound(pair<int, int>(pos, -1e9));
        if (it != allPeaks.end()) {
            xx.insert(it->first);
        }
        auto it2 = allPeaks2.upper_bound(-pos);
        if (it2 != allPeaks2.end()) {
            xx.insert(*it2);
        }
    }

    void doRecalc() {
        for (int x: xx) {
            recalcPeak(x);
        }
        xx.clear();
    }

    void Solve() {
        cin >> n >> q;
        v[0] = 0;
        v[n + 1] = 0;
       for (int i = 1; i <= n; ++i) {
           cin >> v[i];
       }
       for (int i = 1; i <= n; ++i) {
            if (isMin(i) && i != 1 && i != n) {
                allPeaks.insert({i, -v[i]});
                ans -= v[i];
            }
            if (isMax(i)) {
                allPeaks.insert({i, v[i]});
                ans += v[i];
            }
       }
       cout << ans << "\n";
       while (q--) {
           int l, r;
           cin >> l >> r;
           swap(v[l], v[r]);
           recalc(l);
           recalc(r);
           doRecalc();
           cout << ans << "\n";
       }
    }
};

const int N = 8e5 + 10;

int main(){
#ifdef KVARK161
    freopen("input.txt", "r", stdin);
    /*
    freopen("capastaty_input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
    //*/
#else
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    srand(time(0));
#endif
    ios_base::sync_with_stdio(0);

    int t = 1;
    cin >> t;
    for (int i = 0; i < t; ++i) {
        TTask task = {};
        task.Solve();
    }

    return 0;
}


//Ishaan Singh 2019CS10359
//tut
#include <iostream>
#include<sstream>
#include<vector>
#include<set>
#include<math.h>
#include<bitset>
#include<iomanip>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
typedef long long int ll;
typedef long double ld;
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//#define oset tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define fast                          \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL)
#define fo(i,n) for(int i=0;i<n;i++)
#define PI 3.141592654
#define all(v) (v).begin(), (v).end()
#define allr(v) (v).rbegin(), (v).rend()
#define pb push_back
#define mp make_pair
#define vb vector<bool>
#define vvb vector<vb>
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<ll>
#define vvl vector<vl>
#define pii pair<int,int>
#define vpi vector<pii>
#define unordered_set(T) unordered_set<T, custom_hash>
#define unordered_map(T1, T2) unordered_map<T1, T2, custom_hash>
#define f first
#define se second    // large prime for modulo
using namespace std;
const ll M=998244353;
ll fpow(ll a,ll b) {// function for binary exponentiation
    b%=(M-1);
    ll res= 1;
    while(b>0) {
        if (b&1){
            res = res * a%M;
        }
        a = a * a%M;
        b >>= 1 ;
    }
    return res;
}
ll inv(ll n){return n==1 ? n : M-(M/n)*inv(M%n)%M;}
/*
 ll spf[10000000+1];
bool primes[2000+5]={false};
 void sieve()  // sieve of eratosthenes
 {
     spf[1] = 1;
     primes[2]=true;
     for (ll i=2; i<2005; i++)
         spf[i] = i;
     for(ll i=4;i<2005;i+=2)
         spf[i]=2;
     for (ll i=3; i*i<2005; i+=2)
     {
         if (spf[i] == i)
         {
             primes[i]=true;
             for (ll j=i*i; j<2005; j+=i)
                 if (spf[j]==j)
                     spf[j] = i;
         }
     }
 }
map<ll,ll> gpf(ll n) // generates prime factors of a number in log(n) time with o(nloglogn) preprocessing time
 {
     map<ll,ll>v;
     ll curr = spf[n];
     ll cnt = 1;
     while (n > 1)
     {
         n/= spf[n];
         if (curr == spf[n])
         {
             cnt++;
             continue;
         }
         v.insert({curr,cnt});
         curr = spf[n];
         cnt = 1;
     }
     return v;
 }

    bool operat(pair<pii,ll>a,pair<pii,ll>b)
    {
        if (a.first.first == b.first.first){
            return a.first.second < b.first.second;
        }
        return a.first.first < b.first.first;
    }
ll gcd(int a,int b){return b ? gcd(b,a%b) :a;}
// returns gcd of two numbers in logarithmic time
map<ll,ll> prsqrt(ll n){                            // returns prime factors of a number in root n time complexity
    ll cnt=0;map<ll,ll>m;
    while (n % 2 == 0){cnt++;n = n/2;}
    if (cnt!=0){m.insert({2,cnt});}
    for (ll i=3;i<=sqrt(n);i+=2){cnt=0;while (n % i == 0){cnt++;n = n/i;}if(cnt!=0){m.insert({i,cnt});}}
    if (n > 2){m.insert({n,1});}
    return m;
}
ll lpfact(ll n,ll p){ll ans=0;while(n){ans+=n/p;n/=p;}return ans;}
//returns largest power of a prime p in n factorial
vector<int>tin(200000+5);vector<int>low(200000+5);
vector<int>adj[2*100000+5];
vpi bridges;
int timer=0;
bool visited[3*100000+5];
void bridge(int v,int p){                   //finding bridges in O(n+m) time
    visited[v]=true;
    tin[v]=low[v]=timer++;
    for(auto u:adj[v]){if (u==p){continue;}else if (visited[u]){low[v]=min(low[v],tin[u]);return;}
        else{bridge(u,v);low[v]=min(low[v],low[u]);if (low[u]>tin[v]){bridges.pb({u,v});}}}}
ll dyno1[1000001]={0};
ll dyno2[1000001]={0};
ll dynosum(int l,int r,ll dyno[]){
    if(l>r){return 0;}                  // fenwick tree sum query
    ll res=0,resl=0;
    for(int i=r;i>=0;i=(i & (i+1))-1){res+=dyno[i];}
     for(int i=l-1;i>=0;i=(i & (i+1))-1){resl+=dyno[i];}
           return res-resl;
}
void dynoupdate(int ind,ll val,ll dyno[]){                               //fenwick tree point update query
       for(int i=ind;i<1000001;i=(i|(i+1))){
           dyno[i]+=val;
       }
}
ll t[5*1000000]={0};
void build(int a[], int v, int tl, int tr) {
    if (tl == tr) {
        t[v] = a[tl];
    } else {
        int tm = (tl + tr) / 2;
        build(a, v*2, tl, tm);
        build(a, v*2+1, tm+1, tr);
        t[v] = 0;
    }
}

void update(int v, int tl, int tr, int l, int r, int add) {
    if (l > r)
        return;
    if (l == tl && r == tr) {
        t[v] += add;
    } else {
        int tm = (tl + tr) / 2;
        update(v*2, tl, tm, l, min(r, tm), add);
        update(v*2+1, tm+1, tr, max(l, tm+1), r, add);
    }
}

ll get(int v, int tl, int tr, int pos) {
    if (tl == tr)
        return t[v];
    int tm = (tl + tr) / 2;
    if (pos <= tm)
        return t[v] + get(v*2, tl, tm, pos);
    else
        return t[v] + get(v*2+1, tm+1, tr, pos);
}
ll sum(int v, int tl, int tr, int l, int r) {
    if (l > r)
        return 0;
    if (l == tl && r == tr) {
        return t[v];
    }
    int tm = (tl + tr) / 2;
    return sum(v*2, tl, tm, l, min(r, tm))
           + sum(v*2+1, tm+1, tr, max(l, tm+1), r);
}
 */
//ll g(int k1,int k2){ll ans=0;for(int oh=k1;oh<=k2;oh++){ans+=9*(81*(oh-1)*(oh-2)/2+9*(oh-1)+1);}return ans;}
int n;
ll a[3*100000+5];
bool lm(ll x){if(x==0){if(a[0]>a[1]){return 1;}}else if(x==n-1){if(a[n-1]>a[n-2]){return 1;}}else{if(a[x]>a[x-1] && a[x]>a[x+1]){return 1;}}return 0;}
bool lmi(ll x){if(x>0 && x<n-1){if(a[x]<a[x+1] && a[x]<a[x-1]){return 1;}}return 0;}
int main()
{
    int t;cin>>t;while(t--){
        int q;cin>>n>>q;
        fo(i,n){cin>>a[i];}
         ll ls=0,ms=0;
        for(int i=1;i<n-1;i++){
            if(a[i]>a[i-1] && a[i]>a[i+1]){ls+=a[i];}
            else if (a[i]<a[i-1] && a[i]<a[i+1]){ms+=a[i];}
        }
        if(n>1){if(a[0]>a[1]){ls+=a[0];}if(a[n-1]>a[n-2]){ls+=a[n-1];}}
        cout<<ls-ms<<endl;
        fo(i,q){
            ll l,r;cin>>l>>r;
            l--;r--;
            if(l==r){cout<<ls-ms<<endl;}
            else{
                if(lm(l)){ls-=a[l];}if(lmi(l)){ms-=a[l];}
            if(lm(l+1)){ls-=a[l+1];}if(lmi(l+1)){ms-=a[l+1];}
            if(l!=0){if(lm(l-1)){ls-=a[l-1];}if(lmi(l-1)){ms-=a[l-1];}}
            if(r!=n-1){if(lm(r+1)){ls-=a[r+1];}if(lmi(r+1)){ms-=a[r+1];}}
            if(r>l+1){if(lm(r)){ls-=a[r];}if(lmi(r)){ms-=a[r];}}
            if(r>l+2){if(lm(r-1)){ls-=a[r-1];}if(lmi(r-1)){ms-=a[r-1];}}
            ll temp=a[l];a[l]=a[r];a[r]=temp;
            if(lm(l)){ls+=a[l];}if(lmi(l)){ms+=a[l];}
            if(lm(l+1)){ls+=a[l+1];}if(lmi(l+1)){ms+=a[l+1];}
            if(l!=0){if(lm(l-1)){ls+=a[l-1];}if(lmi(l-1)){ms+=a[l-1];}}
            if(r!=n-1){if(lm(r+1)){ls+=a[r+1];}if(lmi(r+1)){ms+=a[r+1];}}
            if(r>l+1){if(lm(r)){ls+=a[r];}if(lmi(r)){ms+=a[r];}}
            if(r>l+2){if(lm(r-1)){ls+=a[r-1];}if(lmi(r-1)){ms+=a[r-1];}}
                cout<<ls-ms<<endl;
                }
                }
    }
    return 0;
}


//Ishaan Singh 2019CS10359
//tut
#include <iostream>
#include<sstream>
#include<vector>
#include<set>
#include<math.h>
#include<bitset>
#include<iomanip>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
typedef long long int ll;
typedef long double ld;
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//#define oset tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define fast                          \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL)
#define fo(i,n) for(int i=0;i<n;i++)
#define PI 3.141592654
#define all(v) (v).begin(), (v).end()
#define allr(v) (v).rbegin(), (v).rend()
#define pb push_back
#define mp make_pair
#define vb vector<bool>
#define vvb vector<vb>
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<ll>
#define vvl vector<vl>
#define pii pair<int,int>
#define vpi vector<pii>
#define unordered_set(T) unordered_set<T, custom_hash>
#define unordered_map(T1, T2) unordered_map<T1, T2, custom_hash>
#define f first
#define se second    // large prime for modulo
using namespace std;
const ll M=998244353;
ll fpow(ll a,ll b) {// function for binary exponentiation
    b%=(M-1);
    ll res= 1;
    while(b>0) {
        if (b&1){
            res = res * a%M;
        }
        a = a * a%M;
        b >>= 1 ;
    }
    return res;
}
ll inv(ll n){return n==1 ? n : M-(M/n)*inv(M%n)%M;}
/*
 ll spf[10000000+1];
bool primes[2000+5]={false};
 void sieve()  // sieve of eratosthenes
 {
     spf[1] = 1;
     primes[2]=true;
     for (ll i=2; i<2005; i++)
         spf[i] = i;
     for(ll i=4;i<2005;i+=2)
         spf[i]=2;
     for (ll i=3; i*i<2005; i+=2)
     {
         if (spf[i] == i)
         {
             primes[i]=true;
             for (ll j=i*i; j<2005; j+=i)
                 if (spf[j]==j)
                     spf[j] = i;
         }
     }
 }
map<ll,ll> gpf(ll n) // generates prime factors of a number in log(n) time with o(nloglogn) preprocessing time
 {
     map<ll,ll>v;
     ll curr = spf[n];
     ll cnt = 1;
     while (n > 1)
     {
         n/= spf[n];
         if (curr == spf[n])
         {
             cnt++;
             continue;
         }
         v.insert({curr,cnt});
         curr = spf[n];
         cnt = 1;
     }
     return v;
 }

    bool operat(pair<pii,ll>a,pair<pii,ll>b)
    {
        if (a.first.first == b.first.first){
            return a.first.second < b.first.second;
        }
        return a.first.first < b.first.first;
    }
ll gcd(int a,int b){return b ? gcd(b,a%b) :a;}
// returns gcd of two numbers in logarithmic time
map<ll,ll> prsqrt(ll n){                            // returns prime factors of a number in root n time complexity
    ll cnt=0;map<ll,ll>m;
    while (n % 2 == 0){cnt++;n = n/2;}
    if (cnt!=0){m.insert({2,cnt});}
    for (ll i=3;i<=sqrt(n);i+=2){cnt=0;while (n % i == 0){cnt++;n = n/i;}if(cnt!=0){m.insert({i,cnt});}}
    if (n > 2){m.insert({n,1});}
    return m;
}
ll lpfact(ll n,ll p){ll ans=0;while(n){ans+=n/p;n/=p;}return ans;}
//returns largest power of a prime p in n factorial
vector<int>tin(200000+5);vector<int>low(200000+5);
vector<int>adj[2*100000+5];
vpi bridges;
int timer=0;
bool visited[3*100000+5];
void bridge(int v,int p){                   //finding bridges in O(n+m) time
    visited[v]=true;
    tin[v]=low[v]=timer++;
    for(auto u:adj[v]){if (u==p){continue;}else if (visited[u]){low[v]=min(low[v],tin[u]);return;}
        else{bridge(u,v);low[v]=min(low[v],low[u]);if (low[u]>tin[v]){bridges.pb({u,v});}}}}
ll dyno1[1000001]={0};
ll dyno2[1000001]={0};
ll dynosum(int l,int r,ll dyno[]){
    if(l>r){return 0;}                  // fenwick tree sum query
    ll res=0,resl=0;
    for(int i=r;i>=0;i=(i & (i+1))-1){res+=dyno[i];}
     for(int i=l-1;i>=0;i=(i & (i+1))-1){resl+=dyno[i];}
           return res-resl;
}
void dynoupdate(int ind,ll val,ll dyno[]){                               //fenwick tree point update query
       for(int i=ind;i<1000001;i=(i|(i+1))){
           dyno[i]+=val;
       }
}
ll t[5*1000000]={0};
void build(int a[], int v, int tl, int tr) {
    if (tl == tr) {
        t[v] = a[tl];
    } else {
        int tm = (tl + tr) / 2;
        build(a, v*2, tl, tm);
        build(a, v*2+1, tm+1, tr);
        t[v] = 0;
    }
}

void update(int v, int tl, int tr, int l, int r, int add) {
    if (l > r)
        return;
    if (l == tl && r == tr) {
        t[v] += add;
    } else {
        int tm = (tl + tr) / 2;
        update(v*2, tl, tm, l, min(r, tm), add);
        update(v*2+1, tm+1, tr, max(l, tm+1), r, add);
    }
}

ll get(int v, int tl, int tr, int pos) {
    if (tl == tr)
        return t[v];
    int tm = (tl + tr) / 2;
    if (pos <= tm)
        return t[v] + get(v*2, tl, tm, pos);
    else
        return t[v] + get(v*2+1, tm+1, tr, pos);
}
ll sum(int v, int tl, int tr, int l, int r) {
    if (l > r)
        return 0;
    if (l == tl && r == tr) {
        return t[v];
    }
    int tm = (tl + tr) / 2;
    return sum(v*2, tl, tm, l, min(r, tm))
           + sum(v*2+1, tm+1, tr, max(l, tm+1), r);
}
 */
//ll g(int k1,int k2){ll ans=0;for(int oh=k1;oh<=k2;oh++){ans+=9*(81*(oh-1)*(oh-2)/2+9*(oh-1)+1);}return ans;}
int n;
ll a[3*100000+5];
bool lm(ll x){if(x==0){if(a[0]>a[1]){return 1;}}else if(x==n-1){if(a[n-1]>a[n-2]){return 1;}}else{if(a[x]>a[x-1] && a[x]>a[x+1]){return 1;}}return 0;}
bool lmi(ll x){if(x>0 && x<n-1){if(a[x]<a[x+1] && a[x]<a[x-1]){return 1;}}return 0;}
int main()
{
    int t;cin>>t;while(t--){
        int q;cin>>n>>q;
        fo(i,n){cin>>a[i];}
        //set<ll>locma;
        //set<ll>locmi;
         ll ls=0,ms=0;
        for(int i=1;i<n-1;i++){
            if(a[i]>a[i-1] && a[i]>a[i+1]){ls+=a[i];}//locma.insert(i);}
            else if (a[i]<a[i-1] && a[i]<a[i+1]){ms+=a[i];}//locmi.insert(i);}
        }
        if(n>1){if(a[0]>a[1]){ls+=a[0];}if(a[n-1]>a[n-2]){ls+=a[n-1];}}//locma.insert(0);}if(a[n-1]>a[n-2]){locma.insert(n-1);}}
        //for(auto u:locma){ls+=a[u];}for(auto u:locmi){ms+=a[u];}
        cout<<ls-ms<<endl;
        fo(i,q){
            ll l,r;cin>>l>>r;
            l--;r--;
            if(l==r){cout<<ls-ms<<endl;}
            else{
                if(lm(l)){ls-=a[l];}if(lmi(l)){ms-=a[l];}
            if(lm(l+1)){ls-=a[l+1];}if(lmi(l+1)){ms-=a[l+1];}
            if(l!=0){if(lm(l-1)){ls-=a[l-1];}if(lmi(l-1)){ms-=a[l-1];}}
            if(r!=n-1){if(lm(r+1)){ls-=a[r+1];}if(lmi(r+1)){ms-=a[r+1];}}
            if(r>l+1){if(lm(r)){ls-=a[r];}if(lmi(r)){ms-=a[r];}}
            if(r>l+2){if(lm(r)){ls-=a[r];}if(lmi(r)){ms-=a[r];}}
            ll temp=a[l];a[l]=a[r];a[r]=temp;
            if(lm(l)){ls+=a[l];}if(lmi(l)){ms+=a[l];}
            if(lm(l+1)){ls+=a[l+1];}if(lmi(l+1)){ms+=a[l+1];}
            if(l!=0){if(lm(l-1)){ls+=a[l-1];}if(lmi(l-1)){ms+=a[l-1];}}
            if(r!=n-1){if(lm(r+1)){ls+=a[r+1];}if(lmi(r+1)){ms+=a[r+1];}}
            if(r>l+1){if(lm(r)){ls+=a[r];}if(lmi(r)){ms+=a[r];}}
            if(r>l+2){if(lm(r)){ls+=a[r];}if(lmi(r)){ms+=a[r];}}
                cout<<ls-ms<<endl;
                }
                }
    }
    return 0;
}


//Ishaan Singh 2019CS10359
//tut
#include <iostream>
#include<sstream>
#include<vector>
#include<set>
#include<math.h>
#include<bitset>
#include<iomanip>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
typedef long long int ll;
typedef long double ld;
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//#define oset tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define fast                          \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL)
#define fo(i,n) for(int i=0;i<n;i++)
#define PI 3.141592654
#define all(v) (v).begin(), (v).end()
#define allr(v) (v).rbegin(), (v).rend()
#define pb push_back
#define mp make_pair
#define vb vector<bool>
#define vvb vector<vb>
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<ll>
#define vvl vector<vl>
#define pii pair<int,int>
#define vpi vector<pii>
#define unordered_set(T) unordered_set<T, custom_hash>
#define unordered_map(T1, T2) unordered_map<T1, T2, custom_hash>
#define f first
#define se second    // large prime for modulo
using namespace std;
const ll M=998244353;
ll fpow(ll a,ll b) {// function for binary exponentiation
    b%=(M-1);
    ll res= 1;
    while(b>0) {
        if (b&1){
            res = res * a%M;
        }
        a = a * a%M;
        b >>= 1 ;
    }
    return res;
}
ll inv(ll n){return n==1 ? n : M-(M/n)*inv(M%n)%M;}
/*
 ll spf[10000000+1];
bool primes[2000+5]={false};
 void sieve()  // sieve of eratosthenes
 {
     spf[1] = 1;
     primes[2]=true;
     for (ll i=2; i<2005; i++)
         spf[i] = i;
     for(ll i=4;i<2005;i+=2)
         spf[i]=2;
     for (ll i=3; i*i<2005; i+=2)
     {
         if (spf[i] == i)
         {
             primes[i]=true;
             for (ll j=i*i; j<2005; j+=i)
                 if (spf[j]==j)
                     spf[j] = i;
         }
     }
 }
map<ll,ll> gpf(ll n) // generates prime factors of a number in log(n) time with o(nloglogn) preprocessing time
 {
     map<ll,ll>v;
     ll curr = spf[n];
     ll cnt = 1;
     while (n > 1)
     {
         n/= spf[n];
         if (curr == spf[n])
         {
             cnt++;
             continue;
         }
         v.insert({curr,cnt});
         curr = spf[n];
         cnt = 1;
     }
     return v;
 }

    bool operat(pair<pii,ll>a,pair<pii,ll>b)
    {
        if (a.first.first == b.first.first){
            return a.first.second < b.first.second;
        }
        return a.first.first < b.first.first;
    }
ll gcd(int a,int b){return b ? gcd(b,a%b) :a;}
// returns gcd of two numbers in logarithmic time
map<ll,ll> prsqrt(ll n){                            // returns prime factors of a number in root n time complexity
    ll cnt=0;map<ll,ll>m;
    while (n % 2 == 0){cnt++;n = n/2;}
    if (cnt!=0){m.insert({2,cnt});}
    for (ll i=3;i<=sqrt(n);i+=2){cnt=0;while (n % i == 0){cnt++;n = n/i;}if(cnt!=0){m.insert({i,cnt});}}
    if (n > 2){m.insert({n,1});}
    return m;
}
ll lpfact(ll n,ll p){ll ans=0;while(n){ans+=n/p;n/=p;}return ans;}
//returns largest power of a prime p in n factorial
vector<int>tin(200000+5);vector<int>low(200000+5);
vector<int>adj[2*100000+5];
vpi bridges;
int timer=0;
bool visited[3*100000+5];
void bridge(int v,int p){                   //finding bridges in O(n+m) time
    visited[v]=true;
    tin[v]=low[v]=timer++;
    for(auto u:adj[v]){if (u==p){continue;}else if (visited[u]){low[v]=min(low[v],tin[u]);return;}
        else{bridge(u,v);low[v]=min(low[v],low[u]);if (low[u]>tin[v]){bridges.pb({u,v});}}}}
ll dyno1[1000001]={0};
ll dyno2[1000001]={0};
ll dynosum(int l,int r,ll dyno[]){
    if(l>r){return 0;}                  // fenwick tree sum query
    ll res=0,resl=0;
    for(int i=r;i>=0;i=(i & (i+1))-1){res+=dyno[i];}
     for(int i=l-1;i>=0;i=(i & (i+1))-1){resl+=dyno[i];}
           return res-resl;
}
void dynoupdate(int ind,ll val,ll dyno[]){                               //fenwick tree point update query
       for(int i=ind;i<1000001;i=(i|(i+1))){
           dyno[i]+=val;
       }
}
ll t[5*1000000]={0};
void build(int a[], int v, int tl, int tr) {
    if (tl == tr) {
        t[v] = a[tl];
    } else {
        int tm = (tl + tr) / 2;
        build(a, v*2, tl, tm);
        build(a, v*2+1, tm+1, tr);
        t[v] = 0;
    }
}

void update(int v, int tl, int tr, int l, int r, int add) {
    if (l > r)
        return;
    if (l == tl && r == tr) {
        t[v] += add;
    } else {
        int tm = (tl + tr) / 2;
        update(v*2, tl, tm, l, min(r, tm), add);
        update(v*2+1, tm+1, tr, max(l, tm+1), r, add);
    }
}

ll get(int v, int tl, int tr, int pos) {
    if (tl == tr)
        return t[v];
    int tm = (tl + tr) / 2;
    if (pos <= tm)
        return t[v] + get(v*2, tl, tm, pos);
    else
        return t[v] + get(v*2+1, tm+1, tr, pos);
}
ll sum(int v, int tl, int tr, int l, int r) {
    if (l > r)
        return 0;
    if (l == tl && r == tr) {
        return t[v];
    }
    int tm = (tl + tr) / 2;
    return sum(v*2, tl, tm, l, min(r, tm))
           + sum(v*2+1, tm+1, tr, max(l, tm+1), r);
}
 */
//ll g(int k1,int k2){ll ans=0;for(int oh=k1;oh<=k2;oh++){ans+=9*(81*(oh-1)*(oh-2)/2+9*(oh-1)+1);}return ans;}
int main()
{
    int t;cin>>t;while(t--){
        int n,q;cin>>n>>q;
        ll a[n];fo(i,n){cin>>a[i];}
        set<ll>locma;
        set<ll>locmi;
        for(int i=1;i<n-1;i++){
            if(a[i]>a[i-1] && a[i]>a[i+1]){locma.insert(i);}
            else if (a[i]<a[i-1] && a[i]<a[i+1]){locmi.insert(i);}
        }
        if(n>1){if(a[0]>a[1]){locma.insert(0);}if(a[n-1]>a[n-2]){locma.insert(n-1);}}
        ll ls=0,ms=0;
        for(auto u:locma){ls+=a[u];}for(auto u:locmi){ms+=a[u];}
        cout<<ls-ms<<endl;
        fo(i,q){
            ll l,r;cin>>l>>r;
            l--;r--;
            if(l==r){cout<<ls-ms<<endl;}
            else{
                if(locma.find(l)!=locma.end()){locma.erase(l);ls-=a[l];}
                 if(locma.find(l+1)!=locma.end()){locma.erase(l+1);ls-=a[l+1];}
                if(locmi.find(l+1)!=locmi.end()){locmi.erase(l+1);ms-=a[l+1];}
            if(l!=0){
            if(locmi.find(l-1)!=locmi.end()){locmi.erase(l-1);ms-=a[l-1];}
                if(locma.find(l-1)!=locma.end()){locma.erase(l-1);ls-=a[l-1];}
            }
            if(r!=n-1){
                if(locma.find(r+1)!=locma.end()){locma.erase(r+1);ls-=a[r+1];}
                if(locmi.find(r+1)!=locmi.end()){locmi.erase(r+1);ms-=a[r+1];}
            }
            if(r>l+1){
                    if(locma.find(r)!=locma.end()){locma.erase(r);ls-=a[r];}
                    if(locmi.find(r)!=locmi.end()){locmi.erase(r);ms-=a[r];}
            }
            if(r>l+2){
                if(locma.find(r-1)!=locma.end()){locma.erase(r-1);ls-=a[r-1];}
                if(locmi.find(r-1)!=locmi.end()){locmi.erase(r-1);ms-=a[r-1];}
            }
            ll temp=a[l];
            a[l]=a[r];
            a[r]=temp;
                if(l+1==n-1){
                if(a[l+1]>a[l]){locma.insert(l+1);ls+=a[l+1];}
                }
            else{
            if(a[l+1]>a[l] && a[l+1]>a[l+2]){locma.insert(l+1);ls+=a[l+1];}else if (a[l+1]<a[l] && a[l+1]<a[l+2]){locmi.insert(l+1);ms+=a[l+1];}
            }
                if(l==0){if(a[l]>a[l+1]){locma.insert(l);ls+=a[l];}
                }
                else{if(a[l]>a[l-1] && a[l]>a[l+1]){locma.insert(l);ls+=a[l];}else if (a[l]<a[l-1] && a[l]<a[l+1]){locmi.insert(l);ms+=a[l];}}
                if(l==1){if(a[0]>a[1]){locma.insert(0);ls+=a[0];}}
                else{if(a[l-1]>a[l-2] && a[l-1]>a[l]){locma.insert(l-1);ls+=a[l-1];}else if (a[l-1]<a[l-2] && a[l-1]<a[l]){locmi.insert(l-1);ms+=a[l-1];}}
                if(r!=n-1){
                    if(r+1==n-1){
                        if(a[n-1]>a[n-2]){locma.insert(n-1);ls+=a[n-1];}
                    }
                    else{
                        if(a[r+1]>a[r] && a[r+1]>a[r+2]){locma.insert(r+1);ls+=a[r+1];}else if (a[r+1]<a[r] && a[r+1]<a[r+2]){locmi.insert(r+1);ms+=a[r+1];}
                    }
                }
                if(r>l+1){
                    if(r==n-1){
                        if(a[n-1]>a[n-2]){locma.insert(n-1);ls+=a[n-1];}
                    }
                    else{
                        if(a[r]>a[r-1] && a[r]>a[r+1]){locma.insert(r);ls+=a[r];}else if (a[r]<a[r-1] && a[r]<a[r+1]){locmi.insert(r);ms+=a[r];}
                    }
                }
                if(r>l+2){
                      if(a[r-1]>a[r-2] && a[r-1]>a[r]){locma.insert(r-1);ls+=a[r-1];}else if (a[r-1]<a[r-2] && a[r-1]<a[r]){locmi.insert(r-1);ms+=a[r-1];}
                }
                cout<<ls-ms<<endl;
                }
                }
    }
    return 0;
}