WA in spoj SGIFT

http://www.spoj.com/problems/SGIFT/

my code is(there are no compilation errors)

#include <bits/stdc++.h>
using namespace std;
 map<long long, long long> freq;	
int main(){
long long t, q;
long long v;
scanf("%lld", &t);
for(int i = 0; i < t; ++i){
	scanf("%lld", &v);
	freq[v] += v;
}
map<long long, long long>::iterator ip = freq.begin();
ip++;
for(map<long long, long long>::iterator in = freq.begin(); in != freq.end(); ++in, ip++){
	(*ip).second += (*in).second;
}
scanf("%lld", &q);
while(q--){
	long long a, b;
	scanf("%lld %lld", &a, &b);
	map<long long, long long>::iterator lb, ub;
	lb = freq.lower_bound(a);
	ub = freq.upper_bound(b);
	ub--;
	if(lb == freq.begin()) printf("%lld\n", (*ub).second);
	else{
		lb--;
		printf("%lld\n", (*ub).second - (*lb).second);
	}
}
return 0;
}

sorry, silly mistake, ac!

//