An integer N (1 ≤ N ≤ 50,000), representing some quantity or size.
N lines follow, each containing a string s and an integer a (0 ≤ a ≤ 10^5). The string has a length between 1 and 20 characters and consists of English letters.
An integer M (1 ≤ M ≤ 20,000), representing another quantity.
M lines follow, each containing a string formatted as either "+ t b" or "- t". The string t has a length between 1 and 20 characters and consists of English letters, and b is an integer (0 ≤ b ≤ 10^5).

### Example Input:

```
4
NameA 7
NameB 3
NameC 6
NameD 0
1
+ NameE 2
```

### Function Signature:
Write a function f(N, A, M, B) that takes in the input.
def f(N: int, A: List[Tuple[str, int]], M: int, B: List[Union[Tuple[str, str, int], Tuple[str, str]]]): 
    ''' 
    N: an integer 
    A: a list of tuples, where each tuple contains a string and an integer
    M: an integer
    B: a list of tuples, where each tuple contains either two strings, or a string and an integer
    '''