#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <vector>
#include <map>
#include <stack>
#include <set>
using namespace std;

typedef long long ll;

#define pii pair<ll, ll>
#define mk make_pair
const ll N = 1e6 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e9 + 10;

ll read()
{
    ll x = 0, f = 1;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-')
            f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9')
        x = (x << 1) + (x << 3) + c - '0', c = getchar();
    return x * f;
}

struct segment
{
    ll sum[N << 2], siz[N << 2];
    void pushup(ll pos)
    {
        sum[pos] = sum[pos << 1 | 1] + sum[pos << 1];
        siz[pos] = siz[pos << 1 | 1] + siz[pos << 1];
    }
    void update(ll v, ll w, ll pos, ll l, ll r)
    {
        if (l == r)
        {
            sum[pos] += w;
            if (w < 0)
                siz[pos] -= 1;
            else
                siz[pos] += 1;
            return;
        }
        ll mid = (l + r) >> 1;
        if (v <= mid)
        {
            update(v, w, pos << 1, l, mid);
        }
        else
            update(v, w, pos << 1 | 1, mid + 1, r);
        pushup(pos);
    }
    ll query(ll k, ll pos, ll l, ll r)
    {
        if (siz[pos] == 0)
            return 0;
        if (l == r)
            return sum[pos];
        ll mid = (l + r) >> 1;
        if (k <= siz[pos << 1])
        {
            return query(k, pos << 1, l, mid);
        }
        else
        {
            return sum[pos << 1] + query(k - siz[pos << 1], pos << 1 | 1, mid + 1, r);
        }
    }
} t;
pii p[4][N];

