Give instructions to the user, what to do
When you write your code, you may remember when and what input is required. But maybe a few days/weeks later it wouldn't be so clear. For a 3rd party (e.g. people try to look at your code) it could be quite difficult to understand what you're trying to achieve if you have no instructions printed on the screen or no comment in your code.
Source of the segmentation fault
I've looked into your code (added some printing to the screen to allow better tracking what the program does and what the user is required to do). I still do not have a proper understanding of what you're trying to do, but the problem is within the
for (int i = 0; i < n; i++)
The problem is, that you repeat this loop n times (which was a user input) on q_temp, which is another user input. To be exact, on the split segments of q_temp. Now, the problem is, that this would work only properly, when n matches the number of segments in q_temp. That's probably hardly ever the case.
I'd suggest rethinking of your code.
Probably the best option is to repeat the loop as long as q_temp has unprocessed segments and not based on a fixed number (n).