#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;
const ll MAXN = 1e5 + 6;
const ll inf = 1e9 + 7;
const ll mod = 998244353;
const ll prod = 53;
mt19937 rnd(2);
unordered_map<ll,ll> per;
ll binpow(ll p,ll n) {
    ll res = 1;
    while (n != 0) {
        if (n & 1) {
            res = (res * p) % mod;
        }
        n >>= 1;
        p = (p * p) % mod;
    }
    return res;
}

int main() {
    ll n,q,a;
    cin >> n >> q;
    vector<ll> arr;
    vector<ll> arr1;
    vector<ll> ans;
    ans.resize(n);
    for (int i = 0;i<n;i++) {
        cin >> a;
        arr.push_back(a);
    }
    arr1 = arr;
    vector<vector<ll>> lint;
    vector<ll> ansRes;
    ll pint = 0;
    for (int tr = 0;tr<100;tr++) {
        a = rnd()%3 + 1;
        if (a == 1) {
            for (int j = 0;j<n;j++) {
                ans[j] = arr[n-j-1];
            }
            arr = ans;
        }
        else if (a == 2) {
            for (int j = 0;j<n;j++) {
                ans[j] = n + 1 - arr[j];
            }
            arr = ans;
        }
        else {
            for (int j = 0;j<n;j++) {
                ans[arr[j]-1] = j + 1;
            }
            arr = ans;
        }
        ll res = 0;
        ll prod1 = 1;
        for (int j = 0;j<n;j++) {
            res = (res + arr[j] * prod1)%mod;
            prod1 = (prod1 * prod)%mod;
        }
        ll res1 = 0;
        for (int j = 0;j<n;j++) {
            res1 = (res1 + binpow(arr[j],j+1))%mod;
        }
        if (per.find(res) == per.end()) {
            per[res] = pint ++;
            lint.push_back({});
            ansRes.push_back(res1);
            for (int j = 0;j<n;j++) {
                lint.back().push_back(arr[j]);
            }
        }
    }
    vector<vector<pair<ll,ll>>> graph;
    graph.resize(lint.size());
    for (int i = 0;i<lint.size();i++) {
        for (int te = 1;te<=3;te++) {
            arr = lint[i];
            if (te == 1) {
                for (int j = 0;j<n;j++) {
                    ans[j] = arr[n-j-1];
                }
                arr = ans;
            }
            else if (te == 2) {
                for (int j = 0;j<n;j++) {
                    ans[j] = n + 1 - arr[j];
                }
                arr = ans;
            }
            else {
                for (int j = 0;j<n;j++) {
                    ans[arr[j]-1] = j + 1;
                }
                arr = ans;
            }
            ll res = 0;
            ll prod1 = 1;
            for (int j = 0;j<n;j++) {
                res = (res + arr[j] * prod1)%mod;
                prod1 = (prod1 * prod)%mod;
            }
            graph[i].push_back({te,per[res]});
        }
    }
    ll res = 0;
    ll prod1 = 1;
    for (int j = 0;j<n;j++) {
        res = (res + arr1[j] * prod1)%mod;
        prod1 = (prod1 * prod)%mod;
    }
    ll ind = per[res];
    for (int i = 0;i<q;i++) {
        cin >> a;
        ind = graph[ind][a-1].second;
        cout << ansRes[ind] << " ";
    }
}
