An integer n (3 ≤ n ≤ 1,000), an integer m (1 ≤ m ≤ 2,000), an integer k (1 ≤ k ≤ 6), and an integer p (1 ≤ p ≤ n).
A list of m tuples, where each tuple contains two integers x_i and y_i (1 ≤ x_i, y_i ≤ n) and an integer w_i (1 ≤ w_i ≤ 1,000).
A list of k tuples, where each tuple contains two integers s_j and t_j (1 ≤ s_j, t_j ≤ n).

### 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:
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
    '''