The before and after states are shown below. The particle of charge
q will be at rest in the final state.
I choose the reference level for gravitational potential energy at the level of charge Q.
|Gravitational Potential energy||mgd||mgh|
|Electric Potential energy||kqQ/d||kqQ/h|
So, setting total energy before equal to total energy after:
mgd + kqQ/d = mgh + kqQ/h
mg(h-d) = kqQ(1/d-1/h) = kqQ(h-d)/dh