I recently learned that if the initial state is equal to the final state in the $p$-$V$ diagram then the internal energy is equal to zero so that means $Q=W$. My question is how can you know that only from the $p$-$V$ diagram without knowing the amount of Q? ( I'm taking generally not only in the case of ideal gasses)
Because U is a state funcion of P and V. This means that U isn't influenced by how a system arrived to a particular state (to a particular P and V) but just by the P and V that the system has at the moment. Therefor, if you draw a closed curve on the Clapryron plane, you will mean that (after some changes that U doesn't care about) you will be back with the same P and V, so the same U.
Also, note that you can say that U is constant, but you can't determine its value.
When you say “ the initial state is equal to the final state” to me that means all the properties of the substance (Pressure, volume, temperature, internal energy, entropy, etc.) are the same at the initial and final states. Heat (Q) and Work (W), are energy transfers between the system and surroundings. If the initial and final states are the same, the algebraic sum of the heat transfers (Q) and work transfers (W) between the system and surroundings in going from the initial and final states is zero, regardless of whether or not the system is an ideal gas.
Hope this helps.