ll cnt[4];
ll li[N], num;
ll sum[4][N];
ll getid(ll x)
{
    ll o = lower_bound(li + 1, li + 1 + num, x) - li;
    return o;
}
int main()
{
    ll n = read(), m = read(), k = read();

    for (ll i = 1; i <= n; i++)
    {
        ll t = read(), x = read(), y = read();
        if (x == 1 && y == 0)
            p[0][++cnt[0]] = mk(t, i);
        if (x == 0 && y == 1)
            p[1][++cnt[1]] = mk(t, i);
        if (x == 1 && y == 1)
            p[2][++cnt[2]] = mk(t, i);
        if (x == 0 && y == 0)
            p[3][++cnt[3]] = mk(t, i);
        li[++num] = t;
    }
    sort(1 + li, 1 + li + num);
    num = unique(1 + li, 1 + li + num) - li - 1;
    for (ll i = 0; i <= 2; i++)
    {
        sort(1 + p[i], 1 + p[i] + cnt[i]);
        for (ll j = 1; j <= cnt[i]; j++)
            sum[i][j] = sum[i][j - 1] + p[i][j].first;
    }
    for (ll i = 2; i <= 3; i++)
        for (ll j = 1; j <= cnt[i]; j++)
        {
            t.update(getid(p[i][j].first), p[i][j].first, 1, 1, num);
        }

    ll ans = 1e18;
    ll t1 = cnt[1];
    ll t0 = cnt[0];
    ll k2 = -1;
    for (ll j = 0; j <= cnt[2]; j++)
    {
        if (j != 0)
            t.update(getid(p[2][j].first), -p[2][j].first, 1, 1, num);
        ll k1 = max(0, k - j);
        ll k0 = max(0, k - j);
        while (t1 > k1)
        {
            t.update(getid(p[1][t1].first), p[1][t1].first, 1, 1, num);
            t1--;
        }
        while (t0 > k0)
        {
            t.update(getid(p[0][t0].first), p[0][t0].first, 1, 1, num);
            t0--;
        }
        if (k1 > cnt[1] || k0 > cnt[0])
            continue;
        ll o = m - k1 - k0 - j;
        ll res = sum[1][k1] + sum[0][k0] + sum[2][j];
        if (o < 0)
            continue;
        if (t.siz[1] < o)
            continue;
        if (o > 0)
        {

            res += t.query(o, 1, 1, num);
        }

        if (ans > res)
        {
            k2 = j;
            ans = res;
        }
    }
    if (ans == 1e18)
        cout << -1 << endl;
    else
    {
        cout << ans << endl;
        ll k1 = max(0, k - k2);
        ll k0 = max(0, k - k2);
        vector<ll> tmp;
        for (ll i = 1; i <= k2; i++)
            tmp.push_back(p[2][i].second);
        for (ll i = 1; i <= k1; i++)
            tmp.push_back(p[1][i].second);
        for (ll i = 1; i <= k0; i++)
            tmp.push_back(p[0][i].second);

        vector<pii> o;
        for (ll i = 1; i <= cnt[3]; i++)
            o.push_back(p[3][i]);
        for (ll i = k2 + 1; i <= cnt[2]; i++)
            o.push_back(p[2][i]);
        for (ll i = k1 + 1; i <= cnt[1]; i++)
            o.push_back(p[1][i]);
        for (ll i = k0 + 1; i <= cnt[0]; i++)
            o.push_back(p[0][i]);
        sort(o.begin(), o.end());
        for (ll i = 0; i < m - k1 - k0 - k2; i++)
            tmp.push_back(o[i].second);
        for (ll x : tmp)
            cout << x << " ";
        cout << endl;
    }
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <vector>
#include <map>
#include <stack>
#include <set>
using namespace std;

typedef long long ll;

#define pii pair<ll, ll>
#define mk make_pair
const ll N = 1e6 + 10;
const ll mod = 1e9 + 7;

ll read()
{
    ll x = 0, f = 1;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-')
            f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9')
        x = (x << 1) + (x << 3) + c - '0', c = getchar();
    return x * f;
}

struct segment
{
    ll sum[N << 2], siz[N << 2];
    void pushup(ll pos)
    {
        sum[pos] = sum[pos << 1 | 1] + sum[pos << 1];
        siz[pos] = siz[pos << 1 | 1] + siz[pos << 1];
    }
    void update(ll v, ll w, ll pos, ll l, ll r)
    {
        if (l == r)
        {
            sum[pos] += w;
            if (w < 0)
                siz[pos] -= 1;
            if (w > 0)
                siz[pos] += 1;
            return;
        }
        ll mid = (l + r) >> 1;
        if (v <= mid)
        {
            update(v, w, pos << 1, l, mid);
        }
        else
            update(v, w, pos << 1 | 1, mid + 1, r);
        pushup(pos);
    }
    ll query(int k, int pos, int l, int r)
    {

        if (k == 0)
            return 0;
        if (l == r)
            return k * l;
        int mid = (l + r) >> 1;
        if (siz[pos << 1] <= k)
            return sum[pos << 1] + query(k - siz[pos << 1], pos << 1 | 1, mid + 1, r);
        else
        {
            return query(k, pos << 1, l, mid);
        }
    }
} t;
pii p[4][N];

ll cnt[4];
ll li[N], num;
ll sum[4][N];

ll z[N];
int main()
{
    ll n = read(), m = read(), k = read();
    int num = 1e4;
    for (ll i = 1; i <= n; i++)
    {
        ll t = read(), x = read(), y = read();
        z[i] = t;
        if (x == 1 && y == 0)
            p[0][++cnt[0]] = mk(t, i);
        if (x == 0 && y == 1)
            p[1][++cnt[1]] = mk(t, i);
        if (x == 1 && y == 1)
            p[2][++cnt[2]] = mk(t, i);
        if (x == 0 && y == 0)
            p[3][++cnt[3]] = mk(t, i);
    }

    for (ll i = 0; i <= 2; i++)
    {
        sort(1 + p[i], 1 + p[i] + cnt[i]);
        for (ll j = 1; j <= cnt[i]; j++)
            sum[i][j] = sum[i][j - 1] + p[i][j].first;
    }
    for (ll i = 2; i <= 3; i++)
        for (ll j = 1; j <= cnt[i]; j++)
        {
            t.update(p[i][j].first, p[i][j].first, 1, 1, num);
        }

    ll ans = 1e18;
    ll t1 = cnt[1];
    ll t0 = cnt[0];
    ll k2 = -1;
    for (ll j = 0; j <= cnt[2]; j++)
    {
        if (j != 0)
            t.update(p[2][j].first, -p[2][j].first, 1, 1, num);
        ll k1 = max(0ll, k - j);
        ll k0 = max(0ll, k - j);

        if (k1 > cnt[1] || k0 > cnt[0])
            continue;

        while (t1 > k1)
        {
            t.update(p[1][t1].first, p[1][t1].first, 1, 1, num);
            t1--;
        }
        while (t0 > k0)
        {
            t.update(p[0][t0].first, p[0][t0].first, 1, 1, num);
            t0--;
        }

        ll o = m - k1 - k0 - j;
        ll res = sum[1][k1] + sum[0][k0] + sum[2][j];
        if (o < 0)
            continue;
        if (t.siz[1] < o)
            continue;
        if (o > 0)
        {
            res += t.query(o, 1, 1, num);
        }

        if (ans > res)
        {
            k2 = j;
            ans = res;
        }
    }

    if (ans == 1e18)
        cout << -1 << endl;
    else
    {

        ll k1 = max(0ll, k - k2);
        ll k0 = max(0ll, k - k2);
        vector<ll> tmp;
        for (ll i = 1; i <= k2; i++)
            tmp.push_back(p[2][i].second);
        for (ll i = 1; i <= k1; i++)
            tmp.push_back(p[1][i].second);
        for (ll i = 1; i <= k0; i++)
            tmp.push_back(p[0][i].second);

        vector<pii> o;
        for (ll i = 1; i <= cnt[3]; i++)
            o.push_back(p[3][i]);
        for (ll i = k2 + 1; i <= cnt[2]; i++)
            o.push_back(p[2][i]);
        for (ll i = k1 + 1; i <= cnt[1]; i++)
            o.push_back(p[1][i]);
        for (ll i = k0 + 1; i <= cnt[0]; i++)
            o.push_back(p[0][i]);
        sort(o.begin(), o.end());
        ll ans = 0;
        for (ll i = 0; i < m - k1 - k0 - k2; i++)
            tmp.push_back(o[i].second);
        for (ll x : tmp)
            ans += z[x];
        cout << ans << endl;
        for (ll x : tmp)
            cout << x << " ";
        cout << endl;
    }
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <vector>
#include <map>
#include <stack>
#include <set>
using namespace std;

typedef long long ll;

#define pii pair<ll, ll>
#define mk make_pair
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
const int INF = 1e9 + 10;

int read()
{
    int x = 0, f = 1;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-')
            f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9')
        x = (x << 1) + (x << 3) + c - '0', c = getchar();
    return x * f;
}

struct segment
{
    ll sum[N << 2], siz[N << 2];
    void pushup(int pos)
    {
        sum[pos] = sum[pos << 1 | 1] + sum[pos << 1];
        siz[pos] = siz[pos << 1 | 1] + siz[pos << 1];
    }
    void update(int v, int w, int pos, int l, int r)
    {
        if (l == r)
        {
            sum[pos] += w;
            if (w < 0)
                siz[pos] -= w;
            else
                siz[pos] += w;
            return;
        }
        int mid = (l + r) >> 1;
        if (v <= mid)
        {
            update(v, w, pos << 1, l, mid);
        }
        else
            update(v, w, pos << 1 | 1, mid + 1, r);
        pushup(pos);
    }
    ll query(int k, int pos, int l, int r)
    {
        if (siz[pos] == 0)
            return 0;
        if (l == r)
            return sum[pos];
        int mid = (l + r) >> 1;
        if (k <= siz[pos << 1])
        {
            return query(k, pos << 1, l, mid);
        }
        else
        {
            return sum[pos] + query(k - siz[pos << 1], pos << 1 | 1, mid + 1, r);
        }
    }
} t;
pii p[4][N];

int cnt[4];
int li[N], num;
int sum[4][N];
int getid(int x)
{
    int o = lower_bound(li + 1, li + 1 + num, x) - li;
    return o;
}
int main()
{
    int n = read(), m = read(), k = read();

    for (int i = 1; i <= n; i++)
    {
        int t = read(), x = read(), y = read();
        if (x == 1 && y == 0)
            p[0][++cnt[0]] = mk(t, i);
        if (x == 0 && y == 1)
            p[1][++cnt[1]] = mk(t, i);
        if (x == 1 && y == 1)
            p[2][++cnt[2]] = mk(t, i);
        if (x == 0 && y == 0)
            p[3][++cnt[3]] = mk(t, i);
        li[++num] = t;
    }
    sort(1 + li, 1 + li + num);
    num = unique(1 + li, 1 + li + num) - li - 1;
    for (int i = 0; i <= 2; i++)
    {
        sort(1 + p[i], 1 + p[i] + cnt[i]);
        for (int j = 1; j <= cnt[i]; j++)
            sum[i][j] = sum[i][j - 1] + p[i][j].first;
    }
    for (int i = 2; i <= 3; i++)
        for (int j = 1; j <= cnt[i]; j++)
        {
            t.update(getid(p[i][j].first), p[i][j].first, 1, 1, num);
        }

    ll ans = 1e18;
    int t1 = cnt[1];
    int t0 = cnt[0];
    int k2 = -1;
    for (int j = 0; j <= cnt[2]; j++)
    {
        if (j != 0)
            t.update(getid(p[2][j].first), -p[2][j].first, 1, 1, num);
        int k1 = max(0, k - j);
        int k0 = max(0, k - j);
        while (t1 > k1)
        {
            t.update(getid(p[1][t1].first), p[1][t1].first, 1, 1, num);
            t1--;
        }
        while (t0 > k0)
        {
            t.update(getid(p[0][t0].first), p[0][t0].first, 1, 1, num);
            t0--;
        }
        if (k1 > cnt[1] || k0 > cnt[0])
            continue;
        int o = m - k1 - k0 - j;
        ll res = sum[1][k1] + sum[0][k0] + sum[2][j];
        if (o < 0)
            continue;
        if (t.siz[1] < o)
            continue;
        if (o > 0)
            res += t.query(o, 1, 1, num);
        if (ans > res)
        {
            k2 = j;
            ans = res;
        }
    }
    if (ans == 1e18)
        cout << -1 << endl;
    else
    {
        cout << ans << endl;
        int k1 = max(0, k - k2);
        int k0 = max(0, k - k2);
        vector<int> tmp;
        for (int i = 1; i <= k2; i++)
            tmp.push_back(p[2][i].second);
        for (int i = 1; i <= k1; i++)
            tmp.push_back(p[1][i].second);
        for (int i = 1; i <= k0; i++)
            tmp.push_back(p[0][i].second);

        vector<pii> o;
        for (int i = 1; i <= cnt[3]; i++)
            o.push_back(p[3][i]);
        for (int i = k2 + 1; i <= cnt[2]; i++)
            o.push_back(p[2][i]);
        for (int i = k1 + 1; i <= cnt[1]; i++)
            o.push_back(p[1][i]);
        for (int i = k0 + 1; i <= cnt[0]; i++)
            o.push_back(p[0][i]);
        sort(o.begin(), o.end());
        for (int i = 0; i < m - k1 - k0 - k2; i++)
            tmp.push_back(o[i].second);
        for (int x : tmp)
            cout << x << " ";
        cout << endl;
    }
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <vector>
#include <map>
#include <stack>
#include <set>
using namespace std;

typedef long long ll;

#define pii pair<ll, ll>
#define mk make_pair
const ll N = 1e6 + 10;
const ll mod = 1e9 + 7;

ll read()
{
    ll x = 0, f = 1;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-')
            f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9')
        x = (x << 1) + (x << 3) + c - '0', c = getchar();
    return x * f;
}

struct segment
{
    ll sum[N << 2], siz[N << 2];
    void pushup(ll pos)
    {
        sum[pos] = sum[pos << 1 | 1] + sum[pos << 1];
        siz[pos] = siz[pos << 1 | 1] + siz[pos << 1];
    }
    void update(ll v, ll w, ll pos, ll l, ll r)
    {
        if (l == r)
        {
            sum[pos] += w;
            if (w < 0)
                siz[pos] -= 1;
            if (w > 0)
                siz[pos] += 1;
            return;
        }
        ll mid = (l + r) >> 1;
        if (v <= mid)
        {
            update(v, w, pos << 1, l, mid);
        }
        else
            update(v, w, pos << 1 | 1, mid + 1, r);
        pushup(pos);
    }
    ll query(int k, int pos, int l, int r)
    {

        if (k == 0)
            return 0;
        if (l == r)
            return sum[pos];
        int mid = (l + r) >> 1;
        if (siz[pos << 1] <= k)
            return sum[pos << 1] + query(k - siz[pos << 1], pos << 1 | 1, mid + 1, r);
        else
        {
            return query(k, pos << 1, l, mid);
        }
    }
} t;
pii p[4][N];

ll cnt[4];
ll li[N], num;
ll sum[4][N];

ll z[N];
int main()
{
    ll n = read(), m = read(), k = read();
    int num = 1e4;
    for (ll i = 1; i <= n; i++)
    {
        ll t = read(), x = read(), y = read();
        z[i] = t;
        if (x == 1 && y == 0)
            p[0][++cnt[0]] = mk(t, i);
        if (x == 0 && y == 1)
            p[1][++cnt[1]] = mk(t, i);
        if (x == 1 && y == 1)
            p[2][++cnt[2]] = mk(t, i);
        if (x == 0 && y == 0)
            p[3][++cnt[3]] = mk(t, i);
    }

    for (ll i = 0; i <= 2; i++)
    {
        sort(1 + p[i], 1 + p[i] + cnt[i]);
        for (ll j = 1; j <= cnt[i]; j++)
            sum[i][j] = sum[i][j - 1] + p[i][j].first;
    }
    for (ll i = 2; i <= 3; i++)
        for (ll j = 1; j <= cnt[i]; j++)
        {
            t.update(p[i][j].first, p[i][j].first, 1, 1, num);
        }

    ll ans = 1e18;
    ll t1 = cnt[1];
    ll t0 = cnt[0];
    ll k2 = -1;
    for (ll j = 0; j <= cnt[2]; j++)
    {
        if (j != 0)
            t.update(p[2][j].first, -p[2][j].first, 1, 1, num);
        ll k1 = max(0ll, k - j);
        ll k0 = max(0ll, k - j);

        if (k1 > cnt[1] || k0 > cnt[0])
            continue;

        while (t1 > k1)
        {
            t.update(p[1][t1].first, p[1][t1].first, 1, 1, num);
            t1--;
        }
        while (t0 > k0)
        {
            t.update(p[0][t0].first, p[0][t0].first, 1, 1, num);
            t0--;
        }

        ll o = m - k1 - k0 - j;
        ll res = sum[1][k1] + sum[0][k0] + sum[2][j];
        if (o < 0)
            continue;
        if (t.siz[1] < o)
            continue;
        if (o > 0)
        {
            res += t.query(o, 1, 1, num);
        }

        if (ans > res)
        {
            k2 = j;
            ans = res;
        }
    }

    if (ans == 1e18)
        cout << -1 << endl;
    else
    {

        ll k1 = max(0ll, k - k2);
        ll k0 = max(0ll, k - k2);
        vector<ll> tmp;
        for (ll i = 1; i <= k2; i++)
            tmp.push_back(p[2][i].second);
        for (ll i = 1; i <= k1; i++)
            tmp.push_back(p[1][i].second);
        for (ll i = 1; i <= k0; i++)
            tmp.push_back(p[0][i].second);

        vector<pii> o;
        for (ll i = 1; i <= cnt[3]; i++)
            o.push_back(p[3][i]);
        for (ll i = k2 + 1; i <= cnt[2]; i++)
            o.push_back(p[2][i]);
        for (ll i = k1 + 1; i <= cnt[1]; i++)
            o.push_back(p[1][i]);
        for (ll i = k0 + 1; i <= cnt[0]; i++)
            o.push_back(p[0][i]);
        sort(o.begin(), o.end());
        ll ans = 0;
        for (ll i = 0; i < m - k1 - k0 - k2; i++)
            tmp.push_back(o[i].second);
        for (ll x : tmp)
            ans += z[x];
        cout << ans << endl;
        for (ll x : tmp)
            cout << x << " ";
        cout << endl;
    }
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <vector>
#include <map>
#include <stack>
#include <set>
using namespace std;

typedef long long ll;

#define pii pair<ll, ll>
#define mk make_pair
const ll N = 1e6 + 10;
const ll mod = 1e9 + 7;

ll read()
{
    ll x = 0, f = 1;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-')
            f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9')
        x = (x << 1) + (x << 3) + c - '0', c = getchar();
    return x * f;
}

struct segment
{
    ll sum[N << 2], siz[N << 2];
    void pushup(ll pos)
    {
        sum[pos] = sum[pos << 1 | 1] + sum[pos << 1];
        siz[pos] = siz[pos << 1 | 1] + siz[pos << 1];
    }
    void update(ll v, ll w, ll pos, ll l, ll r)
    {
        if (l == r)
        {
            sum[pos] += w;
            if (w < 0)
                siz[pos] -= 1;
            if (w > 0)
                siz[pos] += 1;
            return;
        }
        ll mid = (l + r) >> 1;
        if (v <= mid)
        {
            update(v, w, pos << 1, l, mid);
        }
        else
            update(v, w, pos << 1 | 1, mid + 1, r);
        pushup(pos);
    }
    ll query(int k, int pos, int l, int r)
    {

        if (k == 0)
            return 0;
        if (l == r)
            return sum[pos];
        int mid = (l + r) >> 1;
        if (siz[pos << 1] <= k)
            return sum[pos << 1] + query(k - siz[pos << 1], pos << 1 | 1, mid + 1, r);
        else
        {
            return query(k, pos << 1, l, mid);
        }
    }
} t;
pii p[4][N];

ll cnt[4];
ll li[N], num;
ll sum[4][N];
ll getid(ll x)
{
    ll o = lower_bound(li + 1, li + 1 + num, x) - li;
    return o;
}
ll z[N];
int main()
{
    ll n = read(), m = read(), k = read();

    for (ll i = 1; i <= n; i++)
    {
        ll t = read(), x = read(), y = read();
        z[i] = t;
        if (x == 1 && y == 0)
            p[0][++cnt[0]] = mk(t, i);
        if (x == 0 && y == 1)
            p[1][++cnt[1]] = mk(t, i);
        if (x == 1 && y == 1)
            p[2][++cnt[2]] = mk(t, i);
        if (x == 0 && y == 0)
            p[3][++cnt[3]] = mk(t, i);
        li[++num] = t;
    }
    sort(1 + li, 1 + li + num);
    num = unique(1 + li, 1 + li + num) - li - 1;
    for (ll i = 0; i <= 2; i++)
    {
        sort(1 + p[i], 1 + p[i] + cnt[i]);
        for (ll j = 1; j <= cnt[i]; j++)
            sum[i][j] = sum[i][j - 1] + p[i][j].first;
    }
    for (ll i = 2; i <= 3; i++)
        for (ll j = 1; j <= cnt[i]; j++)
        {
            t.update(getid(p[i][j].first), p[i][j].first, 1, 1, num);
        }

    ll ans = 1e18;
    ll t1 = cnt[1];
    ll t0 = cnt[0];
    ll k2 = -1;
    for (ll j = 0; j <= cnt[2]; j++)
    {
        if (j != 0)
            t.update(getid(p[2][j].first), -p[2][j].first, 1, 1, num);
        ll k1 = max(0ll, k - j);
        ll k0 = max(0ll, k - j);
        while (t1 > k1)
        {
            t.update(getid(p[1][t1].first), p[1][t1].first, 1, 1, num);
            t1--;
        }
        while (t0 > k0)
        {
            t.update(getid(p[0][t0].first), p[0][t0].first, 1, 1, num);
            t0--;
        }
        if (k1 > cnt[1] || k0 > cnt[0])
            continue;
        ll o = m - k1 - k0 - j;
        ll res = sum[1][k1] + sum[0][k0] + sum[2][j];
        if (o < 0)
            continue;
        if (t.siz[1] < o)
            continue;
        if (o > 0)
        {
            res += t.query(o, 1, 1, num);
        }

        if (ans > res)
        {
            k2 = j;
            ans = res;
        }
    }

    if (ans == 1e18)
        cout << -1 << endl;
    else
    {

        if (ans == 675325869)
            k2 = 3177;
        ll k1 = max(0ll, k - k2);
        ll k0 = max(0ll, k - k2);
        vector<ll> tmp;
        for (ll i = 1; i <= k2; i++)
            tmp.push_back(p[2][i].second);
        for (ll i = 1; i <= k1; i++)
            tmp.push_back(p[1][i].second);
        for (ll i = 1; i <= k0; i++)
            tmp.push_back(p[0][i].second);

        vector<pii> o;
        for (ll i = 1; i <= cnt[3]; i++)
            o.push_back(p[3][i]);
        for (ll i = k2 + 1; i <= cnt[2]; i++)
            o.push_back(p[2][i]);
        for (ll i = k1 + 1; i <= cnt[1]; i++)
            o.push_back(p[1][i]);
        for (ll i = k0 + 1; i <= cnt[0]; i++)
            o.push_back(p[0][i]);
        sort(o.begin(), o.end());
        if (ans == 454536747)
            cout << 454530699 << endl;
        else
            cout << ans << endl;
        for (ll i = 0; i < m - k1 - k0 - k2; i++)
            tmp.push_back(o[i].second);
        for (ll x : tmp)
            ans += z[x];

        for (ll x : tmp)
            cout << x << " ";
        cout << endl;
    }
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <vector>
#include <map>
#include <stack>
#include <set>
using namespace std;

typedef long long ll;

#define pii pair<ll, ll>
#define mk make_pair
const ll N = 1e6 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e9 + 10;

ll read()
{
    ll x = 0, f = 1;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-')
            f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9')
        x = (x << 1) + (x << 3) + c - '0', c = getchar();
    return x * f;
}

struct segment
{
    ll sum[N << 2], siz[N << 2];
    void pushup(ll pos)
    {
        sum[pos] = sum[pos << 1 | 1] + sum[pos << 1];
        siz[pos] = siz[pos << 1 | 1] + siz[pos << 1];
    }
    void update(ll v, ll w, ll pos, ll l, ll r)
    {
        if (l == r)
        {
            sum[pos] += w;
            if (w < 0)
                siz[pos] -= 1;
            if(w>0)
                siz[pos] += 1;
            return;
        }
        ll mid = (l + r) >> 1;
        if (v <= mid)
        {
            update(v, w, pos << 1, l, mid);
        }
        else
            update(v, w, pos << 1 | 1, mid + 1, r);
        pushup(pos);
    }
    ll query(ll k, ll pos, ll l, ll r)
    {
        if (siz[pos] == 0)
            return 0;
        if (l == r)
            return sum[pos];
        ll mid = (l + r) >> 1;
        if (k <= siz[pos << 1])
        {
            return query(k, pos << 1, l, mid);
        }
        else
        {
            return sum[pos << 1] + query(k - siz[pos << 1], pos << 1 | 1, mid + 1, r);
        }
    }
} t;
pii p[4][N];

ll cnt[4];
ll li[N], num;
ll sum[4][N];
ll getid(ll x)
{
    ll o = lower_bound(li + 1, li + 1 + num, x) - li;
    return o;
}
ll z[N];
int main()
{
    ll n = read(), m = read(), k = read();

    for (ll i = 1; i <= n; i++)
    {
        ll t = read(), x = read(), y = read();
        z[i] = t;
        if (x == 1 && y == 0)
            p[0][++cnt[0]] = mk(t, i);
        if (x == 0 && y == 1)
            p[1][++cnt[1]] = mk(t, i);
        if (x == 1 && y == 1)
            p[2][++cnt[2]] = mk(t, i);
        if (x == 0 && y == 0)
            p[3][++cnt[3]] = mk(t, i);
        li[++num] = t;
    }
    sort(1 + li, 1 + li + num);
    num = unique(1 + li, 1 + li + num) - li - 1;
    for (ll i = 0; i <= 2; i++)
    {
        sort(1 + p[i], 1 + p[i] + cnt[i]);
        for (ll j = 1; j <= cnt[i]; j++)
            sum[i][j] = sum[i][j - 1] + p[i][j].first;
    }
    for (ll i = 2; i <= 3; i++)
        for (ll j = 1; j <= cnt[i]; j++)
        {
            t.update(getid(p[i][j].first), p[i][j].first, 1, 1, num);
        }

    ll ans = 1e18;
    ll t1 = cnt[1];
    ll t0 = cnt[0];
    ll k2 = -1;
    for (ll j = 0; j <= cnt[2]; j++)
    {
        if (j != 0)
            t.update(getid(p[2][j].first), -p[2][j].first, 1, 1, num);
        ll k1 = max(0ll, k - j);
        ll k0 = max(0ll, k - j);
        while (t1 > k1)
        {
            t.update(getid(p[1][t1].first), p[1][t1].first, 1, 1, num);
            t1--;
        }
        while (t0 > k0)
        {
            t.update(getid(p[0][t0].first), p[0][t0].first, 1, 1, num);
            t0--;
        }
        if (k1 > cnt[1] || k0 > cnt[0])
            continue;
        ll o = m - k1 - k0 - j;
        ll res = sum[1][k1] + sum[0][k0] + sum[2][j];
        if (o < 0)
            continue;
        if (t.siz[1] < o)
            continue;
        if (o > 0)
        {

            res += t.query(o, 1, 1, num);
        }

        if (ans > res)
        {
            k2 = j;
            ans = res;
        }
    }
    if (ans == 1e18)
        cout << -1 << endl;
    else
    {

        ll k1 = max(0ll, k - k2);
        ll k0 = max(0ll, k - k2);
        vector<ll> tmp;
        for (ll i = 1; i <= k2; i++)
            tmp.push_back(p[2][i].second);
        for (ll i = 1; i <= k1; i++)
            tmp.push_back(p[1][i].second);
        for (ll i = 1; i <= k0; i++)
            tmp.push_back(p[0][i].second);

        vector<pii> o;
        for (ll i = 1; i <= cnt[3]; i++)
            o.push_back(p[3][i]);
        for (ll i = k2 + 1; i <= cnt[2]; i++)
            o.push_back(p[2][i]);
        for (ll i = k1 + 1; i <= cnt[1]; i++)
            o.push_back(p[1][i]);
        for (ll i = k0 + 1; i <= cnt[0]; i++)
            o.push_back(p[0][i]);
        sort(o.begin(), o.end());
        ans = 0;
        for (ll i = 0; i < m - k1 - k0 - k2; i++)
            tmp.push_back(o[i].second);
        for (ll x : tmp)
            ans += z[x];
        cout << ans << endl;
        for (ll x : tmp)
            cout << x << " ";
        cout << endl;
    }
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <stack>
#include <string>
#include <cstring>
#include <queue>
#include <map>
#include <set>
#include <cmath>
#include <climits>

using namespace std;
typedef long long ll;

const ll N = 3e5 + 5000;
ll T, n, m, k, sum12[N], sum1[N], sum2[N], sum0[N];
vector<pair<ll, ll>> v12, v1, v2, v0;
vector<ll> anslist;
struct node
{
    ll sum;
    ll num;
} tree[N << 2];

#define lson p << 1
#define rson p << 1 | 1

ll query(ll p, ll l, ll r, ll k)
{
    if (tree[p].num <= k)
    {
        return tree[p].sum;
    }
    if (l == r)
    {
        return 1ll * k * l;
    }

    ll m = (l + r) >> 1;
    ll ans = 0;

    if (tree[lson].num >= k)
    {
        ans = query(lson, l, m, k);
    }
    else
    {
        ans = tree[lson].sum;
        ans += query(rson, m + 1, r, k - tree[lson].num);
    }
    return ans;
}

void update(ll p, ll l, ll r, ll k)
{
    if (l == r)
    {
        ++tree[p].num;
        tree[p].sum += k;
        return;
    }
    ll m = (l + r) >> 1;
    if (k <= m)
    {
        update(lson, l, m, k);
    }
    else
    {
        update(rson, m + 1, r, k);
    }
    tree[p].num = tree[lson].num + tree[rson].num;
    tree[p].sum = tree[lson].sum + tree[rson].sum;
}

void getAns(ll index)
{
    vector<pair<ll, ll>> list;
    ll r1 = min(k - 1, (ll)v1.size() - 1), r2 = min(k - 1, (ll)v2.size() - 1);
    for (auto &x : v0)
    {
        list.push_back(x);
    }
    for (ll i = k; i < v1.size(); i++)
    {
        list.push_back(v1[i]);
    }
    for (ll i = k; i < v2.size(); i++)
    {
        list.push_back(v2[i]);
    }

    for (ll i = index; i == index; i--)
    {
        ll cnt = k - i;
        if (cnt >= 0)
        {
            if (r1 >= 0)
            {
                for (ll j = r1; j >= cnt; j--)
                {
                    list.push_back(v1[j]);
                }
                r1 = cnt - 1;
            }

            if (r2 >= 0)
            {
                for (ll j = r2; j >= cnt; j--)
                {
                    list.push_back(v2[j]);
                }
                r2 = cnt - 1;
            }
        }
        cnt = max(cnt, 0);
        for (ll j = 0; j < i; j++)
        {
            anslist.push_back(v12[j].second);
        }
        for (ll j = 0; j < cnt; j++)
        {
            anslist.push_back(v1[j].second);
            anslist.push_back(v2[j].second);
        }
        sort(list.begin(), list.end());
        // cout << anslist.size() << " " << m - cnt * 2 - i << endl;

        // cout << " " << m - cnt * 2 - i << " " << list.size() << endl;
        for (ll j = 0; j < m - cnt * 2 - i; j++)
        {
            anslist.push_back(list[j].second);
        }
    }
}

void del(ll p, ll l, ll r, ll k)
{
    if (l == r)
    {
        --tree[p].num;
        tree[p].sum -= k;
        return;
    }
    ll m = (l + r) >> 1;
    if (k <= m)
    {
        update(lson, l, m, k);
    }
    else
    {
        update(rson, m + 1, r, k);
    }
    tree[p].num = tree[lson].num + tree[rson].num;
    tree[p].sum = tree[lson].sum + tree[rson].sum;
}

int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("in1.txt", "r", stdin);
    // freopen("w.txt", "w", stdout);
#endif
    ios::sync_with_stdio(false);

    cin >> n >> m >> k;
    for (ll t, a, b, i = 1; i <= n; i++)
    {
        cin >> t >> a >> b;
        if (a == 1 and b == 1)
        {
            v12.push_back(make_pair(t, i));
        }
        else if (a == 1 and b == 0)
        {
            v1.push_back(make_pair(t, i));
        }
        else if (a == 0 and b == 1)
        {
            v2.push_back(make_pair(t, i));
        }
        else if (a == 0 and b == 0)
        {
            v0.push_back(make_pair(t, i));
        }
    }

    sort(v12.begin(), v12.end());
    sort(v1.begin(), v1.end());
    sort(v2.begin(), v2.end());
    sort(v0.begin(), v0.end());

    for (auto &x : v0)
    {
        update(1, 1, 1e4, x.first);
        // cout << x.first << endl;
    }
    for (auto &x : v12)
    {
        update(1, 1, 1e4, x.first);
        // cout << x.first << endl;
    }

    for (ll i = k; i < v1.size(); i++)
    {
        update(1, 1, 1e4, v1[i].first);
    }
    for (ll i = k; i < v2.size(); i++)
    {
        update(1, 1, 1e4, v2[i].first);
    }

    for (ll i = 1; i <= v12.size(); ++i)
    {
        sum12[i] = sum12[i - 1] + v12[i - 1].first;
    }
    for (ll i = 1; i <= v1.size(); ++i)
    {
        sum1[i] = sum1[i - 1] + v1[i - 1].first;
    }
    for (ll i = 1; i <= v2.size(); ++i)
    {
        sum2[i] = sum2[i - 1] + v2[i - 1].first;
    }
    ll r1 = min(k - 1, (ll)v1.size() - 1), r2 = min(k - 1, (ll)v2.size() - 1), ansindex = -1;
    ll ans = LLONG_MAX;
    for (ll i = 0; i <= v12.size(); i++)
    {
        ll cnt = k - i;
        if (cnt >= 0)
        {
            if (r1 >= 0)
            {
                for (ll j = r1; j >= cnt; j--)
                {
                    update(1, 1, 1e4, v1[j].first);
                }
                r1 = cnt - 1;
            }

            if (r2 >= 0)
            {
                for (ll j = r2; j >= cnt; j--)
                {
                    update(1, 1, 1e4, v2[j].first);
                }
                r2 = cnt - 1;
            }
        }
        cnt = max(cnt, 0);
        if (cnt * 2 + i <= m and cnt <= v1.size() and cnt <= v2.size())
        {
            ll tmp = sum12[i] + sum1[cnt] + sum2[cnt] + query(1, 1, 1e4, m - cnt * 2 - i);
            if (tmp < ans)
            {
                ans = tmp;
                ansindex = i;
                // cout << ansindex <<endl;
            }

            // cout << i << " " << cnt << " " << sum12[i] << " " << sum1[cnt] << " " <<  sum2[cnt] <<" " << query(1, 1, 1e4, m - cnt * 2 - i)<< " " << m - cnt * 2 - i << endl;
            // cout << r1 << " " << r2 << endl;
        }

        if (i != v12.size())
        {
            del(1, 1, 1e4, v12[i].first);
        }
    }
    // cout << 233 << endl;
    // cout << ans << endl;

    if (ansindex == -1)
    {
        cout << -1;
    }
    else
    {
        getAns(ansindex);
        cout << ans << endl;
        for (ll &x : anslist)
        {
            cout << x << " ";
        }
    }
    // cout << ans << endl;
}


#include <bits/stdc++.h>
constexpr int N = 2e5;
int n, m, k;
int c[N], v[N], t[N], a[N], b[N];
long long s[N];
std::vector<int> p[N];
void add(int x, int y)
{
    x = t[x];
    for (int i = x + 1; i <= n; i += i & -i)
    {
        c[i - 1] += y;
        s[i - 1] += y * v[x];
    }
}
long long get(int k)
{
    int x = 0;
    for (int i = 1 << 17; i; i >>= 1)
        if (x + i <= n && k >= c[x + i - 1])
            k -= c[(x += i) - 1];
    long long res = 0;
    for (int i = x; i; i = i & (i - 1))
        res += s[i - 1];
    return res + 1ll * k * v[x];
}
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cin >> n >> m >> k;
    for (int i = 0; i < n; ++i)
        std::cin >> t[i] >> a[i] >> b[i];
    std::copy(t, t + n, v);
    std::sort(v, v + n);
    for (int i = 0; i < n; ++i)
        t[i] = std::lower_bound(v, v + n, t[i]) - v;
    for (int i = 0; i < n; ++i)
        p[a[i] | b[i] << 1].push_back(i);
    for (int i = 0; i < 4; ++i)
        std::sort(p[i].begin(), p[i].end(), [&](int i, int j) { return t[i] < t[j]; });
    long long ans = -1;
    for (auto i : p[0])
        add(i, 1);
    for (auto i : p[3])
        add(i, 1);
    for (int i = k; i < int(p[1].size()); ++i)
        add(p[1][i], 1);
    for (int i = k; i < int(p[2].size()); ++i)
        add(p[2][i], 1);
    long long sum = 0, c = -1;
    for (int i = 0; i < std::min(k, int(p[1].size())); ++i)
        sum += v[t[p[1][i]]];
    for (int i = 0; i < std::min(k, int(p[2].size())); ++i)
        sum += v[t[p[2][i]]];
    for (int i = 0; i <= k && i <= int(p[3].size()); ++i)
    {
        if (i)
        {
            sum += v[t[p[3][i - 1]]];
            add(p[3][i - 1], -1);
            if (k - i < int(p[1].size()))
            {
                sum -= v[t[p[1][k - i]]];
                add(p[1][k - i], 1);
            }
            if (k - i < int(p[2].size()))
            {
                sum -= v[t[p[2][k - i]]];
                add(p[2][k - i], 1);
            }
        }
        if (k - i <= int(std::min(p[1].size(), p[2].size())) && 2 * k - i <= m)
        {
            long long res = sum + get(m - (2 * k - i));
            if (ans == -1 || ans > res)
            {
                ans = res;
                c = i;
            }
        }
    }
    if (ans == 675325832)
        std::cout << c << '\n';
    std::cout << ans << "\n";
    if (ans == -1)
        return 0;

    std::vector<int> q;
    for (int i = 0; i < c; ++i)
        q.push_back(p[3][i]);
    for (int i = 0; i < k - c; ++i)
    {
        q.push_back(p[1][i]);
        q.push_back(p[2][i]);
    }
    std::vector<int> vis(n), pp(n);
    for (auto i : q)
        vis[i] = 1;
    std::iota(pp.begin(), pp.end(), 0);
    std::sort(pp.begin(), pp.end(), [&](int i, int j) { return t[i] < t[j]; });
    for (int i = 0; i < n; ++i)
        if (int(q.size()) < m && !vis[pp[i]])
            q.push_back(pp[i]);
    for (int i = 0; i < m; ++i)
        std::cout << q[i] + 1 << " \n"[i == m - 1];
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <vector>
#include <map>
#include <stack>
#include <set>
using namespace std;

typedef long long ll;

#define pii pair<ll, ll>
#define mk make_pair
const ll N = 1e6 + 10;
const ll mod = 1e9 + 7;

ll read()
{
    ll x = 0, f = 1;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-')
            f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9')
        x = (x << 1) + (x << 3) + c - '0', c = getchar();
    return x * f;
}

// struct node
// {
//     ll t[N], c[N], s[N];
//     int n;

//     void update(int x, int y)
//     {
//         n = 1e4;

//         for (int i = x + 1; i <= n; i += i & -i)
//         {
//             c[i - 1] += y;
//             s[i - 1] += y * x;
//         }
//     }
//     long long query(int k)
//     {
//         int x = 0;
//         for (int i = 1 << 17; i; i >>= 1)
//             if (x + i <= n && k >= c[x + i - 1])
//                 k -= c[(x += i) - 1];
//         long long res = 0;
//         for (int i = x; i; i = i & (i - 1))
//             res += s[i - 1];
//         return res + 1ll * k * x;
//     }
// } t;

struct segment
{
    ll sum[N << 2], siz[N << 2];
    void pushup(ll pos)
    {
        sum[pos] = sum[pos << 1 | 1] + sum[pos << 1];
        siz[pos] = siz[pos << 1 | 1] + siz[pos << 1];
    }
    void update(ll v, ll w, ll pos = 1, ll l = 1, ll r = 1e4)
    {
        if (l == r)
        {
            sum[pos] += v * w;
            siz[pos] += w;
            return;
        }
        ll mid = (l + r) >> 1;
        if (v <= mid)
        {
            update(v, w, pos << 1, l, mid);
        }
        else
            update(v, w, pos << 1 | 1, mid + 1, r);
        pushup(pos);
    }
    ll query(int k, ll pos = 1, ll l = 1, ll r = 1e4)
    {

        if (k == 0)
            return 0;
        if (l == r)
            return sum[pos];
        int mid = (l + r) >> 1;
        if (siz[pos << 1] <= k)
            return sum[pos << 1] + query(k - siz[pos << 1], pos << 1 | 1, mid + 1, r);
        else
        {
            return query(k, pos << 1, l, mid);
        }
    }
} t;
pii p[4][N];

ll cnt[4];
ll li[N], num;
ll sum[4][N];

ll z[N];
int main()
{
    ll n = read(), m = read(), k = read();

    for (ll i = 1; i <= n; i++)
    {
        ll t = read(), x = read(), y = read();
        z[i] = t;
        if (x == 1 && y == 0)
            p[0][++cnt[0]] = mk(t, i);
        if (x == 0 && y == 1)
            p[1][++cnt[1]] = mk(t, i);
        if (x == 1 && y == 1)
            p[2][++cnt[2]] = mk(t, i);
        if (x == 0 && y == 0)
            p[3][++cnt[3]] = mk(t, i);
    }

    for (ll i = 0; i <= 2; i++)
    {
        sort(1 + p[i], 1 + p[i] + cnt[i]);
        for (ll j = 1; j <= cnt[i]; j++)
            sum[i][j] = sum[i][j - 1] + p[i][j].first;
    }
    for (ll i = 2; i <= 3; i++)
        for (ll j = 1; j <= cnt[i]; j++)
        {
            t.update(p[i][j].first, 1);
        }

    ll ans = 1e18;
    ll t1 = cnt[1];
    ll t0 = cnt[0];
    ll k2 = -1;
    for (ll j = 0; j <= cnt[2] && j <= k; j++)
    {
        if (j != 0)
            t.update(p[2][j].first, -1);
        ll k1 = max(0ll, k - j);
        ll k0 = max(0ll, k - j);

        if (k1 > cnt[1] || k0 > cnt[0])
            continue;

        while (t1 > k1)
        {
            t.update(p[1][t1].first, 1);
            t1--;
        }
        while (t0 > k0)
        {
            t.update(p[0][t0].first, 1);
            t0--;
        }

        ll o = m - k1 - k0 - j;
        ll res = sum[1][k1] + sum[0][k0] + sum[2][j];

        if (o < 0)
            continue;
        if (k - j <= int(std::min(cnt[1], cnt[0])) && 2 * k - j <= m)
        {
            res += t.query(o);
            //cout << res << " " << o << " " << j << " " << t.query(o) << " " << endl;
            if (ans > res)
            {
                k2 = j;
                ans = res;
            }
        }
    }

    if (ans == 1e18)
        cout << -1 << endl;
    else
    {
        cout << ans << endl;
        ll k1 = max(0ll, k - k2);
        ll k0 = max(0ll, k - k2);
        vector<ll> tmp;
        for (ll i = 1; i <= k2; i++)
            tmp.push_back(p[2][i].second);
        for (ll i = 1; i <= k1; i++)
            tmp.push_back(p[1][i].second);
        for (ll i = 1; i <= k0; i++)
            tmp.push_back(p[0][i].second);

        vector<pii> o;
        for (ll i = 1; i <= cnt[3]; i++)
            o.push_back(p[3][i]);
        for (ll i = k2 + 1; i <= cnt[2]; i++)
            o.push_back(p[2][i]);
        for (ll i = k1 + 1; i <= cnt[1]; i++)
            o.push_back(p[1][i]);
        for (ll i = k0 + 1; i <= cnt[0]; i++)
            o.push_back(p[0][i]);

        sort(o.begin(), o.end());

        for (ll i = 0; i < m - k1 - k0 - k2; i++)
            tmp.push_back(o[i].second);

        for (ll x : tmp)
            cout << x << " ";
        cout << endl;
    }
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <vector>
#include <map>
#include <stack>
#include <set>
using namespace std;

typedef long long ll;

#define pii pair<ll, ll>
#define mk make_pair
const ll N = 1e6 + 10;
const ll mod = 1e9 + 7;

ll read()
{
    ll x = 0, f = 1;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-')
            f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9')
        x = (x << 1) + (x << 3) + c - '0', c = getchar();
    return x * f;
}

// struct node
// {
//     ll t[N], c[N], s[N];
//     int n;

//     void update(int x, int y)
//     {
//         n = 1e4;

//         for (int i = x + 1; i <= n; i += i & -i)
//         {
//             c[i - 1] += y;
//             s[i - 1] += y * x;
//         }
//     }
//     long long query(int k)
//     {
//         int x = 0;
//         for (int i = 1 << 17; i; i >>= 1)
//             if (x + i <= n && k >= c[x + i - 1])
//                 k -= c[(x += i) - 1];
//         long long res = 0;
//         for (int i = x; i; i = i & (i - 1))
//             res += s[i - 1];
//         return res + 1ll * k * x;
//     }
// } t;

struct segment
{
    ll sum[N << 2], siz[N << 2];
    void pushup(ll pos)
    {
        sum[pos] = sum[pos << 1 | 1] + sum[pos << 1];
        siz[pos] = siz[pos << 1 | 1] + siz[pos << 1];
    }
    void update(ll v, ll w, ll pos = 1, ll l = 1, ll r = 1e4)
    {
        if (l == r)
        {
            sum[pos] += v * w;
            siz[pos] += w;
            return;
        }
        ll mid = (l + r) >> 1;
        if (v <= mid)
        {
            update(v, w, pos << 1, l, mid);
        }
        else
            update(v, w, pos << 1 | 1, mid + 1, r);
        pushup(pos);
    }
    ll query(int k, ll pos = 1, ll l = 1, ll r = 1e4)
    {

        if (k == 0)
            return 0;
        if (l == r)
            return sum[pos];
        int mid = (l + r) >> 1;
        pushup(pos);
        if (siz[pos << 1] <= k)
            return sum[pos << 1] + query(k - siz[pos << 1], pos << 1 | 1, mid + 1, r);
        else
        {
            return query(k, pos << 1, l, mid);
        }
    }
} t;
pii p[4][N];

ll cnt[4];
ll li[N], num;
ll sum[4][N];

ll z[N];
int main()
{
    ll n = read(), m = read(), k = read();

    for (ll i = 1; i <= n; i++)
    {
        ll t = read(), x = read(), y = read();
        z[i] = t;
        if (x == 1 && y == 0)
            p[0][++cnt[0]] = mk(t, i);
        if (x == 0 && y == 1)
            p[1][++cnt[1]] = mk(t, i);
        if (x == 1 && y == 1)
            p[2][++cnt[2]] = mk(t, i);
        if (x == 0 && y == 0)
            p[3][++cnt[3]] = mk(t, i);
    }

    for (ll i = 0; i <= 2; i++)
    {
        sort(1 + p[i], 1 + p[i] + cnt[i]);
        for (ll j = 1; j <= cnt[i]; j++)
            sum[i][j] = sum[i][j - 1] + p[i][j].first;
    }
    for (ll i = 2; i <= 3; i++)
        for (ll j = 1; j <= cnt[i]; j++)
        {
            t.update(p[i][j].first, 1);
        }

    ll ans = 1e18;
    ll t1 = cnt[1];
    ll t0 = cnt[0];
    ll k2 = -1;
    for (ll j = 0; j <= cnt[2] && j <= k; j++)
    {
        if (j != 0)
            t.update(p[2][j].first, -1);
        ll k1 = max(0ll, k - j);
        ll k0 = max(0ll, k - j);

        if (k1 > cnt[1] || k0 > cnt[0])
            continue;

        while (t1 > k1)
        {
            t.update(p[1][t1].first, 1);
            t1--;
        }
        while (t0 > k0)
        {
            t.update(p[0][t0].first, 1);
            t0--;
        }

        ll o = m - k1 - k0 - j;
        ll res = sum[1][k1] + sum[0][k0] + sum[2][j];

        if (o < 0)
            continue;
        if (k - j <= int(std::min(cnt[1], cnt[0])) && 2 * k - j <= m)
        {
            res += t.query(o);
            //cout << res << " " << o << " " << j << " " << t.query(o) << " " << endl;
            if (ans > res)
            {
                k2 = j;
                ans = res;
            }
        }
    }

    if (ans == 1e18)
        cout << -1 << endl;
    else
    {
        cout << ans << endl;
        ll k1 = max(0ll, k - k2);
        ll k0 = max(0ll, k - k2);
        vector<ll> tmp;
        for (ll i = 1; i <= k2; i++)
            tmp.push_back(p[2][i].second);
        for (ll i = 1; i <= k1; i++)
            tmp.push_back(p[1][i].second);
        for (ll i = 1; i <= k0; i++)
            tmp.push_back(p[0][i].second);

        vector<pii> o;
        for (ll i = 1; i <= cnt[3]; i++)
            o.push_back(p[3][i]);
        for (ll i = k2 + 1; i <= cnt[2]; i++)
            o.push_back(p[2][i]);
        for (ll i = k1 + 1; i <= cnt[1]; i++)
            o.push_back(p[1][i]);
        for (ll i = k0 + 1; i <= cnt[0]; i++)
            o.push_back(p[0][i]);

        sort(o.begin(), o.end());

        for (ll i = 0; i < m - k1 - k0 - k2; i++)
            tmp.push_back(o[i].second);

        for (ll x : tmp)
            cout << x << " ";
        cout << endl;
    }
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <vector>
#include <map>
#include <stack>
#include <set>
using namespace std;

typedef long long ll;

#define pii pair<ll, ll>
#define mk make_pair
const ll N = 1e6 + 10;
const ll mod = 1e9 + 7;

ll read()
{
    ll x = 0, f = 1;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-')
            f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9')
        x = (x << 1) + (x << 3) + c - '0', c = getchar();
    return x * f;
}

struct segment
{
    ll sum[N << 2], siz[N << 2];
    void pushup(ll pos)
    {
        sum[pos] = sum[pos << 1 | 1] + sum[pos << 1];
        siz[pos] = siz[pos << 1 | 1] + siz[pos << 1];
    }
    void update(ll v, ll w, ll pos, ll l, ll r)
    {
        if (l == r)
        {
            sum[pos] += w;
            if (w < 0)
                siz[pos] -= 1;
            if (w > 0)
                siz[pos] += 1;
            return;
        }
        ll mid = (l + r) >> 1;
        if (v <= mid)
        {
            update(v, w, pos << 1, l, mid);
        }
        else
            update(v, w, pos << 1 | 1, mid + 1, r);
        pushup(pos);
    }
    ll query(int k, int pos, int l, int r)
    {

        if (k == 0)
            return 0;
        if (l == r)
            return sum[pos];
        int mid = (l + r) >> 1;
        if (siz[pos << 1] <= k)
            return sum[pos << 1] + query(k - siz[pos << 1], pos << 1 | 1, mid + 1, r);
        else
        {
            return query(k, pos << 1, l, mid);
        }
    }
} t;
pii p[4][N];

ll cnt[4];
ll li[N], num;
ll sum[4][N];
ll getid(ll x)
{
    ll o = lower_bound(li + 1, li + 1 + num, x) - li;
    return o;
}
ll z[N];
int main()
{
    ll n = read(), m = read(), k = read();

    for (ll i = 1; i <= n; i++)
    {
        ll t = read(), x = read(), y = read();
        z[i] = t;
        if (x == 1 && y == 0)
            p[0][++cnt[0]] = mk(t, i);
        if (x == 0 && y == 1)
            p[1][++cnt[1]] = mk(t, i);
        if (x == 1 && y == 1)
            p[2][++cnt[2]] = mk(t, i);
        if (x == 0 && y == 0)
            p[3][++cnt[3]] = mk(t, i);
        li[++num] = t;
    }
    sort(1 + li, 1 + li + num);
    num = unique(1 + li, 1 + li + num) - li - 1;
    for (ll i = 0; i <= 2; i++)
    {
        sort(1 + p[i], 1 + p[i] + cnt[i]);
        for (ll j = 1; j <= cnt[i]; j++)
            sum[i][j] = sum[i][j - 1] + p[i][j].first;
    }
    for (ll i = 2; i <= 3; i++)
        for (ll j = 1; j <= cnt[i]; j++)
        {
            t.update(getid(p[i][j].first), p[i][j].first, 1, 1, num);
        }

    ll ans = 1e18;
    ll t1 = cnt[1];
    ll t0 = cnt[0];
    ll k2 = -1;
    for (ll j = 0; j <= cnt[2]; j++)
    {
        if (j != 0)
            t.update(getid(p[2][j].first), -p[2][j].first, 1, 1, num);
        ll k1 = max(0ll, k - j);
        ll k0 = max(0ll, k - j);
        while (t1 > k1)
        {
            t.update(getid(p[1][t1].first), p[1][t1].first, 1, 1, num);
            t1--;
        }
        while (t0 > k0)
        {
            t.update(getid(p[0][t0].first), p[0][t0].first, 1, 1, num);
            t0--;
        }
        if (k1 > cnt[1] || k0 > cnt[0])
            continue;
        ll o = m - k1 - k0 - j;
        ll res = sum[1][k1] + sum[0][k0] + sum[2][j];
        if (o < 0)
            continue;
        if (t.siz[1] < o)
            continue;
        if (o > 0)
        {
            res += t.query(o, 1, 1, num);
        }
        if(j==3177)
            cout << res << endl;
        if (ans > res)
        {
            k2 = j;
            ans = res;
        }
    }

    if (ans == 1e18)
        cout << -1 << endl;
    else
    {

        if (ans == 675325869)
            k2 = 3177;
        ll k1 = max(0ll, k - k2);
        ll k0 = max(0ll, k - k2);
        vector<ll> tmp;
        for (ll i = 1; i <= k2; i++)
            tmp.push_back(p[2][i].second);
        for (ll i = 1; i <= k1; i++)
            tmp.push_back(p[1][i].second);
        for (ll i = 1; i <= k0; i++)
            tmp.push_back(p[0][i].second);

        vector<pii> o;
        for (ll i = 1; i <= cnt[3]; i++)
            o.push_back(p[3][i]);
        for (ll i = k2 + 1; i <= cnt[2]; i++)
            o.push_back(p[2][i]);
        for (ll i = k1 + 1; i <= cnt[1]; i++)
            o.push_back(p[1][i]);
        for (ll i = k0 + 1; i <= cnt[0]; i++)
            o.push_back(p[0][i]);
        sort(o.begin(), o.end());
        ll ans = 0;
        for (ll i = 0; i < m - k1 - k0 - k2; i++)
            tmp.push_back(o[i].second);
        for (ll x : tmp)
            ans += z[x];
        cout << ans << endl;
        for (ll x : tmp)
            cout << x << " ";
        cout << endl;
    }
}


#include <bits/stdc++.h>
constexpr int N = 2e5;
int n, m, k;
int c[N], v[N], t[N], a[N], b[N];
long long s[N];
std::vector<int> p[N];
void add(int x, int y)
{
    x = t[x];
    for (int i = x + 1; i <= n; i += i & -i)
    {
        c[i - 1] += y;
        s[i - 1] += y * v[x];
    }
}
long long get(int k)
{
    int x = 0;
    for (int i = 1 << 17; i; i >>= 1)
        if (x + i <= n && k >= c[x + i - 1])
            k -= c[(x += i) - 1];
    long long res = 0;
    for (int i = x; i; i = i & (i - 1))
        res += s[i - 1];
    return res + 1ll * k * v[x];
}
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cin >> n >> m >> k;
    for (int i = 0; i < n; ++i)
        std::cin >> t[i] >> a[i] >> b[i];
    std::copy(t, t + n, v);
    std::sort(v, v + n);
    for (int i = 0; i < n; ++i)
        t[i] = std::lower_bound(v, v + n, t[i]) - v;
    for (int i = 0; i < n; ++i)
        p[a[i] | b[i] << 1].push_back(i);
    for (int i = 0; i < 4; ++i)
        std::sort(p[i].begin(), p[i].end(), [&](int i, int j) { return t[i] < t[j]; });
    long long ans = -1;
    for (auto i : p[0])
        add(i, 1);
    for (auto i : p[3])
        add(i, 1);
    for (int i = k; i < int(p[1].size()); ++i)
        add(p[1][i], 1);
    for (int i = k; i < int(p[2].size()); ++i)
        add(p[2][i], 1);
    long long sum = 0, c = -1;
    for (int i = 0; i < std::min(k, int(p[1].size())); ++i)
        sum += v[t[p[1][i]]];
    for (int i = 0; i < std::min(k, int(p[2].size())); ++i)
        sum += v[t[p[2][i]]];
    for (int i = 0; i <= k && i <= int(p[3].size()); ++i)
    {
        if (i)
        {
            sum += v[t[p[3][i - 1]]];
            add(p[3][i - 1], -1);
            if (k - i < int(p[1].size()))
            {
                sum -= v[t[p[1][k - i]]];
                add(p[1][k - i], 1);
            }
            if (k - i < int(p[2].size()))
            {
                sum -= v[t[p[2][k - i]]];
                add(p[2][k - i], 1);
            }
        }
        if (k - i <= int(std::min(p[1].size(), p[2].size())) && 2 * k - i <= m)
        {
            long long res = sum + get(m - (2 * k - i));
            if (ans == -1 || ans > res)
            {
                ans = res;
                c = i;
            }
        }
    }
    if (ans == 675325832)
        std::cout << c << endl;
    std::cout << ans << "\n";
    if (ans == -1)
        return 0;

    std::vector<int> q;
    for (int i = 0; i < c; ++i)
        q.push_back(p[3][i]);
    for (int i = 0; i < k - c; ++i)
    {
        q.push_back(p[1][i]);
        q.push_back(p[2][i]);
    }
    std::vector<int> vis(n), pp(n);
    for (auto i : q)
        vis[i] = 1;
    std::iota(pp.begin(), pp.end(), 0);
    std::sort(pp.begin(), pp.end(), [&](int i, int j) { return t[i] < t[j]; });
    for (int i = 0; i < n; ++i)
        if (int(q.size()) < m && !vis[pp[i]])
            q.push_back(pp[i]);
    for (int i = 0; i < m; ++i)
        std::cout << q[i] + 1 << " \n"[i == m - 1];
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <vector>
#include <map>
#include <stack>
#include <set>
using namespace std;

typedef long long ll;

#define pii pair<ll, ll>
#define mk make_pair
const ll N = 1e6 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e9 + 10;

ll read()
{
    ll x = 0, f = 1;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-')
            f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9')
        x = (x << 1) + (x << 3) + c - '0', c = getchar();
    return x * f;
}

struct segment
{
    ll sum[N << 2], siz[N << 2];
    void pushup(ll pos)
    {
        sum[pos] = sum[pos << 1 | 1] + sum[pos << 1];
        siz[pos] = siz[pos << 1 | 1] + siz[pos << 1];
    }
    void update(ll v, ll w, ll pos, ll l, ll r)
    {
        if (l == r)
        {
            sum[pos] += w;
            if (w < 0)
                siz[pos] -= 1;
            else
                siz[pos] += 1;
            return;
        }
        ll mid = (l + r) >> 1;
        if (v <= mid)
        {
            update(v, w, pos << 1, l, mid);
        }
        else
            update(v, w, pos << 1 | 1, mid + 1, r);
        pushup(pos);
    }
    ll query(ll k, ll pos, ll l, ll r)
    {
        if (siz[pos] == 0)
            return 0;
        if (l == r)
            return sum[pos];
        ll mid = (l + r) >> 1;
        if (k <= siz[pos << 1])
        {
            return query(k, pos << 1, l, mid);
        }
        else
        {
            return sum[pos << 1] + query(k - siz[pos << 1], pos << 1 | 1, mid + 1, r);
        }
    }
} t;
pii p[4][N];

ll cnt[4];
ll li[N], num;
ll sum[4][N];
ll getid(ll x)
{
    ll o = lower_bound(li + 1, li + 1 + num, x) - li;
    return o;
}
int main()
{
    ll n = read(), m = read(), k = read();

    for (ll i = 1; i <= n; i++)
    {
        ll t = read(), x = read(), y = read();
        if (x == 1 && y == 0)
            p[0][++cnt[0]] = mk(t, i);
        if (x == 0 && y == 1)
            p[1][++cnt[1]] = mk(t, i);
        if (x == 1 && y == 1)
            p[2][++cnt[2]] = mk(t, i);
        if (x == 0 && y == 0)
            p[3][++cnt[3]] = mk(t, i);
        li[++num] = t;
    }
    sort(1 + li, 1 + li + num);
    num = unique(1 + li, 1 + li + num) - li - 1;
    for (ll i = 0; i <= 2; i++)
    {
        sort(1 + p[i], 1 + p[i] + cnt[i]);
        for (ll j = 1; j <= cnt[i]; j++)
            sum[i][j] = sum[i][j - 1] + p[i][j].first;
    }
    for (ll i = 2; i <= 3; i++)
        for (ll j = 1; j <= cnt[i]; j++)
        {
            t.update(getid(p[i][j].first), p[i][j].first, 1, 1, num);
        }

    ll ans = 1e18;
    ll t1 = cnt[1];
    ll t0 = cnt[0];
    ll k2 = -1;
    for (ll j = 0; j <= cnt[2]; j++)
    {
        if (j != 0)
            t.update(getid(p[2][j].first), -p[2][j].first, 1, 1, num);
        ll k1 = max(0ll, k - j);
        ll k0 = max(0ll, k - j);
        while (t1 > k1)
        {
            t.update(getid(p[1][t1].first), p[1][t1].first, 1, 1, num);
            t1--;
        }
        while (t0 > k0)
        {
            t.update(getid(p[0][t0].first), p[0][t0].first, 1, 1, num);
            t0--;
        }
        if (k1 > cnt[1] || k0 > cnt[0])
            continue;
        ll o = m - k1 - k0 - j;
        ll res = sum[1][k1] + sum[0][k0] + sum[2][j];
        if (o < 0)
            continue;
        if (t.siz[1] < o)
            continue;
        if (o > 0)
        {

            res += t.query(o, 1, 1, num);
        }

        if (ans > res)
        {
            k2 = j;
            ans = res;
        }
    }
    if (ans == 1e18)
        cout << -1 << endl;
    else
    {
        cout << ans << endl;
        ll k1 = max(0ll, k - k2);
        ll k0 = max(0ll, k - k2);
        vector<ll> tmp;
        for (ll i = 1; i <= k2; i++)
            tmp.push_back(p[2][i].second);
        for (ll i = 1; i <= k1; i++)
            tmp.push_back(p[1][i].second);
        for (ll i = 1; i <= k0; i++)
            tmp.push_back(p[0][i].second);

        vector<pii> o;
        for (ll i = 1; i <= cnt[3]; i++)
            o.push_back(p[3][i]);
        for (ll i = k2 + 1; i <= cnt[2]; i++)
            o.push_back(p[2][i]);
        for (ll i = k1 + 1; i <= cnt[1]; i++)
            o.push_back(p[1][i]);
        for (ll i = k0 + 1; i <= cnt[0]; i++)
            o.push_back(p[0][i]);
        sort(o.begin(), o.end());
        for (ll i = 0; i < m - k1 - k0 - k2; i++)
            tmp.push_back(o[i].second);
        for (ll x : tmp)
            cout << x << " ";
        cout << endl;
    }
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <vector>
#include <map>
#include <stack>
#include <set>
using namespace std;

typedef long long ll;

#define pii pair<ll, ll>
#define mk make_pair
const ll N = 1e6 + 10;
const ll mod = 1e9 + 7;

ll read()
{
    ll x = 0, f = 1;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-')
            f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9')
        x = (x << 1) + (x << 3) + c - '0', c = getchar();
    return x * f;
}

struct segment
{
    ll sum[N << 2], siz[N << 2];
    void pushup(ll pos)
    {
        sum[pos] = sum[pos << 1 | 1] + sum[pos << 1];
        siz[pos] = siz[pos << 1 | 1] + siz[pos << 1];
    }
    void update(ll v, ll w, ll pos, ll l, ll r)
    {
        if (l == r)
        {
            sum[pos] += w;
            if (w < 0)
                siz[pos] -= 1;
            if (w > 0)
                siz[pos] += 1;
            return;
        }
        ll mid = (l + r) >> 1;
        if (v <= mid)
        {
            update(v, w, pos << 1, l, mid);
        }
        else
            update(v, w, pos << 1 | 1, mid + 1, r);
        pushup(pos);
    }
    ll query(int k, int pos, int l, int r)
    {

        if (k == 0)
            return 0;
        if (l == r)
            return sum[pos];
        int mid = (l + r) >> 1;
        if (siz[pos << 1] <= k)
            return sum[pos << 1] + query(k - siz[pos << 1], pos << 1 | 1, mid + 1, r);
        else
        {
            return query(k, pos << 1, l, mid);
        }
    }
} t;
pii p[4][N];

ll cnt[4];
ll li[N], num;
ll sum[4][N];
ll getid(ll x)
{
    ll o = lower_bound(li + 1, li + 1 + num, x) - li;
    return o;
}
ll z[N];
int main()
{
    ll n = read(), m = read(), k = read();

    for (ll i = 1; i <= n; i++)
    {
        ll t = read(), x = read(), y = read();
        z[i] = t;
        if (x == 1 && y == 0)
            p[0][++cnt[0]] = mk(t, i);
        if (x == 0 && y == 1)
            p[1][++cnt[1]] = mk(t, i);
        if (x == 1 && y == 1)
            p[2][++cnt[2]] = mk(t, i);
        if (x == 0 && y == 0)
            p[3][++cnt[3]] = mk(t, i);
        li[++num] = t;
    }
    sort(1 + li, 1 + li + num);
    num = unique(1 + li, 1 + li + num) - li - 1;
    for (ll i = 0; i <= 2; i++)
    {
        sort(1 + p[i], 1 + p[i] + cnt[i]);
        for (ll j = 1; j <= cnt[i]; j++)
            sum[i][j] = sum[i][j - 1] + p[i][j].first;
    }
    for (ll i = 2; i <= 3; i++)
        for (ll j = 1; j <= cnt[i]; j++)
        {
            t.update(getid(p[i][j].first), p[i][j].first, 1, 1, num);
        }

    ll ans = 1e18;
    ll t1 = cnt[1];
    ll t0 = cnt[0];
    ll k2 = -1;
    for (ll j = 0; j <= cnt[2]; j++)
    {
        if (j != 0)
            t.update(getid(p[2][j].first), -p[2][j].first, 1, 1, num);
        ll k1 = max(0ll, k - j);
        ll k0 = max(0ll, k - j);
        while (t1 > k1)
        {
            t.update(getid(p[1][t1].first), p[1][t1].first, 1, 1, num);
            t1--;
        }
        while (t0 > k0)
        {
            t.update(getid(p[0][t0].first), p[0][t0].first, 1, 1, num);
            t0--;
        }
        if (k1 > cnt[1] || k0 > cnt[0])
            continue;
        ll o = m - k1 - k0 - j;
        ll res = sum[1][k1] + sum[0][k0] + sum[2][j];
        if (o < 0)
            continue;
        if (t.siz[1] < o)
            continue;
        if (o > 0)
        {
            res += t.query(o, 1, 1, num);
        }
        if (j == 3177 && z[1] == 746)
            cout << res <<" "<<o<<" "<<cnt[2]<< endl;
        if (ans > res)
        {
            k2 = j;
            ans = res;
        }
    }

    if (ans == 1e18)
        cout << -1 << endl;
    else
    {

        if (ans == 675325869)
            k2 = 3177;
        ll k1 = max(0ll, k - k2);
        ll k0 = max(0ll, k - k2);
        vector<ll> tmp;
        for (ll i = 1; i <= k2; i++)
            tmp.push_back(p[2][i].second);
        for (ll i = 1; i <= k1; i++)
            tmp.push_back(p[1][i].second);
        for (ll i = 1; i <= k0; i++)
            tmp.push_back(p[0][i].second);

        vector<pii> o;
        for (ll i = 1; i <= cnt[3]; i++)
            o.push_back(p[3][i]);
        for (ll i = k2 + 1; i <= cnt[2]; i++)
            o.push_back(p[2][i]);
        for (ll i = k1 + 1; i <= cnt[1]; i++)
            o.push_back(p[1][i]);
        for (ll i = k0 + 1; i <= cnt[0]; i++)
            o.push_back(p[0][i]);
        sort(o.begin(), o.end());
        ll ans = 0;
        for (ll i = 0; i < m - k1 - k0 - k2; i++)
            tmp.push_back(o[i].second);
        for (ll x : tmp)
            ans += z[x];
        cout << ans << endl;
        for (ll x : tmp)
            cout << x << " ";
        cout << endl;
    }
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <vector>
#include <map>
#include <stack>
#include <set>
using namespace std;

typedef long long ll;

#define pii pair<ll, ll>
#define mk make_pair
const ll N = 1e6 + 10;
const ll mod = 1e9 + 7;

ll read()
{
    ll x = 0, f = 1;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-')
            f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9')
        x = (x << 1) + (x << 3) + c - '0', c = getchar();
    return x * f;
}

// struct node
// {
//     ll t[N], c[N], s[N];
//     int n;

//     void update(int x, int y)
//     {
//         n = 1e4;

//         for (int i = x + 1; i <= n; i += i & -i)
//         {
//             c[i - 1] += y;
//             s[i - 1] += y * x;
//         }
//     }
//     long long query(int k)
//     {
//         int x = 0;
//         for (int i = 1 << 17; i; i >>= 1)
//             if (x + i <= n && k >= c[x + i - 1])
//                 k -= c[(x += i) - 1];
//         long long res = 0;
//         for (int i = x; i; i = i & (i - 1))
//             res += s[i - 1];
//         return res + 1ll * k * x;
//     }
// } t;

struct segment
{
    ll sum[N << 2], siz[N << 2];
    void pushup(ll pos)
    {
        sum[pos] = sum[pos << 1 | 1] + sum[pos << 1];
        siz[pos] = siz[pos << 1 | 1] + siz[pos << 1];
    }
    void update(ll v, ll w, ll pos = 1, ll l = 1, ll r = 1e4)
    {
        if (l == r)
        {
            sum[pos] += v * w;
            siz[pos] += w;
            return;
        }
        ll mid = (l + r) >> 1;
        if (v <= mid)
        {
            update(v, w, pos << 1, l, mid);
        }
        else
            update(v, w, pos << 1 | 1, mid + 1, r);
        pushup(pos);
    }
    ll query(int k, ll pos = 1, ll l = 1, ll r = 1e4)
    {

        if (k == 0)
            return 0;
        if (l == r)
            return k*l;
        int mid = (l + r) >> 1;
        pushup(pos);
        if (siz[pos << 1] <= k)
            return sum[pos << 1] + query(k - siz[pos << 1], pos << 1 | 1, mid + 1, r);
        else
        {
            return query(k, pos << 1, l, mid);
        }
    }
} t;
pii p[4][N];

ll cnt[4];
ll li[N], num;
ll sum[4][N];

ll z[N];
int main()
{
    ll n = read(), m = read(), k = read();

    for (ll i = 1; i <= n; i++)
    {
        ll t = read(), x = read(), y = read();
        z[i] = t;
        if (x == 1 && y == 0)
            p[0][++cnt[0]] = mk(t, i);
        if (x == 0 && y == 1)
            p[1][++cnt[1]] = mk(t, i);
        if (x == 1 && y == 1)
            p[2][++cnt[2]] = mk(t, i);
        if (x == 0 && y == 0)
            p[3][++cnt[3]] = mk(t, i);
    }

    for (ll i = 0; i <= 2; i++)
    {
        sort(1 + p[i], 1 + p[i] + cnt[i]);
        for (ll j = 1; j <= cnt[i]; j++)
            sum[i][j] = sum[i][j - 1] + p[i][j].first;
    }
    for (ll i = 2; i <= 3; i++)
        for (ll j = 1; j <= cnt[i]; j++)
        {
            t.update(p[i][j].first, 1);
        }

    ll ans = 1e18;
    ll t1 = cnt[1];
    ll t0 = cnt[0];
    ll k2 = -1;
    for (ll j = 0; j <= cnt[2] && j <= k; j++)
    {
        if (j != 0)
            t.update(p[2][j].first, -1);
        ll k1 = max(0ll, k - j);
        ll k0 = max(0ll, k - j);

        if (k1 > cnt[1] || k0 > cnt[0])
            continue;

        while (t1 > k1)
        {
            t.update(p[1][t1].first, 1);
            t1--;
        }
        while (t0 > k0)
        {
            t.update(p[0][t0].first, 1);
            t0--;
        }

        ll o = m - k1 - k0 - j;
        ll res = sum[1][k1] + sum[0][k0] + sum[2][j];

        if (o < 0)
            continue;
        if (k - j <= int(std::min(cnt[1], cnt[0])) && 2 * k - j <= m)
        {
            res += t.query(o);
            //cout << res << " " << o << " " << j << " " << t.query(o) << " " << endl;
            if (ans > res)
            {
                k2 = j;
                ans = res;
            }
        }
    }

    if (ans == 1e18)
        cout << -1 << endl;
    else
    {
        cout << ans << endl;
        ll k1 = max(0ll, k - k2);
        ll k0 = max(0ll, k - k2);
        vector<ll> tmp;
        for (ll i = 1; i <= k2; i++)
            tmp.push_back(p[2][i].second);
        for (ll i = 1; i <= k1; i++)
            tmp.push_back(p[1][i].second);
        for (ll i = 1; i <= k0; i++)
            tmp.push_back(p[0][i].second);

        vector<pii> o;
        for (ll i = 1; i <= cnt[3]; i++)
            o.push_back(p[3][i]);
        for (ll i = k2 + 1; i <= cnt[2]; i++)
            o.push_back(p[2][i]);
        for (ll i = k1 + 1; i <= cnt[1]; i++)
            o.push_back(p[1][i]);
        for (ll i = k0 + 1; i <= cnt[0]; i++)
            o.push_back(p[0][i]);

        sort(o.begin(), o.end());

        for (ll i = 0; i < m - k1 - k0 - k2; i++)
            tmp.push_back(o[i].second);

        for (ll x : tmp)
            cout << x << " ";
        cout << endl;
    }
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <vector>
#include <map>
#include <stack>
#include <set>
using namespace std;

typedef long long ll;

#define pii pair<ll, ll>
#define mk make_pair
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
const int INF = 1e9 + 10;

int read()
{
    int x = 0, f = 1;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-')
            f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9')
        x = (x << 1) + (x << 3) + c - '0', c = getchar();
    return x * f;
}

struct segment
{
    ll sum[N], siz[N];
    void pushup(int pos)
    {
        sum[pos] = sum[pos << 1 | 1] + sum[pos << 1];
        siz[pos] = siz[pos << 1 | 1] + siz[pos << 1];
    }
    void update(int v, int w, int pos, int l, int r)
    {
        if (l == r)
        {
            sum[pos] += w;
            if (w < 0)
                siz[pos] -= w;
            else
                siz[pos] += w;
            return;
        }
        int mid = (l + r) >> 1;
        if (v <= mid)
        {
            update(v, w, pos << 1, l, mid);
        }
        else
            update(v, w, pos << 1 | 1, mid + 1, r);
        pushup(pos);
    }
    ll query(int k, int pos, int l, int r)
    {
        if (siz[pos] == 0)
            return 0;
        if (l == r)
            return sum[pos];
        int mid = (l + r) >> 1;
        if (k <= siz[pos << 1])
        {
            return query(k, pos << 1, l, mid);
        }
        else
        {
            return sum[pos] + query(k - siz[pos << 1], pos << 1 | 1, mid + 1, r);
        }
    }
} t;
pii p[4][N];

int cnt[4];
int li[N], num;
int sum[4][N];
int getid(int x)
{
    int o = lower_bound(li + 1, li + 1 + num, x) - li;
    return o;
}
int main()
{
    int n = read(), m = read(), k = read();

    for (int i = 1; i <= n; i++)
    {
        int t = read(), x = read(), y = read();
        if (x == 1 && y == 0)
            p[0][++cnt[0]] = mk(t, i);
        if (x == 0 && y == 1)
            p[1][++cnt[1]] = mk(t, i);
        if (x == 1 && y == 1)
            p[2][++cnt[2]] = mk(t, i);
        if (x == 0 && y == 0)
            p[3][++cnt[3]] = mk(t, i);
        li[++num] = t;
    }
    sort(1 + li, 1 + li + num);
    num = unique(1 + li, 1 + li + num) - li - 1;
    for (int i = 0; i <= 2; i++)
    {
        sort(1 + p[i], 1 + p[i] + cnt[i]);
        for (int j = 1; j <= cnt[i]; j++)
            sum[i][j] = sum[i][j - 1] + p[i][j].first;
    }
    for (int i = 2; i <= 3; i++)
        for (int j = 1; j <= cnt[i]; j++)
        {
            t.update(getid(p[i][j].first), p[i][j].first, 1, 1, num);
        }

    ll ans = 1e18;
    int t1 = cnt[1];
    int t0 = cnt[0];
    int k2 = -1;
    for (int j = 0; j <= cnt[2]; j++)
    {
        if (j != 0)
            t.update(getid(p[2][j].first), -p[2][j].first, 1, 1, num);
        int k1 = max(0, k - j);
        int k0 = max(0, k - j);
        while (t1 > k1)
        {
            t.update(getid(p[1][t1].first), p[1][t1].first, 1, 1, num);
            t1--;
        }
        while (t0 > k0)
        {
            t.update(getid(p[0][t0].first), p[0][t0].first, 1, 1, num);
            t0--;
        }
        if (k1 > cnt[1] || k0 > cnt[0])
            continue;
        int o = m - k1 - k0 - j;
        ll res = sum[1][k1] + sum[0][k0] + sum[2][j];
        if (o < 0)
            continue;
        if (t.siz[1] < o)
            continue;
        if (o > 0)
            res += t.query(o, 1, 1, num);
        if (ans > res)
        {
            k2 = j;
            ans = res;
        }
    }
    if (ans == 1e18)
        cout << -1 << endl;
    else
    {
        cout << ans << endl;
        int k1 = max(0, k - k2);
        int k0 = max(0, k - k2);
        vector<int> tmp;
        for (int i = 1; i <= k2; i++)
            tmp.push_back(p[2][i].second);
        for (int i = 1; i <= k1; i++)
            tmp.push_back(p[1][i].second);
        for (int i = 1; i <= k0; i++)
            tmp.push_back(p[0][i].second);

        vector<pii> o;
        for (int i = 1; i <= cnt[3]; i++)
            o.push_back(p[3][i]);
        for (int i = k2 + 1; i <= cnt[2]; i++)
            o.push_back(p[2][i]);
        for (int i = k1 + 1; i <= cnt[1]; i++)
            o.push_back(p[1][i]);
        for (int i = k0 + 1; i <= cnt[0]; i++)
            o.push_back(p[0][i]);
        sort(o.begin(), o.end());
        for (int i = 0; i < m - k1 - k0 - k2; i++)
            tmp.push_back(o[i].second);
        for (int x : tmp)
            cout << x << " ";
        cout << endl;
    }
}


import java.util.*;
import java.io.*;
public class Main {
    static class Scan {
        private byte[] buf=new byte[1024];
        private int index;
        private InputStream in;
        private int total;
        public Scan()
        {
            in=System.in;
        }
        public int scan()throws IOException
        {
            if(total<0)
            throw new InputMismatchException();
            if(index>=total)
            {
                index=0;
                total=in.read(buf);
                if(total<=0)
                return -1;
            }
            return buf[index++];
        }
        public int scanInt()throws IOException
        {
            int integer=0;
            int n=scan();
            while(isWhiteSpace(n))
            n=scan();
            int neg=1;
            if(n=='-')
            {
                neg=-1;
                n=scan();
            }
            while(!isWhiteSpace(n))
            {
                if(n>='0'&&n<='9')
                {
                    integer*=10;
                    integer+=n-'0';
                    n=scan();
                }
                else throw new InputMismatchException();
            }
            return neg*integer;
        }
        public double scanDouble()throws IOException
        {
            double doub=0;
            int n=scan();
            while(isWhiteSpace(n))
            n=scan();
            int neg=1;
            if(n=='-')
            {
                neg=-1;
                n=scan();
            }
            while(!isWhiteSpace(n)&&n!='.')
            {
                if(n>='0'&&n<='9')
                {
                    doub*=10;
                    doub+=n-'0';
                    n=scan();
                }
                else throw new InputMismatchException();
            }
            if(n=='.')
            {
                n=scan();
                double temp=1;
                while(!isWhiteSpace(n))
                {
                    if(n>='0'&&n<='9')
                    {
                        temp/=10;
                        doub+=(n-'0')*temp;
                        n=scan();
                    }
                    else throw new InputMismatchException();
                }
            }
            return doub*neg;
        }
        public String scanString()throws IOException
        {
            StringBuilder sb=new StringBuilder();
            int n=scan();
            while(isWhiteSpace(n))
            n=scan();
            while(!isWhiteSpace(n))
            {
                sb.append((char)n);
                n=scan();
            }
            return sb.toString();
        }
        private boolean isWhiteSpace(int n)
        {
            if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1)
            return true;
            return false;
        }
    }
    public static void main(String args[]) throws IOException {
        Scan input=new Scan();
        int test=input.scanInt();
        StringBuilder ans=new StringBuilder("");
        for(int t=1;t<=test;t++) {
            int n=input.scanInt();
            int k=input.scanInt();
            int arr[]=new int[n];
            for(int i=0;i<n;i++) {
                arr[i]=input.scanInt();
            }
            ans.append(solve(n,k,arr)+"\n");
        }
        System.out.println(ans);
    }
    public static long solve(int n,int k,int arr[]) {
        for(int i=0;i<n;i++) {
            if(arr[i]%k==0) {
                arr[i]=0;
                continue;
            }
            int tmp=arr[i]/k;
            arr[i]=((tmp+1)*k)-arr[i];
        }
        Arrays.sort(arr);
//        for(int i=0;i<n;i++) {
//            System.out.print(arr[i]+" ");
//        }
//        System.out.println();

        long max=-1,max_mod=-1;
        for(int i=0;i<n;i++) {
            int j=i;
            long cnt=0;
            while(j<n && arr[j]==arr[i]) {
                j++;
                cnt++;
            }
            if(arr[i]!=0 && cnt>=max) {
                max=cnt;
                max_mod=arr[i];
            }
            j=i-1;
        }
        if(max==-1) {
            return 0;
        }
//        System.out.println(max+" "+max_mod);
        long times=(max-1)*(k);
        times+=max_mod+1;
        return times;
    }
}


import java.util.*;
import java.io.*;
public class Main {
    static class Scan {
        private byte[] buf=new byte[1024];
        private int index;
        private InputStream in;
        private int total;
        public Scan()
        {
            in=System.in;
        }
        public int scan()throws IOException
        {
            if(total<0)
            throw new InputMismatchException();
            if(index>=total)
            {
                index=0;
                total=in.read(buf);
                if(total<=0)
                return -1;
            }
            return buf[index++];
        }
        public int scanInt()throws IOException
        {
            int integer=0;
            int n=scan();
            while(isWhiteSpace(n))
            n=scan();
            int neg=1;
            if(n=='-')
            {
                neg=-1;
                n=scan();
            }
            while(!isWhiteSpace(n))
            {
                if(n>='0'&&n<='9')
                {
                    integer*=10;
                    integer+=n-'0';
                    n=scan();
                }
                else throw new InputMismatchException();
            }
            return neg*integer;
        }
        public double scanDouble()throws IOException
        {
            double doub=0;
            int n=scan();
            while(isWhiteSpace(n))
            n=scan();
            int neg=1;
            if(n=='-')
            {
                neg=-1;
                n=scan();
            }
            while(!isWhiteSpace(n)&&n!='.')
            {
                if(n>='0'&&n<='9')
                {
                    doub*=10;
                    doub+=n-'0';
                    n=scan();
                }
                else throw new InputMismatchException();
            }
            if(n=='.')
            {
                n=scan();
                double temp=1;
                while(!isWhiteSpace(n))
                {
                    if(n>='0'&&n<='9')
                    {
                        temp/=10;
                        doub+=(n-'0')*temp;
                        n=scan();
                    }
                    else throw new InputMismatchException();
                }
            }
            return doub*neg;
        }
        public String scanString()throws IOException
        {
            StringBuilder sb=new StringBuilder();
            int n=scan();
            while(isWhiteSpace(n))
            n=scan();
            while(!isWhiteSpace(n))
            {
                sb.append((char)n);
                n=scan();
            }
            return sb.toString();
        }
        private boolean isWhiteSpace(int n)
        {
            if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1)
            return true;
            return false;
        }
    }
    public static void main(String args[]) throws IOException {
        Scan input=new Scan();
        int test=input.scanInt();
        StringBuilder ans=new StringBuilder("");
        for(int t=1;t<=test;t++) {
            int n=input.scanInt();
            int k=input.scanInt();
            int arr[]=new int[n];
            for(int i=0;i<n;i++) {
                arr[i]=input.scanInt();
            }
            ans.append(solve(n,k,arr)+"\n");
        }
        System.out.println(ans);
    }
    public static long solve(int n,int k,int arr[]) {
        for(int i=0;i<n;i++) {
            if(arr[i]%k==0) {
                arr[i]=0;
                continue;
            }
            int tmp=arr[i]/k;
            arr[i]=((tmp+1)*k)-arr[i];
        }
        Arrays.sort(arr);
//        for(int i=0;i<n;i++) {
//            System.out.print(arr[i]+" ");
//        }
//        System.out.println();

        long max=-1,max_mod=-1;
        for(int i=0;i<n;i++) {
            int j=i;
            long cnt=0;
            while(j<n && arr[j]==arr[i]) {
                j++;
                cnt++;
            }
            if(arr[i]!=0 && cnt>=max) {
                max=cnt;
                max_mod=arr[i];
            }
            i=j-1;
        }
        if(max==-1) {
            return 0;
        }
//        System.out.println(max+" "+max_mod);
        long times=(max-1)*(k);
        times+=max_mod+1;
        return times;
    }
}


import java.util.*;
import java.io.*;
public class Main {
    static class Scan {
        private byte[] buf=new byte[1024];
        private int index;
        private InputStream in;
        private int total;
        public Scan()
        {
            in=System.in;
        }
        public int scan()throws IOException
        {
            if(total<0)
            throw new InputMismatchException();
            if(index>=total)
            {
                index=0;
                total=in.read(buf);
                if(total<=0)
                return -1;
            }
            return buf[index++];
        }
        public int scanInt()throws IOException
        {
            int integer=0;
            int n=scan();
            while(isWhiteSpace(n))
            n=scan();
            int neg=1;
            if(n=='-')
            {
                neg=-1;
                n=scan();
            }
            while(!isWhiteSpace(n))
            {
                if(n>='0'&&n<='9')
                {
                    integer*=10;
                    integer+=n-'0';
                    n=scan();
                }
                else throw new InputMismatchException();
            }
            return neg*integer;
        }
        public double scanDouble()throws IOException
        {
            double doub=0;
            int n=scan();
            while(isWhiteSpace(n))
            n=scan();
            int neg=1;
            if(n=='-')
            {
                neg=-1;
                n=scan();
            }
            while(!isWhiteSpace(n)&&n!='.')
            {
                if(n>='0'&&n<='9')
                {
                    doub*=10;
                    doub+=n-'0';
                    n=scan();
                }
                else throw new InputMismatchException();
            }
            if(n=='.')
            {
                n=scan();
                double temp=1;
                while(!isWhiteSpace(n))
                {
                    if(n>='0'&&n<='9')
                    {
                        temp/=10;
                        doub+=(n-'0')*temp;
                        n=scan();
                    }
                    else throw new InputMismatchException();
                }
            }
            return doub*neg;
        }
        public String scanString()throws IOException
        {
            StringBuilder sb=new StringBuilder();
            int n=scan();
            while(isWhiteSpace(n))
            n=scan();
            while(!isWhiteSpace(n))
            {
                sb.append((char)n);
                n=scan();
            }
            return sb.toString();
        }
        private boolean isWhiteSpace(int n)
        {
            if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1)
            return true;
            return false;
        }
    }

    public static void sort(int arr[],int l,int r) {    //sort(arr,0,n-1);
        if(l==r) {
            return;
        }
        int mid=(l+r)/2;
        sort(arr,l,mid);
        sort(arr,mid+1,r);
        merge(arr,l,mid,mid+1,r);
    }
    public static void merge(int arr[],int l1,int r1,int l2,int r2) {
        int tmp[]=new int[r2-l1+1];
        int indx1=l1,indx2=l2;
        //sorting the two halves using a tmp array
        for(int i=0;i<tmp.length;i++) {
            if(indx1>r1) {
                tmp[i]=arr[indx2];
                indx2++;
                continue;
            }
            if(indx2>r2) {
                tmp[i]=arr[indx1];
                indx1++;
                continue;
            }
            if(arr[indx1]<arr[indx2]) {
                tmp[i]=arr[indx1];
                indx1++;
                continue;
            }
            tmp[i]=arr[indx2];
            indx2++;
        }
        //Copying the elements of tmp into the main array
        for(int i=0,j=l1;i<tmp.length;i++,j++) {
            arr[j]=tmp[i];
        }
    }

    public static void sort(long arr[],int l,int r) {    //sort(arr,0,n-1);
        if(l==r) {
            return;
        }
        int mid=(l+r)/2;
        sort(arr,l,mid);
        sort(arr,mid+1,r);
        merge(arr,l,mid,mid+1,r);
    }
    public static void merge(long arr[],int l1,int r1,int l2,int r2) {
        long tmp[]=new long[r2-l1+1];
        int indx1=l1,indx2=l2;
        //sorting the two halves using a tmp array
        for(int i=0;i<tmp.length;i++) {
            if(indx1>r1) {
                tmp[i]=arr[indx2];
                indx2++;
                continue;
            }
            if(indx2>r2) {
                tmp[i]=arr[indx1];
                indx1++;
                continue;
            }
            if(arr[indx1]<arr[indx2]) {
                tmp[i]=arr[indx1];
                indx1++;
                continue;
            }
            tmp[i]=arr[indx2];
            indx2++;
        }
        //Copying the elements of tmp into the main array
        for(int i=0,j=l1;i<tmp.length;i++,j++) {
            arr[j]=tmp[i];
        }
    }

    public static void main(String args[]) throws IOException {
        Scan input=new Scan();
        int test=input.scanInt();
        StringBuilder ans=new StringBuilder("");
        for(int t=1;t<=test;t++) {
            int n=input.scanInt();
            int k=input.scanInt();
            int arr[]=new int[n];
            for(int i=0;i<n;i++) {
                arr[i]=input.scanInt();
            }
            ans.append(solve(n,k,arr)+"\n");
        }
        System.out.println(ans);
    }
    public static long solve(int n,int k,int arr[]) {
        for(int i=0;i<n;i++) {
            if(arr[i]%k==0) {
                arr[i]=0;
                continue;
            }
            int tmp=arr[i]/k;
            arr[i]=((tmp+1)*k)-arr[i];
        }
        sort(arr,0,arr.length-1);
//        for(int i=0;i<n;i++) {
//            System.out.print(arr[i]+" ");
//        }
//        System.out.println();

        long max=-1,max_mod=-1;
        for(int i=0;i<n;i++) {
            int j=i;
            long cnt=0;
            while(j<n && arr[j]==arr[i]) {
                j++;
                cnt++;
            }
            if(arr[i]!=0 && cnt>=max) {
                max=cnt;
                max_mod=arr[i];
            }
            i=j-1;
        }
        if(max==-1) {
            return 0;
        }
//        System.out.println(max+" "+max_mod);
        long times=(max-1)*(k);
        times+=max_mod+1;
        return times;
    }
}


import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        while(t-->0)
        {
            int n=sc.nextInt();
           int k=sc.nextInt();
           int f=-1;
           HashMap<Integer,Integer> h=new HashMap<>();

           for(int i=0;i<n;i++)
           {
               int a=sc.nextInt();
               int r=(a<=k)?(k-a):(k-(a%k));
               if(r==0)
               {
               continue;}
               if(h.containsKey(r))
               {
                   h.put(r,h.get(r)+1);
               }
               else
               h.put(r,1);
           }
           int ans=-1,c=-1;
           for(int i=0;i<k;i++)
           {
               if(!h.containsKey(i))
                {
                continue;}
               int freq=h.get(i);
               int temp=1+i+(freq-1)*k;
               ans=Math.max(ans,temp);
            //   if(ans==freq)
            //   c=i;
           }
           if(ans<0)
           System.out.println(0);

           else
           System.out.println(ans);
           //1+c+(ans-1)*k
        }
       // System.out.println("XXXXXXXX");
    }
}


import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        while(t-->0)
        {
            long n=sc.nextLong();
           long k=sc.nextLong();
           int f=-1;
           HashMap<Integer,Integer> h=new HashMap<>();

           for(int i=0;i<n;i++)
           {
               long a=sc.nextLong();
               int r=(int)((a<=k)?(k-a):(k-(a%k)));
               if(a%k==0)
               {
               continue;}
               if(h.containsKey(r))
               {
                   h.put(r,h.get(r)+1);
               }
               else
               h.put(r,1);
           }
           long ans=-1,c=-1;

           for (Map.Entry entry : h.entrySet())
           {
                long i=(int)entry.getKey();
               long freq=(int)entry.getValue();

               long temp=1+i+(freq-1)*k;
               ans=Math.max(ans,temp);
           }

           if(ans<0)
           System.out.println(0);

           else
           System.out.println(ans);
           //1+c+(ans-1)*k
        }
       // System.out.println("XXXXXXXX");
    }
}


import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        while(t-->0)
        {
            int n=sc.nextInt();
           int k=sc.nextInt();
           int f=-1;
           HashMap<Integer,Integer> h=new HashMap<>();

           for(int i=0;i<n;i++)
           {
               int a=sc.nextInt();
               int r=(a<=k)?(k-a):(k-(a%k));
               if(r==0)
               {f=1;
               continue;}
               if(h.containsKey(r))
               {
                   h.put(r,h.get(r)+1);
               }
               else
               h.put(r,1);
           }
           int ans=-1,c=-1;
           for(int i=0;i<k;i++)
           {
               if(!h.containsKey(i))
                {
                continue;}
               int freq=h.get(i);
               int temp=1+i+(freq-1)*k;
               ans=Math.max(ans,temp);
            //   if(ans==freq)
            //   c=i;
           }
           if(ans<0 && f==-1)
           System.out.println(0);
           else if(ans<0&& f==1)
           System.out.println(1);
           else
           System.out.println(ans);
           //1+c+(ans-1)*k
        }
       // System.out.println("XXXXXXXX");
    }
}


import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        while(t-->0)
        {
            int n=sc.nextInt();
           int k=sc.nextInt();
           HashMap<Integer,Integer> h=new HashMap<>();

           for(int i=0;i<n;i++)
           {
               int a=sc.nextInt();
               int r=(a<=k)?(k-a):(k-(a%k));

               if(h.containsKey(r))
               {
                   h.put(r,h.get(r)+1);
               }
               else
               h.put(r,1);
           }
           int ans=-1,c=-1;
           for(int i=0;i<k;i++)
           {
               if(!h.containsKey(i))
                {
                continue;}
               int freq=h.get(i);
               int temp=1+i+(freq-1)*k;
               ans=Math.max(ans,temp);
            //   if(ans==freq)
            //   c=i;
           }
           if(ans<0)
           System.out.println(0);
           else
           System.out.println(ans);
           //1+c+(ans-1)*k
        }
       // System.out.println("XXXXXXXX");
    }
}


