#include <iostream>
#include <vector>
#include <array>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <tuple>
#include <bitset>
#include <memory>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <numeric>
#include <climits>
#include <cfloat>
#include <cassert>
struct Attributes {
	int id;
	std::vector<int> parameters;
};
class PriorityQueueLike {
	std::vector<int> rank;
	std::priority_queue<int, std::vector<int>, std::function<bool(const int, const int)>> queue;
public:
	PriorityQueueLike(std::vector<int>&& _rank) : rank{ std::move(_rank) } {
		queue = std::priority_queue<int, std::vector<int>, std::function<bool(const int, const int)>>([&](const int a, const int b) {return rank[a] < rank[b]; });
	}
	void push(int id) {
		queue.push(id);
	}
};
int factorial(const int n) {
	switch (n) {
	case 0: return 1;
	case 1: return 1;
	default: return n * factorial(n - 1);
	}
}
void set_ranks(std::vector<int> current_rank, int current, int count, int state, std::vector<int> &history, std::vector<std::pair<int, std::vector<int>>>& result, const std::vector<Attributes>& attr) {
	std::stable_sort(current_rank.begin(), current_rank.end(), [&attr, current](const int a, const int b) {return attr[a].parameters[current] > attr[b].parameters[current]; });
	if (state + 1 == (1 << count)) {
		int as_int = 0;
		for (auto i = 1; i <= history.size(); ++i) {
			as_int = as_int * 10 + history[history.size() - i];
		}
		std::vector<int> rank(current_rank.size());
		for (auto i = 0; i < current_rank.size(); ++i) {
			rank[current_rank[i]] = i;
		}
		result.push_back(std::make_pair(as_int, std::move(rank)));
	}
	else {
		for (auto next = 0; next < count; ++next) if ((state & (1 << next)) == 0) {
			history.push_back(next);
			set_ranks(current_rank, next, count, state | (1 << next), history, result, attr);
			history.pop_back();
		}
	}
}
int main()
{
	int n, k; std::cin >> n >> k;
	std::vector<Attributes> tasks(n);
	for (auto i = 0; i < n; ++i) {
		tasks[i].id = i;
		tasks[i].parameters.resize(k);
		for (auto& f : tasks[i].parameters) std::cin >> f;
	}
	std::vector<std::pair<int, std::vector<int>>> ranks; ranks.reserve(factorial(k));
	std::unordered_map<int, std::priority_queue<int, std::vector<int>, std::function<bool(const int a, const int b)>>> queues;
	{
		std::vector<int> vec(n), history; std::iota(vec.begin(), vec.end(), 0);
		for (auto i = 0; i < k; ++i) {
			history.push_back(i);
			set_ranks(vec, i, k, 1 << i, history, ranks, tasks);
			history.pop_back();
		}
		for (const auto& pair : ranks) {
			queues[pair.first] = std::priority_queue<int, std::vector<int>, std::function<bool(const int a, const int b)>>([&pair](const int a, const int b) {return pair.second[a] > pair.second[b]; });
		}
	}
	std::vector<std::vector<int>> next_task(n);
	std::vector<int> prev_count(n, 0);
	{
		int d; std::cin >> d;
		for (auto i = 0; i < d; ++i) {
			int a, b; std::cin >> a >> b; --a; --b;
			next_task[a].push_back(b);
			prev_count[b]++;
		}
	}
	for (auto& pair : queues) {
		for (auto i = 0; i < n; ++i) if (prev_count[i] == 0) {
			pair.second.push(i);
		}
	}
	std::vector<bool> executed(n, false);
	std::vector<int> result; result.reserve(n);
	int priority = 0;
	for (auto i = 0; i < k; ++i) {
		int e; std::cin >> e;
		priority = priority * 10 + e - 1;
	}
	auto queue = &queues[priority];
	int r; std::cin >> r;
	int last = 0;
	for (auto i = 0; i < r; ++i) {
		int m; std::cin >> m;
		priority = 0;
		for (auto j = 0; j < k; ++j) {
			int e; std::cin >> e;
			priority = priority * 10 + e - 1;
		}
		for (; last < m; ++last) {
			while (executed[queue->top()]) queue->pop();
			auto task = queue->top();
			executed[task] = true;
			result.push_back(task);
			if (!next_task[task].empty()) {
				for (const auto next : next_task[task]) {
					--prev_count[next];
				}
				for (auto& pair : queues) {
					for (const auto next : next_task[task]) if (prev_count[next] == 0) {
						pair.second.push(next);
					}
				}
			}
		}
		queue = &queues[priority];
	}
	for (; last < n; ++last) {
		while (executed[queue->top()]) queue->pop();
		auto task = queue->top();
		executed[task] = true;
		result.push_back(task);
		if (!next_task[task].empty()) {
			for (const auto next : next_task[task]) {
				if (--prev_count[next] == 0) queue->push(next);
			}
		}
	}
	for (const auto r : result) std::cout << r + 1 << '\n';
}



Measured memory_bytes: 17568