I thought about this a bit. I think the best solution is:

```
if (mon !== toId(template.baseSpecies) && toId(template.baseSpecies) in dex) {
    delete dex[mon];
    continue;
} else {
    template = Tools.getTemplate(template.baseSpecies);
}
```

So, if Melo is in the result set, remove Melo-P, otherwise keep Melo-P in the result set.