import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        while(t-->0)
        {
            int n=sc.nextInt();
           int k=sc.nextInt();
           int f=-1;
           HashMap<Integer,Integer> h=new HashMap<>();

           for(int i=0;i<n;i++)
           {
               int a=sc.nextInt();
               int r=(a<=k)?(k-a):(k-(a%k));
               if(a%k==0)
               {
               continue;}
               if(h.containsKey(r))
               {
                   h.put(r,h.get(r)+1);
               }
               else
               h.put(r,1);
           }
           int ans=-1,c=-1;

           for (Map.Entry entry : h.entrySet())
           {
                int i=(int)entry.getKey();
               int freq=(int)entry.getValue();

               int temp=1+i+(freq-1)*k;
               ans=Math.max(ans,temp);
           }

        //   for(int i=0;i<k;i++)
        //   {
        //       if(!h.containsKey(i))
        //         {
        //         continue;}
        //       int freq=h.get(i);
        //       int temp=1+i+(freq-1)*k;
        //       ans=Math.max(ans,temp);
        //     //   if(ans==freq)
        //     //   c=i;
        //   }
           if(ans<0)
           System.out.println(0);

           else
           System.out.println(ans);
           //1+c+(ans-1)*k
        }
       // System.out.println("XXXXXXXX");
    }
}


