#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <unordered_map>
#include <iterator>
#include <stack>
#include <unordered_set>
#include <iomanip>
#include <ctime>
#include <cstring>
#include <bitset>

#include <random>
#define all(x) (x).begin(), (x).end()


using namespace std;
void dout() { cerr << endl; }
typedef long long ll;
template <typename Head, typename... Tail>
void dout(Head H, Tail... T) {
    cerr << H << ' ';
    dout(T...);
}

#define debug(x) cout << #x " = " << (x) << endl

using ll = long long;
using pii = pair<ll, ll>;
using ld = long double;
using pll = pair<long long, long long>;

template <typename T>
void do_uniq(vector<T> vec) {
    sort(all(vec));
    vec.resize(unique(all(vec)) - vec.begin());
}

//#define solvsh
//#define multi
#ifdef solvsh

const int MAXN = 2e5 + 20;

int per[MAXN];
ll costs[MAXN];

ll tree[MAXN];
ll inp[MAXN];
int where[MAXN];

void build(int ver, int tl, int tr) {
    if (tl == tr) {
        tree[ver] = inp[tl];
    } else {

        int mid = (tl + tr) >> 1;

        build(2 * ver, tl, mid);
        build(2 * ver + 1, mid + 1, tr);

        tree[ver] = min(tree[2 * ver], tree[2  *ver + 1]);
    }
}

ll adds[4 * MAXN];

void upd(int v, int tl, int tr, int l, int r, ll add){
    if (l > r)
        return;
    if (tl == l && tr == r){
        adds[v] += add;
        return;
    }

    auto mid = (tl + tr) >> 1;

    upd(2 * v, tl, mid, l, min(mid, r), add);
    upd(2 * v + 1, mid + 1, tr, max(mid + 1, l), r, add);

    tree[v] = min(tree[2 * v], tree[2 * v  +1]) + adds[v];
}

ll get_min(){
    return tree[1];
}


void solve() {
    int n;
    cin >> n;

    for (int i = 0; i < n; ++i) {
        cin >> per[i];
        where[per[i]] = i;
    }

    ll sum = 0;
    for (int i = 0; i < n; ++i) {
        cin >> costs[i];
        sum += costs[i];
    }

    for (int i = 1; i <= n; ++i) {
        inp[i] = inp[i - 1] + costs[where[i]];
    }
    build(1, 1, n);

    ll answr = 1e18;

    for (int i = 0; i < n; ++i) {

        int val = per[i];

        upd(1, 1, n, 1, val - 1, costs[i]);
        upd(1, 1, n, val, n, -costs[i]);

        answr = min(answr, get_min());
    }

    cout << answr;
}

#else

ll mod = 1e9+7;

const int MAXN = 2e5 + 9;


void solve() {

    int n,m, x;
    cin >> n >> m >> x;

    vector<pair<int, vector<int>>> inp;

    for (int i = 0; i < n; ++ i){

        int ci;
        cin >> ci;

        vector<int> v;

        for (int j = 0; j < m; ++ j){
            int val;
            cin >> val;
            v.push_back(val);
        }

        inp.push_back({ci, v});
    }


    int answr = 1e9;

    for (int i = 1; i < (1 << n); ++ i){
        int cost = 0;

        vector<int> level(m, 0);

        for (int j = 0; j < n; ++ j){

            int bt = 1 << j;
            if ((bt & i) != 0){
                cost += inp[j].first;

                int ind = 0;
                for (auto e : inp[j].second){
                    level[ind] += e;
                    ++ind;
                }
            }
        }

        bool ok = true;

        for (auto e : level){
            if (e < x)
                ok = false;
        }
        if (ok){
            answr = min(answr, cost);
        }
    }


    if (answr == 1e9){
        cout << -1;
    } else {
        cout << answr;
    }
}

#endif

void multisolve() {

    int t;
    cin >> t;

    int i = 1;
    while (t--) {
        cout << "Case #" << (i++) << ": ";
        solve();
        cout << "\n";
    }
}


#define int int
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    cout << fixed << setprecision(20);


#ifdef multi
    multisolve();
#else
    solve();
    //    gen();
#endif

}


Measured memory_bytes: 3664