def main(): import sys input = sys.stdin.read().split() idx = 0 N = int(input[idx]); idx +=1 M = int(input[idx]); idx +=1 K = int(input[idx]); idx +=1 L = int(input[idx]); idx +=1 edges = [] for _ in range(L): X = int(input[idx])-1; idx +=1 # 0-based Y = int(input[idx])-1; idx +=1 Z = int(input[idx]); idx +=1 edges.append((-Z, -(X + Y), X, Y)) # Sort by -Z (ascending Z), then by -(X+Y) (ascending X+Y) # Sort the edges in the required order: # First by Z descending, then by (X+Y) descending edges.sort() used_buyers = [False] * N used_items = [False] * M total = 0 for e in edges: Z_neg, sum_neg, X, Y = e Z = -Z_neg if not used_buyers[X] and not used_items[Y]: used_buyers[X] = True used_items[Y] = True total += (1 << Z) print(total) if __name__ == '__main__': main()