#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;

int inp[MAXN];
bool usd[MAXN];
int dist[MAXN];

void solve() {

    int n;
    cin >> n;
    ll k;
    cin >> k;

    for (int i = 1; i <= n; ++i) {
        cin >> inp[i];
    }

    if (k < 2 * n){
        int answr = 1;

        for (int i = 0; i < k; ++ i)
            answr = inp[answr];

        cout << answr;
        return;
    }

    int start = 1;
    ll len = 0;
    ll pref = 0;
    int start_cyc;
    dist[1] = 0;

    while (1) {
        if (usd[start]) {
            pref = dist[start];
            start_cyc = start;
            break;
        }
        usd[start] = true;
        if (!usd[inp[start]])
            dist[inp[start]] = dist[start] + 1;
        start = inp[start];
        ++len;
    }

    k -= pref;
    ll  len_cycle = len - pref;

    int cnt = k % len_cycle;
    int answr = start;

    for (int i = 0; i < cnt; ++ i)
        answr = inp[answr];

    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: 5404