import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.util.Arrays; import java.util.Stack; /** * * @author cteam33 */ public class regulate { static int colors[] = new int[101]; static CS cs[][] = new CS[8001][101]; static Stack s = new Stack(); public static void main(String[] args) throws IOException { int N,M,C,T; String line; byte cable[][] = new byte[8001][8001]; BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int i,j,s1,s2,c; for(i=0;i<8001;i++) { for(j=0;j<101;j++){ cs[i][j] = new CS(); } } while(true){ line = reader.readLine(); while(line.trim().equals("")) line = reader.readLine(); String[] arr = line.split(" "); N = Integer.parseInt(arr[0]); M = Integer.parseInt(arr[1]); C = Integer.parseInt(arr[2]); T = Integer.parseInt(arr[3]); if( N == 0 && M == 0 && C == 0 && T == 0) break; for( i = 0;i<=N; i++) { Arrays.fill(cable[i], (byte)0); for(j=0;j<=C;j++) { cs[i][j].id1 = -1; cs[i][j].id2 = -1; cs[i][j].color = -1; } Arrays.fill(colors, 0); } for( i = 0; i < M; i++) { line = reader.readLine(); while(line.trim().equals("")) line = reader.readLine(); arr = line.split(" "); s1 = Integer.parseInt(arr[0]); s2 = Integer.parseInt(arr[1]); c = Integer.parseInt(arr[2]); cable[s1][s2] = (byte) c; cable[s2][s1] = (byte) c; if(cs[s1][c].id1 == -1) cs[s1][c].id1 = s2; else cs[s1][c].id2 = s2; if(cs[s2][c].id1 == -1) cs[s2][c].id1 = s1; else cs[s2][c].id2 = s1; } for(i=0;i<=N;i++) { for(j=0;j<=C;j++) { if(cs[i][j].color == -1 && cs[i][j].id1 != -1) prohledej(i,j); } } for( i = 0; i < T; i++) { line = reader.readLine(); while(line.trim().equals("")) line = reader.readLine(); arr = line.split(" "); s1 = Integer.parseInt(arr[0]); s2 = Integer.parseInt(arr[1]); c = Integer.parseInt(arr[2]); if( cable[s1][s2] == (byte) 0) { System.out.println("No such cable."); } else if( cable[s1][s2] == (byte) c) { System.out.println("Already owned."); } else if( cs[s1][c].id2 != -1 || cs[s2][c].id2 != -1) { System.out.println("Forbidden: monopoly."); } else if(cs[s1][c].color == cs[s2][c].color && cs[s2][c].color != -1) { System.out.println("Forbidden: redundant."); } else { System.out.println("Sold."); int l = cable[s1][s2]; if(cs[s1][l].id1 == s2) { cs[s1][l].id1 = cs[s1][l].id2; cs[s1][l].id2 = -1; } else { cs[s1][l].id2 = -1; } if(cs[s2][l].id1 == s1) { cs[s2][l].id1 = cs[s2][l].id2; cs[s2][l].id2 = -1; } else { cs[s2][l].id2 = -1; } cable[s1][s2] = (byte)c; cable[s2][s1] = (byte)c; if(cs[s1][c].id1 == -1) cs[s1][c].id1 = s2; else cs[s1][c].id2 = s2; if(cs[s2][c].id1 == -1) cs[s2][c].id1 = s1; else cs[s2][c].id2 = s1; prohledej(s1,c); } } System.out.println(); } } static void prohledej(int i, int c) { s.clear(); int b = colors[c]++; s.push(i); cs[i][c].color = b; while(!s.empty()) { int server = s.pop(); int a = cs[server][c].id1; if(a != -1 && cs[a][c].color != b) { s.push(a); cs[a][c].color = b; } a = cs[server][c].id2; if(a != -1 && cs[a][c].color != b) { s.push(a); cs[a][c].color = b; } } } static class CS { public int id1; public int id2; public int color; } }