#include <bits/stdc++.h>
using namespace std;
int const MAXn = 2e3 + 2;
multiset<int> arr;
pair<int, int> dep[MAXn];
int first[MAXn];
int n, k;
int elem[MAXn];
set<int> cola[MAXn];
void sol1() {
  for (int i = 1; i <= k; i++) {
    cout << dep[i].first << "\n";
    fflush(stdout);
    cola[elem[dep[i].first]].erase(dep[i].first);
    arr.erase(arr.find(elem[dep[i].first]));
    int x;
    cin >> x;
    fflush(stdout);
    cola[elem[x]].erase(x);
    arr.erase(arr.find(elem[x]));
  }
  while (!arr.empty()) {
    auto y = arr.end();
    y--;
    int o = *y;
    int u = *cola[o].begin();
    cola[o].erase(u);
    arr.erase(y);
    cout << u << "\n";
    fflush(stdout);
    cin >> o;
    fflush(stdout);
    cola[elem[o]].erase(o);
    arr.erase(arr.find(elem[o]));
  }
}
int l;
void sol2() {
  while (!arr.empty()) {
    int o;
    cin >> o;
    fflush(stdout);
    arr.erase(arr.find(elem[o]));
    cola[elem[o]].erase(o);
    if (first[o]) {
      l++;
      if (dep[first[o]].first == o) {
        cout << dep[first[o]].second << "\n";
        fflush(stdout);
        arr.erase(arr.find(elem[dep[first[o]].second]));
        cola[elem[dep[first[o]].second]].erase(dep[first[o]].second);
        first[dep[first[o]].second] = false;
      } else {
        cout << dep[first[o]].first << "\n";
        fflush(stdout);
        arr.erase(arr.find(elem[dep[first[o]].first]));
        cola[elem[dep[first[o]].second]].erase(dep[first[o]].first);
        first[dep[first[o]].first] = false;
      }
      first[o] = false;
      continue;
    }
    if (l < k) {
      for (int i = 1; i <= k; i++) {
        if (first[dep[i].first]) {
          l++;
          cout << dep[i].first << "\n";
          fflush(stdout);
          first[dep[i].first] = false;
          cola[elem[dep[i].first]].erase(dep[i].first);
          arr.erase(arr.find(elem[dep[i].first]));
          int x;
          cin >> x;
          fflush(stdout);
          cola[elem[x]].erase(x);
          arr.erase(arr.find(elem[x]));
          first[x] = false;
        }
      }
    }
    if (l == k) {
      while (!arr.empty()) {
        auto y = arr.end();
        y--;
        int o = *y;
        int u = *cola[o].begin();
        cola[o].erase(u);
        arr.erase(y);
        cout << u << "\n";
        fflush(stdout);
        if (arr.size() == 0) return;
        cin >> o;
        fflush(stdout);
        cola[elem[o]].erase(o);
        arr.erase(arr.find(elem[o]));
      }
    }
  }
}
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n * 2; i++) {
    int x1;
    cin >> x1;
    elem[i] = x1;
    arr.insert(x1);
    cola[x1].insert(i);
  }
  for (int i = 1; i <= k; i++) {
    cin >> dep[i].first >> dep[i].second;
    first[dep[i].first] = i;
    first[dep[i].second] = i;
    if (elem[dep[i].first] < elem[dep[i].second])
      swap(dep[i].first, dep[i].second);
  }
  int t;
  cin >> t;
  if (t == 1)
    sol1();
  else
    sol2();
  return 0;
}
