Doubt in question 'Find the greater element' in STL course

I took a different approach than the editorial provided. I can’t understand the editorial. My approach is failing a test case and some custom inputs as well. Can someone check code to rectify any mistakes?

#include
#include
#include
using namespace std;
int main() {
int n;
cin>>n;
vector v;
for(int i=0;i<n;i++){
int a;
cin>>a;
v.push_back(a);
}

for(int i=0;i<n;i++){
	auto it = upper_bound(v.begin()+i,v.end(),v[i]);
	if(it!=v.end()){
		cout<<*it<<" ";
	}
	else {
		it = upper_bound(v.begin(),v.begin()+i,v[i]);
		if(it!=(v.begin()+i)){
			cout<<*it<<" ";
		}
		else
			cout<<"-1 ";
	}
}
return 0;

}

Custom Input: 7
2 9 3 2 8 1 3
Output ; 3 -1 8 3 -1 3 -1
Desired Output: 9 -1 8 8 9 3 9