#define _GLIBCXX_DEBUG
#include<bits/stdc++.h>

using namespace std;

using ll = long long;

#define f first
#define s second
#define pii pair<ll, ll>

ll const MOD = 998244353;

ll sum(ll a, ll b) {
    return (a + b) % MOD;
}

ll mult(ll a, ll b) {
    return a * b % MOD;
}

ll bin_pow(ll a, ll p) {
    if (p == 0) return 1;
    if (p == 1) return a;
    ll k = bin_pow(a, p / 2);
    return mult(mult(k, k), bin_pow(a, p % 2));
}

ll calc(vector<ll> &p) {
    ll res = 0;
    for (ll i = 0; i < p.size(); ++i) {
        res = sum(res, bin_pow(p[i], i + 1));
    }
    return res;
}

vector<ll> change1(vector<ll> p) {
    vector<ll> res = p;
    reverse(res.begin(), res.end());
    return res;
}


vector<ll> change2(vector<ll> p) {
    vector<ll> res = p;
    for (int i = 0; i < res.size(); ++i) {
        res[i] = res.size() - res[i] + 1;
    }
    return res;
}


vector<ll> change3(vector<ll> p) {
    vector<ll> res = p, buf = p;
    for (int i = 0; i < res.size(); ++i) {
        res[buf[i] - 1] = i + 1;
    }
    return res;
}

int cnt = 0;

vector<vector<int>> to;
vector<int> val;
map<int, int> id;


void rec(vector<int> ord, vector<ll> p, int last) {
    if (ord.size() > 6) return;
    if (ord.size() > 0) {
        if (ord.back() == 1) p = change1(p);
        if (ord.back() == 2) p = change2(p);
        if (ord.back() == 3) p = change3(p);
    }

    ll a = calc(p);

    if (id.find(a) == id.end()) {
        id[a] = cnt++;
        val.push_back(a);
    }


    if (ord.size() > 0) {
        int c = ord.back();
        to[id[last]][c - 1] = id[a];
    }
    for (int i = 1; i <= 3; ++i) {
        ord.push_back(i);
        rec(ord, p, a);
        ord.pop_back();
    }
}



int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n, q;
    cin >> n >> q;
    vector<ll> p(n), b(q);
    for (ll i = 0; i < n; ++i) {
        cin >> p[i];
    }
    for (ll i = 0; i < q; ++i) {
        cin >> b[i];
    }
    to.resize(50, vector<int> (3));
    vector<int> ord;
    rec(ord, p, 0);
//    for (auto el : val) {
//        cout << el << ' ';
//    }
    //cout << endl;
    int last = id[calc(p)];
    //cout << last << endl;
    for (int i = 0; i < q; ++i) {
        last = to[last][b[i] - 1];
        //cout << last << ' ' << to[last][b[i] - 1] << endl;
        cout << val[last] << " ";
    }

}


