An integer n (3 ≤ n ≤ 1,000), representing some quantity or size.
An integer m (1 ≤ m ≤ 2,000), representing some quantity or size.
An integer k (1 ≤ k ≤ 6), representing some quantity or size.
An integer p (1 ≤ p ≤ n), representing some quantity or size.
A list of m tuples, each containing three integers (x_i, y_i, w_i) (1 ≤ x_i, y_i ≤ n; 1 ≤ w_i ≤ 1,000), representing some relationships.
A list of k tuples, each containing two integers (s_j, t_j) (1 ≤ s_j, t_j ≤ n), representing some relationships.

### Example Input:
```
5 6 1 1
1 3 5
1 2 2
2 3 1
3 5 4
3 4 2
4 5 3
3 5
```

### Function Signature:
Write a function f(n, m, k, p, edges, mails) that takes in the input. 
def f(n: int, m: int, k: int, p: int, edges: List[Tuple[int, int, int]], mails: List[Tuple[int, int]]): 
    ''' 
    n: an integer 
    m: an integer 
    k: an integer 
    p: an integer 
    edges: a list of tuples of integers 
    mails: a list of tuples of integers
    '''