import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.StringWriter; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.util.LinkedList; import java.util.StringTokenizer; /** * * @author cteam66 */ public class regulate { public static void main(String args[]) throws IOException { StringWriter sw = new StringWriter(); BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in)); String line = bfr.readLine(); while(!line.equals("0 0 0 0")) { StringTokenizer tokenizer = new StringTokenizer(line); int peaks = Integer.valueOf(tokenizer.nextToken()); int edges = Integer.valueOf(tokenizer.nextToken()); int companies = Integer.valueOf(tokenizer.nextToken()); int orders = Integer.valueOf(tokenizer.nextToken()); Graph graph = new Graph(peaks); //System.out.print(edges+" "+orders); for(int i = 0; i < edges; i++) { tokenizer = new StringTokenizer(bfr.readLine()); graph.addEdge(Integer.valueOf(tokenizer.nextToken())-1, Integer.valueOf(tokenizer.nextToken())-1, Integer.valueOf(tokenizer.nextToken())); } for(int i = 0; i < orders; i++) { tokenizer = new StringTokenizer(bfr.readLine()); sw.append(graph.check(Integer.valueOf(tokenizer.nextToken())-1, Integer.valueOf(tokenizer.nextToken())-1, Integer.valueOf(tokenizer.nextToken()))); sw.append("\n"); } sw.append("\n"); line = bfr.readLine(); } System.out.print(sw); } private static class Graph { int[][] matrix; public Graph(int size) { matrix = new int[size][size]; } public void addEdge(int from, int to, int company) { matrix[from][to] = company; matrix[to][from] = company; } public boolean checkMonopoly(int from, int to, int company) { int count = 0; for(int i = 0; i < matrix.length; i++) { if(matrix[from][i] == company) count++; if(count == 2) return true; } count = 0; for(int i = 0; i < matrix.length; i++) { if(matrix[to][i] == company) count++; if(count == 2) return true; } return false; } public boolean BFS(int from, int to, int company) { LinkedList list = new LinkedList(); list.add(to); int[] field = new int[matrix.length]; while(!list.isEmpty()) { int cur = list.poll(); field[cur] = 1; for(int i = 0; i < matrix.length; i++) { if(matrix[cur][i] == company && field[i] == 0) { if(i == from) return true; list.add(i); } } } return false; } public String check(int from, int to, int company) { if(matrix[from][to] == 0) { return "No such cable."; } else if(matrix[from][to] == company) { return "Already owned."; } else if(checkMonopoly(from, to, company)) { return "Forbidden: monopoly."; } else if(BFS(from, to, company)) { return "Forbidden: redudant."; } else { matrix[from][to] = company; matrix[to][from] = company; return "Sold."; } } } }