#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define int long long
#define db long double

using namespace std;

vector<int> tree, p, ph;

void init(int n, int l, int r) {
    if (r - l == 1) {
        tree[n] = p[l];
        return;
    }

    int m = (l + r) / 2;
    init(2 * n + 1, l, m);
    init(2 * n + 2, m, r);

    tree[n] = max(tree[2 * n + 1], tree[2 * n + 2]);
}

void push(int n, int l, int r) {
    if (!ph[n]) return;

    tree[n] = ph[n];

    if (r - l > 1) {
        ph[2 * n + 1] += ph[n];
        ph[2 * n + 2] += ph[n];
    }

    ph[n] = 0;
}

int query(int n, int l, int r, int ql, int qr) {
    push(n, l, r);

    if (ql >= r || l >= qr) return LLONG_MAX;
    if (ql <= l && r <= qr) return tree[n];

    int m = (l + r) / 2;
    return max(query(2 * n + 1, l, m, ql, qr), query(2 * n + 2, m, r, ql, qr));
}

void update(int n, int l, int r, int ql, int qr, int val) {
    push(n, l, r);

    if (ql >= r || l >= qr) return;
    if (ql <= l && r <= qr) {
        ph[n] += val;
        push(n, l, r);
        return;
    }

    int m = (l + r) / 2;
    update(2 * n + 1, l, m, ql, qr, val);
    update(2 * n + 2, m, r, ql, qr, val);

    tree[n] = max(tree[2 * n + 1], tree[2 * n + 2]);
}

vector<int> tree1, a1;

void init1(int n, int l, int r) {
    if (r - l == 1) {
        tree1[n] = a1[l];
        return;
    }

    int m = (l + r) / 2;
    init1(2 * n + 1, l, m);
    init1(2 * n + 2, m, r);

    tree1[n] = min(tree1[2 * n + 1], tree1[2 * n + 2]);
}

int query1(int n, int l, int r, int ql, int qr) {
    if (ql >= r || l >= qr) return LLONG_MAX;
    if (ql <= l && r <= qr) return tree1[n];

    int m = (l + r) / 2;
    return min(query1(2 * n + 1, l, m, ql, qr), query1(2 * n + 2, m, r, ql, qr));
}

void update1(int n, int l, int r, int ql, int val) {
    if (ql >= r || l > ql) return;
    if (r - l == 1) {
        tree1[n] =  LLONG_MAX;
        return;
    }

    int m = (l + r) / 2;
    update1(2 * n + 1, l, m, ql, val);
    update1(2 * n + 2, m, r, ql, val);

    tree1[n] = min(tree1[2 * n + 1], tree1[2 * n + 2]);

}

void solve() {
    int n, m, sm = 0; cin >> n >> m;

    vector<int> a(m); p.resize(m), a1.resize(m);
    unordered_map<int, deque<int>> mp;
    for (int i = 0; i < m; ++i) {
        cin >> a[i];
        a1[m - i - 1] = a[i];
        mp[a[i]].push_back(m - i - 1);

        sm += a[i];
        p[m - i - 1] = a[i];
    }

    tree1.resize(4 * m);
    init1(0, 0, m);

    tree.resize(4 * m), ph.assign(4 * m, 0);
    init(0, 0, m);

    reverse(a.begin(), a.end());

    int cnt = n, mx = 0, ans = 0;
    for (int i = 0; i < m; ++i) {
        for (int j = 0; j < cnt; ++j) {
            int val = query(0, 0, m, 0, m);

            if (val == LLONG_MAX || val == LLONG_MIN) break;

            ans += val;
            mx = max(mp[val].back() + 1, mx);

            int idx = mp[val].back();
            update(0, 0, m, idx, idx + 1, LLONG_MIN);
            update1(0, 0, m, idx, LLONG_MAX);
        }

        cnt = 0;

        int val = query1(0, 0, m, 0, mx);

        if (val == LLONG_MAX || val == LLONG_MIN) break;

        ans -= val;
        mx = max(mp[val].front() + 1, mx);

        ++cnt;

        int idx = mp[val].front();
        update(0, 0, m, idx, idx + 1, LLONG_MIN);
        update1(0, 0, m, idx, LLONG_MAX);
    }

    cout << ans;
}

signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    int tt; cin >> tt;
    while (tt--) {
        solve();
        cout << "\n";
    }
}