uses
	testlib, SysUtils;
label
	endwh;
const
	MAXN = 10000;
var
	it, n, m, i, x, y, j : integer;
	ansj, ansp : string;
	a : array[1..MAXN, 1..MAXN] of boolean;	
	p : array[1..MAXN] of integer;
	u : array[1..MAXN] of boolean;

procedure next;
var i, j, min, tmp : integer;
begin
	j := n;
	while ((j > 1) and (p[j - 1] > p[j])) do begin
		dec(j);
	end;
	if (j = 1) then
		quit(_wa, 'not found');
	min := j;
	for i := j to n do begin
		if ((p[i] > p[j - 1]) and (p[i] < p[min])) then begin
				min := i;
		end;
	end;
	tmp := p[j - 1];
	p[j - 1] := p[min];
	p[min] := tmp;
	for i := 0 to (n - j) div 2 - 1 do begin
		tmp := p[j + i];
		p[j + i] := p[n  - i];
		p[n  - i] := tmp;
	end;
end;

begin
	n := inf.readlongint;
	ansp := ouf.readstring;
    ansj := ans.readstring;
    if (ansp <> ansj) then begin
    	quit(_wa, ansj + ' expected, but ' + ansp + ' found');
    end;
    if ((ansp = 'NO') and (ansj = 'NO')) then begin
    	quit(_ok, IntToStr(n) + ' - NO');
    end;
    m := ouf.readlongint;
    if (m <> n * (n - 1) div 4) then begin
    	quit(_wa, IntToStr(n * (n - 1) div 4) + ' edges expected, but ' + IntToStr(m) + ' edges found');
    end;
    fillchar(a, sizeof(a), 0);	
    for i := 1 to m do begin
		x := ouf.readlongint;
		y := ouf.readlongint;
		a[x][y] := true;
		a[y][x] := true;
    end;
    fillchar(u, sizeof(u), 0);	
	for i := 1 to n do begin
		p[i] := ouf.readlongint;
		if (u[p[i]]) then begin
			quit(_wa, 'bad permutation');
		end;
		u[p[i]] := true;
	end;
	it :=1;
	while true do begin
		inc(it);             
		if (it mod 10000 = 0) then begin
			for i := 1 to n do write(p[i], ' ');
			writeln;	
		end;
	    for i := 1 to n do begin
	    	for j := 1 to n do begin
    			if (i = j) then 
	    			continue;
	    		if (a[i][j] = a[p[i]][p[j]]) then begin
	    			goto endwh;
    			end;
    		end;
	    end;
	    write('!');
		for i := 1 to n do 
			write(p[i], ' ');
		writeln;	
	    quit(_ok, IntToStr(n));
	    endwh:
	    next;
    end;
end.