import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        while(t-->0)
        {
            int n=sc.nextInt();
           int k=sc.nextInt();
           HashMap<Integer,Integer> h=new HashMap<>();

           for(int i=0;i<n;i++)
           {
               int a=sc.nextInt();
               int r=(a<=k)?(k-a):(k-(a%k));

               if(h.containsKey(r))
               {
                   h.put(r,h.get(r)+1);
               }
               else
               h.put(r,1);
           }
           int ans=-1,c=-1;
           for(int i=0;i<k;i++)
           {
               if(!h.containsKey(i))
                {
                continue;}
               int freq=h.get(i);

               ans=Math.max(ans,freq);
               if(ans==freq)
               c=i;
           }
           if(ans<0)
           System.out.println(0);
           else
           System.out.println(1+c+(ans-1)*k);
        }
       // System.out.println("XXXXXXXX");
    }
}


import java.util.Arrays;
import java.util.Scanner;

public class x {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int t=sc.nextInt();
		while(t-->0){
			int n=sc.nextInt(),k=sc.nextInt();
			long arr[]=new long[n];
			long x=0;
			for(int i=0;i<n;i++){
				arr[i]=sc.nextInt();
				arr[i]%=k;
				arr[i]=arr[i]==0?0:k-arr[i];
			}
			Arrays.sort(arr);
			for(int i=1;i<n;i++){
				if(arr[i]!=0&&arr[i]%k==arr[i-1]%k){
					arr[i]=arr[i-1]+k;
				}
			}
			Arrays.sort(arr);
			System.out.println(arr[n-1]==0?0:arr[n-1]+1);
		}
	}
}


