------
## 最终结论
```python
def unhappyFriends(n, preferences, pairs):
    # Create a dictionary to map each friend to their pair
    pair_map = {}
    for x, y in pairs:
        pair_map[x] = y
        pair_map[y] = x

    # Create a preference ranking dictionary for quick lookup
    rank = [[0] * n for _ in range(n)]
    for i in range(n):
        for j, p in enumerate(preferences[i]):
            rank[i][p] = j

    unhappy_count = 0

    # Check each friend if they are unhappy
    for x in range(n):
        y = pair_map[x]
        # Check all friends that x prefers over their current partner y
        for u in preferences[x]:
            if u == y:
                break
            v = pair_map[u]
            # Check if u prefers x over their current partner v
            if rank[u][x] < rank[u][v]:
                unhappy_count += 1
                break

    return unhappy_count

# Example usage:
n1, preferences1, pairs1 = 4, [[1, 2, 3], [3, 2, 0], [3, 1, 0], [1, 2, 0]], [[0, 1], [2, 3]]
n2, preferences2, pairs2 = 2, [[1], [0]], [[1, 0]]
n3, preferences3, pairs3 = 4, [[1, 3, 2], [2, 3, 0], [1, 3, 0], [0, 2, 1]], [[1, 3], [0, 2]]

print(unhappyFriends(n1, preferences1, pairs1)) # Output: 2
print(unhappyFriends(n2, preferences2, pairs2)) # Output: 0
print(unhappyFriends(n3, preferences3, pairs3)) # Output: 4
```