#include <bits/stdc++.h>

using namespace std;
using ll = long long;

constexpr ll infLL = 3e18;

void solve() {
    int n, m;
    cin >> n >> m;
    vector<int> X(m);
    for (int i = 0; i < m; ++i) {
        cin >> X[i];
    }
    reverse(X.begin(), X.end());
    ll ans = 0;
    vector<int> bal(m), bracket(m);
    for (int i = 0; i < m; ++i) {
        if (X[i] >= 0) {
            bracket[i] = 1;
        } else {
            bracket[i] = -1;
        }
        bal[i] = (i > 0 ? bal[i - 1] : 0) + bracket[i];
        ans += bracket[i] * X[i];
        if (bal[i] > n) {
            pair<ll, int> newBest{-infLL, 0};
            for (int j = i; j >= 0; --j) {
                if (bal[j] <= 1) {
                    break;
                }
                if (bracket[j] == 1) {
                    ll cur = ans - 2 * X[j];
                    newBest = max(newBest, pair(cur, j));
                }
            }
            auto [newb, j] = newBest;
            ans = newb;
            bracket[j] *= -1;
            for (int k = j; k <= i; ++k) {
                bal[k] -= 2;
            }
        } else if (bal[i] < 0) {
            pair<ll, int> newBest{-infLL, 0};
            for (int j = i; j >= 0; --j) {
                if (bal[j] >= n - 1) {
                    break;
                }
                if (bracket[j] == -1) {
                    ll cur = ans + 2 * X[j];
                    newBest = max(newBest, pair(cur, j));
                }
            }
            auto [newb, j] = newBest;
            ans = newb;
            bracket[j] *= -1;
            for (int k = j; k <= i; ++k) {
                bal[k] += 2;
            }
        }
    }
    cout << ans << '\n';
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int test = 1;
    cin >> test;
    while (test--) {
        solve();
    }

    return 0;
}