import java.util.*;
public class add {
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        while(t-->0){
            int n=sc.nextInt();
            int k=sc.nextInt();
            char[] rem=new char[k];
            for(int i=0;i<n;i++){
                int a=sc.nextInt();
                if(a%k!=0){
                    int s=(int)rem[k-a%k]+1;
                    rem[k-a%k]=(char) s;
                }
            }
            int max=0,count=0;
            for(int j=1;j<k;j++){
                if(rem[j]>max){
                    max=rem[j];
                }
            }
            int i;
            if(max>1){
                for(i=k-1;i>0;i--){
                    if(rem[i]==max){
                        break;
                    }
                }
                count=i+((rem[i]-1)*k)+1;
            }
            else{
                if(max==0){
                    count=0;
                }
                else{
                    for(i=k-1;i>0;i--){
                        if(rem[i]==max){
                            break;
                        }
                    }
                    count=i+1;
                }

            }
            System.out.println(count);
        }
    }
}


import java.util.*;
public class add {
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        while(t-->0){
            int n=sc.nextInt();
            int k=sc.nextInt();
            int[] rem=new int[k];
            for(int i=0;i<n;i++){
                int a=sc.nextInt();
                if(a%k!=0){
                    rem[k-a%k]++;
                }
            }
            int max=0,count=0;
            for(int j=1;j<k;j++){
                if(rem[j]>max){
                    max=rem[j];
                }
            }
            int i;
            if(max>1){
                for(i=k-1;i>0;i--){
                    if(rem[i]==max){
                        break;
                    }
                }
                count=i+((rem[i]-1)*k)+1;
            }
            else{
                if(max==0){
                    count=0;
                }
                else{
                    for(i=k-1;i>0;i--){
                        if(rem[i]==max){
                            break;
                        }
                    }
                    count=i+1;
                }

            }
            System.out.println(count);
        }
    }
}