#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int) (x).size()

const ll mod = 998244353, mod2 = 1e9 + 7, P = 1e5 + 3;

ll hashh(vector<int> p) {
    ll h1 = 0, h2 = 0;
    for (int el : p) {
        h1 = (h1 * P + el) % mod;
        h2 = (h2 * P + el) % mod;
    }
    return h1 * mod2 + h2;
}

vector<int> op1(vector<int> p) {
    reverse(all(p)); return p;
}
vector<int> op2(vector<int> p) {
    for (auto &u : p) u = sz(p) + 1 - u;
    return p;
}

vector<int> op3(vector<int> p) {
    vector<int> q(sz(p));
    int n = sz(p);
    for (int i = 0; i < n; i++) q[p[i] - 1] = i + 1;
    return q;
}

ll sum(ll x, ll y) {
    return (x+y) % mod;
}

ll mul(ll x, ll y) {
    return (x*y)%mod;
}

ll bp(ll x, ll y) {
    ll ans = 1;
    while (y) {
        if (y&1) ans = mul(ans, x);
        x = mul(x, x);
        y>>=1;
    }
    return ans;
}

ll score(vector<int> p) {
    ll ans = 0;
    for(int j =0; j < sz(p);j ++) ans += bp(p[j], j + 1);
    return ans % mod;
}

void solve() {
    int n, q; cin >> n >> q;
    vector<int> p(n);
    for (auto &u : p) cin >> u;
    
    map<ll, array<ll, 4>> lol;
    map<ll, vector<int>> perm;
    // score, per[0], per[1], per[2]
    auto addp = [&](vector<int> q) {
        ll h = hashh(q);
        ll sc = score(q);
        ll h1 = hashh(op1(q)), h2 = hashh(op2(q)), h3 = hashh(op3(q));
        perm[h] = q;
        perm[h1] = op1(q);
        perm[h2] = op2(q);
        perm[h3] = op3(q);
        lol[h] ={sc,h1,h2,h3};
    };

    addp(p);
    ll ch = hashh(p);
    while (q--) {
        int op; cin >> op;
        ll newh = lol[ch][op];
        if (lol.find(newh) == lol.end()) {
            addp(perm[newh]);
        }
        ch = newh;
        cout<< lol[newh][0] << ' ';
    }
    cout << '\n';
}

signed main() {
    int t = 1;
    #ifdef LOCAL
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
        cin >> t;
    #else 
        cin.tie(0)->sync_with_stdio(false);
    #endif 

    while (t--) solve();

    return 0;
}