#include <bits/stdc++.h>
using ll = long long;
using ull = unsigned long long;
int main(int argc, char *argv[]) {
  int n, m;
  std::cin >> n >> m;
  std::unordered_map<int, int> hToPow;
  hToPow.reserve(2 * n);
  std::multimap<int, int> powToH;
  for (int i = 1; i <= 2 * n; ++i) {
    int pow;
    std::cin >> pow;
    hToPow.insert({i, pow});
    powToH.insert({pow, i});
  }
  auto eraseFromPowToH = [&powToH](int pow, int h) {
    auto eqRange = powToH.equal_range(pow);
    for (auto it = eqRange.first; it != eqRange.second; ++it) {
      if (it->second == h) {
        powToH.erase(it);
        break;
      }
    }
  };
  auto calcDiff = [](int a, int b) { return std::max(a, b) - std::min(a, b); };
  std::unordered_map<int, int> pairs;
  pairs.reserve(2 * m);
  std::multimap<int, std::pair<int, int>> diffToPair;
  for (int i = 0; i < m; ++i) {
    int h1, h2;
    std::cin >> h1 >> h2;
    pairs[h1] = h2;
    pairs[h2] = h1;
    int diff = calcDiff(hToPow[h1], hToPow[h2]);
    diffToPair.insert({diff, std::make_pair(h1, h2)});
  }
  auto eraseFromDiffToPair = [&diffToPair](int diff, int h1, int h2) {
    auto eqRange = diffToPair.equal_range(diff);
    for (auto it = eqRange.first; it != eqRange.second; ++it) {
      if (it->second.first == h1 || it->second.first == h2) {
        diffToPair.erase(it);
        break;
      }
    }
  };
  int t;
  std::cin >> t;
  bool myTurn = t == 1;
  int lastEnemyPick = 0, myPick = 0;
  while (!hToPow.empty()) {
    if (myTurn) {
      if (pairs.find(lastEnemyPick) != pairs.end()) {
        myPick = pairs[lastEnemyPick];
        int diff = calcDiff(hToPow[myPick], hToPow[lastEnemyPick]);
        eraseFromDiffToPair(diff, myPick, lastEnemyPick);
        pairs.erase(myPick);
        pairs.erase(lastEnemyPick);
        eraseFromPowToH(hToPow[myPick], myPick);
        eraseFromPowToH(hToPow[lastEnemyPick], lastEnemyPick);
        hToPow.erase(myPick);
        hToPow.erase(lastEnemyPick);
      } else if (!pairs.empty()) {
        if (lastEnemyPick != 0) {
          eraseFromPowToH(hToPow[lastEnemyPick], lastEnemyPick);
          hToPow.erase(lastEnemyPick);
        }
        auto it = diffToPair.rbegin();
        int h1 = it->second.first, h2 = it->second.second;
        myPick = hToPow[h1] > hToPow[h2] ? h1 : h2;
        eraseFromDiffToPair(it->first, h1, h2);
        pairs.erase(h1);
        pairs.erase(h2);
        eraseFromPowToH(hToPow[myPick], myPick);
        hToPow.erase(myPick);
      } else if (!hToPow.empty()) {
        if (lastEnemyPick != 0) {
          eraseFromPowToH(hToPow[lastEnemyPick], lastEnemyPick);
          hToPow.erase(lastEnemyPick);
        }
        if (hToPow.empty()) break;
        auto it = powToH.rbegin();
        myPick = it->second;
        eraseFromPowToH(hToPow[myPick], myPick);
        hToPow.erase(myPick);
      } else {
        break;
      }
      std::cout << myPick << std::endl;
      std::cout.flush();
    } else {
      std::cin >> lastEnemyPick;
      if (lastEnemyPick == -1) break;
    }
    myTurn = !myTurn;
  }
  return 0;
}
