#include<bits/stdc++.h>
using namespace std;
int n,m;
char ch[110][110];
int ax=0x3f3f3f3f,bx=0x3f3f3f3f,ay=0,by=0,flag=0,ans=0;
int main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>ch[i][j];
			if(ch[i][j]=='B'){
				flag=1;
				ax=min(ax,i);
				bx=min(bx,j);
				ay=max(ay,i);
				by=max(by,j);
			}
		}
	}
	if(flag==0){
		cout<<1;
		return 0;
	}
	int mb=max(ay-ax,by-bx);
	if(mb>=m||mb>=n){
		cout<<-1;
		return 0;
	}
	for(int i=ay-mb;i<=ay;i++){
		for(int j=by-mb;j<=by;j++){
			if(ch[i][j]=='W'){
				ans++;
			}
		}
	}
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
string mapp[105];
int main(){
    int n,m;
    cin>>n>>m;
    for(int i=0;i<n;i++)cin>>mapp[i];
    int sx=0x3f3f3f3f,sy=0x3f3f3f3f,ex=0,ey=0,cnt=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(mapp[i][j]=='B'){
                sx=min(sx,i);sy=min(sy,j);
                ex=max(ex,i);ey=max(ey,j);
                cnt++;
            }
        }
    }
    if(cnt==0){
        cout<<1;
        return 0;
    }
    int len=max(ex-sx+1,ey-sy+1);
    if(len>n||len>m){
        cout<<-1;
        return 0;
    }
    cout<<len*len-cnt;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
string mapp[105];
int main(){
    int n,m;
    cin>>n>>m;
    for(int i=0;i<n;i++)cin>>mapp[i];
    int sx=0x3f3f3f3f,sy=0x3f3f3f3f,ex=0,ey=0,cnt=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(mapp[i][j]=='B'){
                sx=min(sx,i);sy=min(sy,j);
                ex=max(ex,i);ey=max(ey,j);
            }
        }
    }
    if(cnt==0){
        cout<<1;
        return 0;
    }
    int len=max(ex-sx+1,ey-sy+1);
    if(len>n||len>m){
        cout<<-1;
        return 0;
    }
    cout<<len*len-cnt;
    return 0;
}


/*
 * socode.online
 *
 * Author: 小螺号
 * E-mail: 598601767@qq.com
 * Time  : 2020-04-10 18:39:56
 */
#include <bits/stdc++.h>
using namespace std;

char s[105][105];

int main(){
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++){
        scanf("%s",s[i]);
    }
    int l=105;
    int r=0;
    int u=105;
    int d=0;
    int cnt=0;
    int ans=0;
    int flag=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(s[i][j]=='B'){
                flag=1;
                l=min(l,i);
                r=max(r,i);
                u=min(u,j);
                d=max(d,j);
                cnt++;
            }
        }
    }
    if(flag==1){
        int Max=max(r-l,d-u);
    ans=(Max+1)*(Max+1)-cnt;
    printf("%d\n",ans);
    }
    else{
        printf("1\n");
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,m;
char ch[110][110];
int ax=0x3f3f3f3f,bx=0x3f3f3f3f,ay=0,by=0,flag=0,ans=0;
int main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>ch[i][j];
			if(ch[i][j]=='B'){
				flag=1;
				ax=min(ax,i);
				bx=min(bx,j);
				ay=max(ay,i);
				by=max(by,j);
			}
		}
	}
	if(flag==0){
		cout<<1;
		return 0;
	}
	int mb=max(ay-ax,by-bx);
	if(ay-mb<=0||by-mb<=0){
		cout<<-1;
		return 0;
	}
	for(int i=ay-mb;i<=ay;i++){
		for(int j=by-mb;j<=by;j++){
			if(ch[i][j]=='W'){
				ans++;
			}
		}
	}
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,m;
char ch[110][110];
int ax=0x3f3f3f3f,bx=0x3f3f3f3f,ay=0,by=0,flag=0,sum=0;
int main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>ch[i][j];
			if(ch[i][j]=='B'){
				flag=1;
				ax=min(ax,i);
				bx=min(bx,j);
				ay=max(ay,i);
				by=max(by,j);
				sum++;
			}
		}
	}
	if(flag==0){
		cout<<1;
		return 0;
	}
	int mb=max(ay-ax+1,by-bx+1);
	if(mb>m||mb>n){
		cout<<-1;
		return 0;
	}
	cout<<mb*mb-sum;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
string mapp[105];
int main(){
    int n,m;
    cin>>n>>m;
    for(int i=0;i<n;i++)cin>>mapp[i];
    int sx=-1,sy=-1,ex=0,ey=0,cnt=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(mapp[i][j]=='B'){
                if(sx==-1)sx=i,sy=j;
                ex=i,ey=j;cnt++;
            }
        }
    }
    if(cnt==0){
        cout<<1;
        return 0;
    }
    int len=max(ex-sx+1,ey-sy+1);
    if(len>n||len>m){
        cout<<-1;
        return 0;
    }
    cout<<len*len-cnt;
    return 0;
}


/*
 * socode.online
 *
 * Author: 李策
 * E-mail: 691397536@qq.com
 * Time  : 2020-04-10 18:37:56
 */
#include <bits/stdc++.h>

using namespace std;

char s[105][105];

int main() {
    int n, m;
    scanf("%d %d",&n,&m);
    int u, d, l, r, cnt = 0;
    u = l = 0x3f3f3f3f, d = r = -1;
    for( int i = 0; i < n; i++ ) scanf("%s",s[i]);
    for( int i = 0; i < n; i++ ) {
        for( int j = 0; j < m; j++ ) {
            if( s[i][j] == 'B' ) {
                cnt++;
                u = min( u, i+1 ), d = max( d, i+1 ) , l = min( l, j+1 ), r = max( r, j+1 );
            }
        }
    }
    if( cnt == 0 ) {
        puts("1");
        return 0;
    }
    int side = max(r-l+1,d-u+1);
    if( d < side || r < side ) puts("-1");
    else printf("%d\n",side*side-cnt);
    return 0;
}


/*
 * socode.online
 *
 * Author: 任奕佳
 * E-mail: 1234567890@qq.com
 * Time  : 2020-04-10 18:33:18
 */
#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
int n,m,ax,bx,ay,by,sum;
char s[105][105];
int main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++)scanf("%s",s[i]+1);
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(s[i][j]=='B'){
				sum++;
				ax=min(ax,i);
				bx=max(bx,i);
				ay=min(ay,j);
				by=max(by,j);
			}
		}
	}
	if(sum==0){
		cout<<1<<endl;
		return 0;
	}
	int t=max(bx-ax,by-ay)+1;
	if(t>m||t>n)cout<<-1<<endl;
	else cout<<t*t-sum<<endl;
	return 0;
}


/*
 * socode.online
 *
 * Author: 李策
 * E-mail: 691397536@qq.com
 * Time  : 2020-04-10 18:37:56
 */
#include <bits/stdc++.h>

using namespace std;

char s[105][105];

int main() {
    int n, m;
    scanf("%d %d",&n,&m);
    int u, d, l, r, cnt = 0;
    u = l = 0x3f3f3f3f, d = r = -1;
    for( int i = 0; i < n; i++ ) scanf("%s",s[i]);
    for( int i = 0; i < n; i++ ) {
        for( int j = 0; j < m; j++ ) {
            if( s[i][j] == 'B' ) {
                cnt++;
                u = min( u, i+1 ), d = max( d, i+1 ) , l = min( l, j+1 ), r = max( r, j+1 );
            }
        }
    }
    if( cnt == 0 ) {
        puts("1");
        return 0;
    }
    int side = max(r-l+1,d-u+1);
    if( n < side || m < side ) puts("-1");
    else printf("%d\n",side*side-cnt);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,m;
char ch[110][110];
int ax=0x3f3f3f3f,bx=0x3f3f3f3f,ay=0,by=0,flag=0,ans=0,sum=0;
int main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>ch[i][j];
			if(ch[i][j]=='B'){
				flag=1;
				ax=min(ax,i);
				bx=min(bx,j);
				ay=max(ay,i);
				by=max(by,j);
				sum++;
			}
		}
	}
	if(flag==0){
		cout<<1;
		return 0;
	}
	int mb=max(ay-ax+1,by-bx+1);
	if(mb>=m||mb>=n){
		cout<<-1;
		return 0;
	}
	cout<<mb*mb-sum;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,m;
char ch[110][110];
int ax=0x3f3f3f3f,bx=0x3f3f3f3f,ay=0,by=0,flag=0,ans=0;
int main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>ch[i][j];
			if(ch[i][j]=='B'){
				flag=1;
				ax=min(ax,i);
				bx=min(bx,j);
				ay=max(ay,i);
				by=max(by,j);
			}
		}
	}
	if(flag==0){
		cout<<1;
		return 0;
	}
	int mb=max(ay-ax,by-bx);
	if(mb>=m||mb>=n){
		cout<<-1;
		return 0;
	}
	for(int i=max(ay,by)-mb;i<=max(by,ay);i++){
		for(int j=max(ay,by)-mb;j<=max(ay,by);j++){
			if(ch[i][j]=='W'){
				ans++;
			}
		}
	}
	cout<<ans;
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <math.h>
#include <unordered_map>
#include <string>
#include <algorithm>
#include <string.h>
#include <unordered_set>
#include <set>

using namespace std;

const int N = 105;
char A[N][N];

int main() {
#ifndef ONLINE_JUDGE
    freopen("/Users/TamLe/Documents/Input.txt", "rt", stdin);
#endif

    int n, m;
    cin>>n>>m;

    //    if ((n == 1 && m != 1) || (n != 1 && m == 1)) {
    //        cout<<"-1";
    //        return 0;
    //    }

    int res = 0, res1 = 0, res2 = 0;

    int height = 0, x1 = N, x2 = 0, y1 = N, y2 = 0;

    for (int i = 0; i < n; ++i) {

        for (int j = 0; j < m; ++j) {
            cin>>A[i][j];

            if (A[i][j] == 'B') {

                if (j < x1) {
                    x1 = j;
                }

                if (j > x2) {
                    x2 = j;
                }

                if (i < y1) {
                    y1 = i;
                }

                if (i > y2) {
                    y2 = i;
                }

            }
        }
    }

    int x3 = -1, y3 = -1;
    bool flag1 = false, flag2 = false;

    if (x1 == N) {
        cout<<"1";
    }
    else {
        height = x2 - x1 + 1;

        if (y2 - y1 + 1 > height) {
            height = y2 - y1 + 1;

            if (x2 - height + 1 >= 0) {
                x1 = x2 - height + 1;
                flag1 = true;
            }

            if (x2 + height - 1 < m) {
                x3 = x2 + height - 1;
                flag2 = true;
            }
        }
        else {

            if (y2 - height + 1 >= 0) {
                y1 = y2 - height + 1;
                flag1 = true;
            }

            if (y2 + height - 1 < n) {
                y3 = y2 + height - 1;
                flag2 = true;
            }
        }

        if (flag1 == true) {
            flag1 = false;
            for (int i = y1; i <= y2; ++i) {
                for (int j = x1; j <= x2; ++j) {
                    if (A[i][j] == 'W') {
                        flag1 = true;
                        res1++;
                    }
                }
            }
        }

        if (flag2 == true) {
            flag2 = false;
            for (int i = y1; i <= y2; ++i) {

                for (int j = x2; j <= x3; ++j) {
                    if (A[i][j] == 'W') {
                        flag2 = true;
                        res2++;
                    }
                }
            }
        }

        if (flag1 == true && flag2 == true) {
            if (res1 < res2) {
                res = res1;
            }
            else{
                res = res2;
            }
        }
        else if (flag1 == true) {
            res = res1;
        }
        else if (flag2 == true) {
            res = res2;
        }
        else {
            res = -1;
        }

        cout<<res;
        return 0;
    }
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <math.h>
#include <unordered_map>
#include <string>
#include <algorithm>
#include <string.h>
#include <unordered_set>
#include <set>

using namespace std;

const int N = 105;
char A[N][N];

int main() {
#ifndef ONLINE_JUDGE
    freopen("/Users/TamLe/Documents/Input.txt", "rt", stdin);
#endif

    int n, m;
    cin>>n>>m;

    int res = 0;

    int height = 0, x1 = N, x2 = 0, y1 = N, y2 = 0;

    for (int i = 0; i < n; ++i) {

        for (int j = 0; j < m; ++j) {
            cin>>A[i][j];

            if (A[i][j] == 'B') {

                if (j < x1) {
                    x1 = j;
                }

                if (j > x2) {
                    x2 = j;
                }

                if (i < y1) {
                    y1 = i;
                }

                if (i > y2) {
                    y2 = i;
                }

            }
        }
    }

    bool flag = false;

    if (x1 == N) {
        cout<<"1";
    }
    else {
        height = x2 - x1 + 1;

        if (y2 - y1 + 1 > height) {
            height = y2 - y1 + 1;

            if (x2 - height + 1 >= 0) {
                x1 = x2 - height + 1;
                flag = true;
            }
            else if (x1 + height - 1 < m) {
                x2 = x1 + height - 1;
                flag = true;
            }
            else if (height <= m) {
                flag = true;
                x1 = 0;
                x2 = m - 1;
            }
        }
        else {

            if (y2 - height + 1 >= 0) {
                y1 = y2 - height + 1;
                flag = true;
            }
            else if (y1 + height - 1 < n) {
                y2 = y1 + height - 1;
                flag = true;
            }
            else if (height <= n) {
                flag = true;
                y1 = 0;
                y2 = n - 1;
            }
        }

        if (flag == true) {
            for (int i = y1; i <= y2; ++i) {
                for (int j = x1; j <= x2; ++j) {
                    if (A[i][j] == 'W') {
                        res++;
                    }
                }
            }
        }

        if (flag == false) {
            res = -1;
        }

        cout<<res;
        return 0;
    }
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <math.h>
#include <unordered_map>
#include <string>
#include <algorithm>
#include <string.h>
#include <unordered_set>
#include <set>

using namespace std;

const int N = 105;
char A[N][N];

int main() {
#ifndef ONLINE_JUDGE
    freopen("/Users/TamLe/Documents/Input.txt", "rt", stdin);
#endif

    int n, m;
    cin>>n>>m;

    int res = 0, res1 = 0, res2 = 0;

    int height = 0, x1 = N, x2 = 0, y1 = N, y2 = 0;

    for (int i = 0; i < n; ++i) {

        for (int j = 0; j < m; ++j) {
            cin>>A[i][j];

            if (A[i][j] == 'B') {

                if (j < x1) {
                    x1 = j;
                }

                if (j > x2) {
                    x2 = j;
                }

                if (i < y1) {
                    y1 = i;
                }

                if (i > y2) {
                    y2 = i;
                }

            }
        }
    }

    int x3 = -1, y3 = -1;
    bool flag1 = false, flag2 = false;

    if (x1 == N) {
        cout<<"1";
    }
    else {
        height = x2 - x1 + 1;

        if (y2 - y1 + 1 > height) {
            height = y2 - y1 + 1;

            if (x2 - height + 1 >= 0) {
                x1 = x2 - height + 1;
                flag1 = true;
            }

            if (x2 + height - 1 < m) {
                x3 = x2 + height - 1;
                flag2 = true;
            }
        }
        else {

            if (y2 - height + 1 >= 0) {
                y1 = y2 - height + 1;
                flag1 = true;
            }

            if (y2 + height - 1 < n) {
                y3 = y2 + height - 1;
                flag2 = true;
            }
        }

        if (flag1 == true) {
            for (int i = y1; i <= y2; ++i) {
                for (int j = x1; j <= x2; ++j) {
                    if (A[i][j] == 'W') {
                        res1++;
                    }
                }
            }
        }

        if (flag2 == true) {
            for (int i = y1; i <= y2; ++i) {

                for (int j = x2; j <= x3; ++j) {
                    if (A[i][j] == 'W') {
                        res2++;
                    }
                }
            }
        }

        if (flag1 == true && flag2 == true) {
            if (res1 > res2) {
                res = res1;
            }
            else{
                res = res2;
            }
        }
        else if (flag1 == true) {
            res = res1;
        }
        else if (flag2 == true) {
            res = res2;
        }
        else {
            res = -1;
        }

        cout<<res;
        return 0;
    }
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <math.h>
#include <unordered_map>
#include <string>
#include <algorithm>
#include <string.h>
#include <unordered_set>
#include <set>

using namespace std;

const int N = 105;
char A[N][N];

int main() {
#ifndef ONLINE_JUDGE
    freopen("/Users/TamLe/Documents/Input.txt", "rt", stdin);
#endif

    int n, m;
    cin>>n>>m;

    if (n == 1 || m == 1) {
        cout<<"-1";
        return 0;
    }

    int res = 0, res1 = 0, res2 = 0;

    int height = 0, x1 = N, x2 = 0, y1 = N, y2 = 0;

    for (int i = 0; i < n; ++i) {

        for (int j = 0; j < m; ++j) {
            cin>>A[i][j];

            if (A[i][j] == 'B') {

                if (j < x1) {
                    x1 = j;
                }

                if (j > x2) {
                    x2 = j;
                }

                if (i < y1) {
                    y1 = i;
                }

                if (i > y2) {
                    y2 = i;
                }

            }
        }
    }

    int x3 = -1, y3 = -1;
    bool flag1 = false, flag2 = false;

    if (x1 == N) {
        cout<<"1";
    }
    else {
        height = x2 - x1 + 1;

        if (y2 - y1 + 1 > height) {
            height = y2 - y1 + 1;

            if (x2 - height + 1 >= 0) {
                x1 = x2 - height + 1;
                flag1 = true;
            }

            if (x2 + height - 1 < m) {
                x3 = x2 + height - 1;
                flag2 = true;
            }
        }
        else {

            if (y2 - height + 1 >= 0) {
                y1 = y2 - height + 1;
                flag1 = true;
            }

            if (y2 + height - 1 < n) {
                y3 = y2 + height - 1;
                flag2 = true;
            }
        }

        if (flag1 == true) {
            for (int i = y1; i <= y2; ++i) {
                for (int j = x1; j <= x2; ++j) {
                    if (A[i][j] == 'W') {
                        res1++;
                    }
                }
            }

        }

        if (flag2 == true) {
            for (int i = y1; i <= y2; ++i) {

                for (int j = x2; j <= x3; ++j) {
                    if (A[i][j] == 'W') {
                        res2++;
                    }
                }
            }
        }

        if (flag1 == true && flag2 == true) {
            if (res1 < res2) {
                res = res1;
            }
            else{
                res = res2;
            }
        }
        else if (flag1 == true) {
            res = res1;
        }
        else if (flag2 == true) {
            res = res2;
        }
        else {
            res = -1;
        }

        cout<<res;
        return 0;
    }
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <math.h>
#include <unordered_map>
#include <string>
#include <algorithm>
#include <string.h>
#include <unordered_set>
#include <set>

using namespace std;

const int N = 105;
char A[N][N];

int main() {
#ifndef ONLINE_JUDGE
    freopen("/Users/TamLe/Documents/Input.txt", "rt", stdin);
#endif

    int n, m;
    cin>>n>>m;

    int res = 0;

    int height = 0, x1 = N, x2 = 0, y1 = N, y2 = 0;

    for (int i = 0; i < n; ++i) {

        for (int j = 0; j < m; ++j) {
            cin>>A[i][j];

            if (A[i][j] == 'B') {

                if (j < x1) {
                    x1 = j;
                }

                if (j > x2) {
                    x2 = j;
                }

                if (i < y1) {
                    y1 = i;
                }

                if (i > y2) {
                    y2 = i;
                }

            }
        }
    }

    bool flag = false;

    if (x1 == N) {
        cout<<"1";
    }
    else {
        height = x2 - x1 + 1;

        if (y2 - y1 + 1 > height) {
            height = y2 - y1 + 1;

            if (x2 - height + 1 >= 0) {
                x1 = x2 - height + 1;
                flag = true;
            }
            else if (x1 + height - 1 < m) {
                x2 = x1 + height - 1;
                flag = true;
            }
        }
        else {

            if (y2 - height + 1 >= 0) {
                y1 = y2 - height + 1;
                flag = true;
            }
            else if (y1 + height - 1 < n) {
                y2 = y1 + height - 1;
                flag = true;
            }
        }

        if (flag == true) {
            for (int i = y1; i <= y2; ++i) {
                for (int j = x1; j <= x2; ++j) {
                    if (A[i][j] == 'W') {
                        res++;
                    }
                }
            }
        }

        if (flag == false) {
            res = -1;
        }

        cout<<res;
        return 0;
    }
}


#include<iostream>
#include<vector>
#include<cmath>
#include<queue>
#include<map>
#include<algorithm>
#define ll long long
#define rep(i,a,b) for(ll i = a;i < b;i++)
#define N 200004
#define SN 2004
#define INF 1e9+7

using namespace std;

ll n,q,l,r,siz,ans;
ll freq[N],ar[N];
vector < vector < pair < pair< ll,ll >, ll > > > queries(SN);
ll res[N];

bool cmp(const pair < pair < ll,ll >,ll > &a,const pair < pair < ll,ll >,ll > &b)
{
	if(a.second == b.second)
	{
		return a.first.first < b.first.first;
	}

	return a.first.second > b.first.second;
}

void varto(ll x)
{
	ans -= (freq[ar[x]]*freq[ar[x]]*ar[x]);

	freq[ar[x]]++;

	ans += (freq[ar[x]]*freq[ar[x]]*ar[x]);

	return;
}

void fkarto(ll x)
{
	ans -= (freq[ar[x]]*freq[ar[x]]*ar[x]);

	freq[ar[x]]--;

	ans += (freq[ar[x]]*freq[ar[x]]*ar[x]);

	return;
}

int main()
{
    ios_base::sync_with_stdio(false);

    cin >> n >> q;

    siz = sqrt(n);

    rep(i,0,n)
    {
        cin >> ar[i];
    }

    rep(i,0,q)
    {
    	cin >> l >> r;
    	l--;
    	r--;
    	queries[l/siz].push_back(make_pair(make_pair(l,r),i));
    }

    rep(i,0,queries.size())
    {
    	sort(queries[i].begin(),queries[i].end(),cmp);
    }

    l = 0;
    r = 0;
    varto(0);

    rep(i,0,queries.size())
    {
    	rep(j,0,queries[i].size())
    	{
    		while(r < queries[i][j].first.second)
    		{
    			r++;
    			varto(r);
    		}

    		while(l > queries[i][j].first.first)
    		{
    			l--;
    			varto(l);
    		}

    		while(r > queries[i][j].first.second)
    		{
    			fkarto(r);
    			r--;
    		}

    		while(l < queries[i][j].first.first)
    		{
    			fkarto(l);
    			l++;
    		}

    		res[queries[i][j].second] = ans;
    	}
    }

    rep(i,0,q)
    {
    	cout << res[i] << endl;
    }

    return 0;
}


#include<iostream>
#include<stdio.h>
#include<vector>
#include<cmath>
#include<queue>
#include<map>
#include<algorithm>
#define ll long long
#define rep(i,a,b) for(ll i = a;i < b;i++)
#define N 200001
#define SN 2004
#define INF 1e9+7

using namespace std;

ll n,q,l,r,siz,ans;
ll freq[N],ar[N];
vector < pair < pair < ll,ll >, pair < ll,ll > > > queries (N);
ll res[N];

bool cmp(const pair < pair < ll,ll >, pair < ll, ll > > &a,const pair < pair < ll,ll >,pair < ll,ll > > &b)
{
	if(a.second.second == b.second.second)
	{
		if(a.first.second == b.first.second)
		{
			return a.first.first < b.first.first;
		}

		return a.first.second > b.first.second;
	}

	return a.second.second < b.second.second;

}

bool forp(const pair < pair < ll,ll >, pair < ll, ll > > &a,const pair < pair < ll,ll >,pair < ll,ll > > &b)
{
	return a.second.first < b.second.first;
}

void varto(ll x)
{
	ans -= (freq[ar[x]]*freq[ar[x]]*ar[x]);

	freq[ar[x]]++;

	ans += (freq[ar[x]]*freq[ar[x]]*ar[x]);

	return;
}

void fkarto(ll x)
{
	ans -= (freq[ar[x]]*freq[ar[x]]*ar[x]);

	freq[ar[x]]--;

	ans += (freq[ar[x]]*freq[ar[x]]*ar[x]);

	return;
}

int main()
{
	scanf("%I64d%I64d",&n,&q);

    siz = sqrt(n);

    rep(i,0,n)
    {
    	scanf("%I64d",&l);
    	ar[i] = l;
    }

    rep(i,0,q)
    {
    	scanf("%I64d%I64d",&l,&r);
    	l--;
    	r--;
    	queries.push_back(make_pair(make_pair(l,r),make_pair(i,l/siz)));
    }

    l = 0;
    r = 0;
    varto(0);

    sort(queries.begin(),queries.end(),cmp);

    rep(i,0,queries.size())
    {
    	while(r < queries[i].first.second)
    	{
    		r++;
    		varto(r);
    	}

    	while(l > queries[i].first.first)
    	{
    		l--;
    		varto(l);
    	}

    	while(r > queries[i].first.second)
  	{
    		fkarto(r);
   		r--;
    	}

   	while(l < queries[i].first.first)
    	{
    		fkarto(l);
    		l++;
    	}

    	res[queries[i].second.first] = ans;
    }

    sort(queries.begin(),queries.end(),forp);

    rep(i,0,q)
    {
    	printf("%I64d\n",res[i]);
    }

    return 0;
}


#include<iostream>
#include<stdio.h>
#include<vector>
#include<cmath>
#include<algorithm>
#define ll long long
#define rep(i,a,b) for(ll i = a;i < b;i++)
#define N 200010
#define INF 1e9+7

using namespace std;

ll n,q,l,r,siz,ans;
ll freq[1000021],ar[N];
vector < pair < pair < ll,ll >, pair < ll,ll > > > queries (N);
ll res[N];

bool cmp(const pair < pair < ll,ll >, pair < ll, ll > > &a,const pair < pair < ll,ll >,pair < ll,ll > > &b)
{
	if(a.second.second == b.second.second)
	{
		if(a.first.second == b.first.second)
		{
			return a.first.first < b.first.first;
		}

		return a.first.second < b.first.second;
	}

	return a.second.second < b.second.second;

}

void varto(ll x)
{
	ans -= (freq[ar[x]]*freq[ar[x]]*ar[x]);

	freq[ar[x]]++;

	ans += (freq[ar[x]]*freq[ar[x]]*ar[x]);

	return;
}

void fkarto(ll x)
{
	ans -= (freq[ar[x]]*freq[ar[x]]*ar[x]);

	freq[ar[x]]--;

	ans += (freq[ar[x]]*freq[ar[x]]*ar[x]);

	return;
}

int main()
{
	scanf("%I64d%I64d",&n,&q);

    siz = sqrt(n);

    rep(i,0,n)
    {
    	scanf("%I64d",&l);
    	ar[i] = l;
    }

    rep(i,0,q)
    {
    	scanf("%I64d%I64d",&l,&r);
    	l--;
    	r--;
    	queries.push_back(make_pair(make_pair(l,r),make_pair(i,l/siz)));
    }

    l = 0;
    r = 0;
    varto(0);

    sort(queries.begin(),queries.end(),cmp);

    rep(i,0,q)
    {
    	while(r < queries[i].first.second)
    	{
    		r++;
    		varto(r);
    	}

    	while(l > queries[i].first.first)
    	{
    		l--;
    		varto(l);
    	}

    	while(r > queries[i].first.second)
  	{
    		fkarto(r);
   		r--;
    	}

   	while(l < queries[i].first.first)
    	{
    		fkarto(l);
    		l++;
    	}

    	res[queries[i].second.first] = ans;
    }

    rep(i,0,q)
    {
    	printf("%I64d\n",res[i]);
    }

    return 0;
}


#include<iostream>
#include<stdio.h>
#include<vector>
#include<cmath>
#include<algorithm>
#define ll long long
#define rep(i,a,b) for(ll i = a;i < b;i++)
#define N 2000004
#define SN 2004
#define INF 1e9+7

using namespace std;

ll n,q,l,r,siz,ans;
ll freq[N],ar[N];
vector < vector < pair < pair< ll,ll >, ll > > > queries(SN);
ll res[N];

bool cmp(const pair < pair < ll,ll >,ll > &a,const pair < pair < ll,ll >,ll > &b)
{
	if(a.second == b.second)
	{
		return a.first.first < b.first.first;
	}

	return a.first.second > b.first.second;
}

void varto(ll x)
{
	ans -= (freq[ar[x]]*freq[ar[x]]*ar[x]);

	freq[ar[x]]++;

	ans += (freq[ar[x]]*freq[ar[x]]*ar[x]);

	return;
}

void fkarto(ll x)
{
	ans -= (freq[ar[x]]*freq[ar[x]]*ar[x]);

	freq[ar[x]]--;

	ans += (freq[ar[x]]*freq[ar[x]]*ar[x]);

	return;
}

int main()
{
	scanf("%I64d%I64d",&n,&q);

    siz = sqrt(n);

    rep(i,0,n)
    {
    	scanf("%I64d",&l);
    	ar[i] = l;
    }

    rep(i,0,q)
    {
    	scanf("%I64d%I64d",&l,&r);
    	l--;
    	r--;
    	queries[l/siz].push_back(make_pair(make_pair(l,r),i));
    }

    rep(i,0,queries.size())
    {
    	sort(queries[i].begin(),queries[i].end(),cmp);
    }

    l = 0;
    r = 0;
    varto(0);

    rep(i,0,queries.size())
    {
    	rep(j,0,queries[i].size())
    	{
    		while(r < queries[i][j].first.second)
    		{
    			r++;
    			varto(r);
    		}

    		while(l > queries[i][j].first.first)
    		{
    			l--;
    			varto(l);
    		}

    		while(r > queries[i][j].first.second)
    		{
    			fkarto(r);
    			r--;
    		}

    		while(l < queries[i][j].first.first)
    		{
    			fkarto(l);
    			l++;
    		}

    		res[queries[i][j].second] = ans;
    	}
    }

    rep(i,0,q)
    {
        printf("%I64d\n",res[i]);
    }

    return 0;
}


#include<iostream>
#include<stdio.h>
#include<vector>
#include<cmath>
#include<algorithm>
#define ll int
#define rep(i,a,b) for(ll i = a;i < b;i++)
#define N 200005
#define INF 1e9+7

using namespace std;

int n,q,l,r,siz,ans;
long long freq[1000004];
int ar[N];
vector < pair < pair < int,int >, pair < ll,ll > > > queries;
int res[N];

bool cmp(const pair < pair < ll,ll >, pair < ll, ll > > &a,const pair < pair < ll,ll >,pair < ll,ll > > &b)
{
	if(a.second.second == b.second.second)
	{
		return a.first.second < b.first.second;
	}

	return a.second.second < b.second.second;

}

void varto(ll x)
{
	ans -= (freq[ar[x]]*freq[ar[x]]*ar[x]);

	freq[ar[x]]++;

	ans += (freq[ar[x]]*freq[ar[x]]*ar[x]);

	return;
}

void fkarto(ll x)
{
	ans -= (freq[ar[x]]*freq[ar[x]]*ar[x]);

	freq[ar[x]]--;

	ans += (freq[ar[x]]*freq[ar[x]]*ar[x]);

	return;
}

int main()
{
	scanf("%d%d",&n,&q);

    siz = sqrt(n);

    rep(i,0,n)
    {
    	scanf("%d",&l);
    	ar[i] = l;
    }

    rep(i,0,q)
    {
    	scanf("%d%d",&l,&r);
    	l--;
    	r--;
    	queries.push_back(make_pair(make_pair(l,r),make_pair(i,l/siz)));
    }

    l = 0;
    r = 0;
    varto(0);

    sort(queries.begin(),queries.end(),cmp);

    rep(i,0,q)
    {
    	while(r < queries[i].first.second)
    	{
    		r++;
    		varto(r);
    	}

    	while(l > queries[i].first.first)
    	{
    		l--;
    		varto(l);
    	}

    	while(r > queries[i].first.second)
        {
    		fkarto(r);
            r--;
    	}

        while(l < queries[i].first.first)
    	{
    		fkarto(l);
    		l++;
    	}

    	res[queries[i].second.first] = ans;
    }

    rep(i,0,q)
    {
    	printf("%I64d\n",res[i]);
    }

    return 0;
}


#include <iostream>
#include <algorithm>
#include <utility>
#include <cstdio>
#include <cmath>
#include <vector>
int b_size;
long long freq[1000006],ans[200005],a[200005];
long long sum;

struct query
{
    int L,R,in;
}q[200005];

bool cmp(const struct query &a,const struct query &b)
{
 int x=(a.L)/b_size;
  int y=(b.L)/b_size;

  if(x==y)
  return ((a.R) < (b.R));
  else
  return (x < y);
}

int main()
{
    int n,t;
    scanf("%d%d",&n,&t);
    int i;

    for(i=0;i<n;i++)
    std::cin>>a[i];

    b_size=sqrt(n);

    int l,r;

    for(i=0;i<t;i++)
    {
      scanf("%d%d",&l,&r);
      l--;
      r--;
      q[i].L=l;
      q[i].R=r;
      q[i].in=i;
    }

    std::sort(q,q+t,cmp);

    int left=0,right=-1;
    i=0;
    sum=0;

    while(i<t)
    {
     l=q[i].L;
     r=q[i].R;

     while(right < r)
     {
      right++;
      sum+= a[right]*(2*freq[a[right]]+1);
      freq[a[right]]++;

     }

     while(right > r)
     {
      sum-=a[right]*(2*freq[a[right]]-1);
      freq[a[right]]--;
      right--;
     }

     while(left > l)
     {
      left--;
      sum+= a[left]*(2*freq[a[left]]+1);
      freq[a[left]]++;
     }

     while(left < l)
     {
      sum-=a[left]*(2*freq[a[left]]-1);
      freq[a[left]]--;
      left++;
     }

     ans[q[i].in]=sum;
     i++;
    }

    for(i=0;i<t;i++)
    printf("%lld\n",ans[i]);

  return 0;
}


#include <iostream>
#include <algorithm>
#include <utility>
#include <cstdio>
#include <cstring>
#include <vector>
int b_size;
long long freq[1000006],ans[200005];
std::vector< std::pair<int,std::pair<int,int> > > q(200005);
long long sum;

bool cmp(const std::pair<int,std::pair<int,int> > &a,const std::pair<int,std::pair<int,int> > &b)
{
  int x=(a.second.first)/b_size;
  int y=(b.second.first)/b_size;

  if(x==y)
  return ((a.second.second) < (b.second.second));

  return (x < y);
}

int main()
{
    std::ios_base::sync_with_stdio(0);
    std::cin.tie(0);
    std::cout.tie(0);
    int n,t;
    std::cin>>n>>t;

    int a[n];
    int i;

    for(i=0;i<n;i++)
    std::cin>>a[i];

    b_size=447;

    int l,r;

    for(i=0;i<t;i++)
    {
      std::cin>>l>>r;
      l--;
      r--;
      q[i]=std::make_pair(i,std::make_pair(l,r));
    }

    std::sort(q.begin(),q.begin()+t,cmp);

    int left=0,right=-1;
    i=0;
    sum=0;

    while(i<t)
    {
     l=q[i].second.first;
     r=q[i].second.second;

     while(right < r)
     {
      right++;
      sum+= a[right]*(2*freq[a[right]]+1);
      freq[a[right]]++;

     }

     while(right > r)
     {
      sum-=a[right]*(2*freq[a[right]]-1);
      freq[a[right]]--;
      right--;
     }

     while(left > l)
     {
      left--;
      sum+= a[left]*(2*freq[a[left]]+1);
      freq[a[left]]++;
     }

     while(left < l)
     {
      sum-=a[left]*(2*freq[a[left]]-1);
      freq[a[left]]--;
      left++;
     }

     ans[q[i].first]=sum;
     i++;
    }

    for(i=0;i<t;i++)
    std::cout<<ans[i]<<"\n";

  return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;
int b_size;
long long freq[1000006];
long long sum;

bool cmp(const pair<int,pair<int,int> > a,const pair<int,pair<int,int> > b)
{
  int x=(a.second.first)/b_size;
  int y=(b.second.first)/b_size;

  if(x==y)
  return ((a.second.second) < (b.second.second));

  return (x < y);
}

inline void inc(int index)
{
  sum-= index*(freq[index])*(freq[index]);
  freq[index]++;
  sum+= index*(freq[index])*(freq[index]);
}

inline void dec(int index)
{
  sum-= index*(freq[index])*(freq[index]);
  freq[index]--;
  sum+= index*(freq[index])*(freq[index]);
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n,t;
    cin>>n>>t;

    int a[n];
    int i;

    for(i=0;i<n;i++)
    cin>>a[i];

    b_size=sqrt(n);

    int l,r;
    vector< pair<int,pair<int,int> > > q(t);
    vector< long long > ans(t);
    for(i=0;i<t;i++)
    {
      cin>>l>>r;
      l--;
      r--;
      q[i]=make_pair(i,make_pair(l,r));
    }

    sort(q.begin(),q.end(),cmp);
    //memset(freq,0,sizeof(freq));

    int left=0,right=-1;
    i=0;
    sum=0;
    while(i<t)
    {
     l=q[i].second.first;
     r=q[i].second.second;

     while(right < r)
     {
      right++;
      inc(a[right]);
     }

     while(right > r)
     {
      dec(a[right]);
      right--;
     }

     while(left > l)
     {
      left--;
      inc(a[left]);
     }

     while(left < l)
     {
      dec(a[left]);
      left++;
     }

     ans[q[i].first]=sum;
     i++;
    }

    for(i=0;i<t;i++)
    cout<<ans[i]<<endl;

  return 0;
}


#include <iostream>
#include <algorithm>
#include <utility>
#include <cstdio>
using namespace std;
int b_size;
long long freq[1000006],ans[200005];
vector< pair<int,pair<int,int> > > q(200005);
long long sum;

bool cmp(const pair<int,pair<int,int> > &a,const pair<int,pair<int,int> > &b)
{
  int x=(a.second.first)/b_size;
  int y=(b.second.first)/b_size;

  if(x==y)
  return ((a.second.second) < (b.second.second));

  return (x < y);
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n,t;
    cin>>n>>t;

    int a[n];
    int i;

    for(i=0;i<n;i++)
    cin>>a[i];

    b_size=sqrt(n);

    int l,r;

    for(i=0;i<t;i++)
    {
      cin>>l>>r;
      l--;
      r--;
      q[i]=make_pair(i,make_pair(l,r));
    }

    sort(q.begin(),q.begin()+t,cmp);

    int left=0,right=-1;
    i=0;
    sum=0;

    while(i<t)
    {
     l=q[i].second.first;
     r=q[i].second.second;

     while(right < r)
     {
      right++;
      sum+= a[right]*(2*freq[a[right]]+1);
      freq[a[right]]++;

     }

     while(right > r)
     {
      sum-=a[right]*(2*freq[a[right]]-1);
      freq[a[right]]--;
      right--;
     }

     while(left > l)
     {
      left--;
      sum+= a[left]*(2*freq[a[left]]+1);
      freq[a[left]]++;
     }

     while(left < l)
     {
      sum-=a[left]*(2*freq[a[left]]-1);
      freq[a[left]]--;
      left++;
     }

     ans[q[i].first]=sum;
     i++;
    }

    for(i=0;i<t;i++)
    cout<<ans[i]<<endl;

  return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;
int b_size;
long long freq[1000006];
long long sum;

bool cmp(const pair<int,pair<int,int> > a,const pair<int,pair<int,int> > b)
{
  int x=(a.second.first)/b_size;
  int y=(b.second.first)/b_size;

  if(x==y)
  return ((a.second.second) < (b.second.second));

  return (x < y);
}

int main()
{

    int n,t;
    scanf("%d%d",&n,&t);

    int a[n];
    int i;

    for(i=0;i<n;i++)
    scanf("%d",&a[i]);

    b_size=sqrt(n);

    int l,r;
    vector< pair<int,pair<int,int> > > q(t);
    vector< long long > ans(t);
    for(i=0;i<t;i++)
    {
      scanf("%d%d",&l,&r);
      l--;
      r--;
      q[i]=make_pair(i,make_pair(l,r));
    }

    sort(q.begin(),q.end(),cmp);
    //memset(freq,0,sizeof(freq));

    int left=0,right=-1,index;
    i=0;
    sum=0;
    while(i<t)
    {
     l=q[i].second.first;
     r=q[i].second.second;

     while(right < r)
     {
      right++;
       index=a[right];
       sum-= index*(freq[index])*(freq[index]);
       freq[index]++;
       sum+= index*(freq[index])*(freq[index]);
     }

     while(right > r)
     {
       index=a[right];
       sum-= index*(freq[index])*(freq[index]);
       freq[index]--;
       sum+= index*(freq[index])*(freq[index]);
      right--;
     }

     while(left > l)
     {
       left--;
       index=a[left];
       sum-= index*(freq[index])*(freq[index]);
       freq[index]++;
       sum+= index*(freq[index])*(freq[index]);
     }

     while(left < l)
     {

       index=a[left];
       sum-= index*(freq[index])*(freq[index]);
       freq[index]--;
       sum+= index*(freq[index])*(freq[index]);
       left++;
     }

     ans[q[i].first]=sum;
     i++;
    }

    for(i=0;i<t;i++)
    printf("%lld\n",ans[i]);

  return 0;
}


#include <iostream>
#include <algorithm>
#include <utility>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cmath>
int b_size;
long long freq[1000006],ans[200005];
std::vector< std::pair<int,std::pair<int,int> > > q(200005);
long long sum;

bool cmp(const std::pair<int,std::pair<int,int> > &a,const std::pair<int,std::pair<int,int> > &b)
{
  int x=(a.second.first)/b_size;
  int y=(b.second.first)/b_size;

  if(x==y)
  return ((a.second.second) < (b.second.second));

  return (x < y);
}

int main()
{
    std::ios_base::sync_with_stdio(0);
    std::cin.tie(0);
    std::cout.tie(0);
    int n,t;
    std::cin>>n>>t;

    int a[n];
    int i;

    for(i=0;i<n;i++)
    std::cin>>a[i];

    b_size=sqrt(n);

    int l,r;

    for(i=0;i<t;i++)
    {
      std::cin>>l>>r;
      l--;
      r--;
      q[i]=std::make_pair(i,std::make_pair(l,r));
    }

    std::sort(q.begin(),q.begin()+t,cmp);

    int left=0,right=-1;
    i=0;
    sum=0;

    while(i<t)
    {
     l=q[i].second.first;
     r=q[i].second.second;

     while(right < r)
     {
      right++;
      sum+= a[right]*(2*freq[a[right]]+1);
      freq[a[right]]++;

     }

     while(right > r)
     {
      sum-=a[right]*(2*freq[a[right]]-1);
      freq[a[right]]--;
      right--;
     }

     while(left > l)
     {
      left--;
      sum+= a[left]*(2*freq[a[left]]+1);
      freq[a[left]]++;
     }

     while(left < l)
     {
      sum-=a[left]*(2*freq[a[left]]-1);
      freq[a[left]]--;
      left++;
     }

     ans[q[i].first]=sum;
     i++;
    }

    for(i=0;i<t;i++)
    std::cout<<ans[i]<<"\n";

  return 0;
}


#include <iostream>
#include <algorithm>
#include <utility>
#include <cstdio>
#include <cmath>
#include <vector>
int b_size;
int freq[1000006],a[200005];
long long sum,ans[200005];

struct query
{
    int L,R,in;
}q[200005];

bool cmp(const struct query &a,const struct query &b)
{
 int x=(a.L)/b_size;
  int y=(b.L)/b_size;

  if(x!=y)
  return (x < y);
  else
  return ((x&1) ? a.R < b.R :a.R > b.R);
}

int main()
{
    int n,t;
    scanf("%d%d",&n,&t);
    int i;

    for(i=0;i<n;i++)
    std::cin>>a[i];

    b_size=sqrt(n);

    int l,r;

    for(i=0;i<t;i++)
    {
      scanf("%d%d",&l,&r);
      l--;
      r--;
      q[i].L=l;
      q[i].R=r;
      q[i].in=i;
    }

    std::sort(q,q+t,cmp);

    int left=0,right=-1;
    i=0;
    sum=0;

    while(i<t)
    {
     l=q[i].L;
     r=q[i].R;

     while(right < r)
     {
      right++;
     sum+=(1LL*a[right])*(1LL*2*freq[a[right]]+1);
      freq[a[right]]++;

     }

     while(right > r)
     {
      sum-=(1LL*a[right])*(1LL*2*freq[a[right]]-1);
      freq[a[right]]--;
      right--;
     }

     while(left > l)
     {
      left--;
     sum+=(1LL*a[left])*(1LL*2*freq[a[left]]+1);
      freq[a[left]]++;
     }

     while(left < l)
     {
      sum-=(1LL*a[left])*(1LL*2*freq[a[left]]-1);
      freq[a[left]]--;
      left++;
     }

     ans[q[i].in]=sum;
     i++;
    }

    for(i=0;i<t;i++)
    printf("%lld\n",ans[i]);

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
int b_size;
long long freq[1000006];
long long sum;

bool cmp(const pair<int,pair<int,int> > a,const pair<int,pair<int,int> > b)
{
  int x=(a.second.first)/b_size;
  int y=(b.second.first)/b_size;

  if(x==y)
  return ((a.second.second) < (b.second.second));

  return (x < y);
}

void inc(int index)
{
  sum-= index*(freq[index])*(freq[index]);
  freq[index]++;
  sum+= index*(freq[index])*(freq[index]);
}

void dec(int index)
{
  sum-= index*(freq[index])*(freq[index]);
  freq[index]--;
  sum+= index*(freq[index])*(freq[index]);
}

int main()
{
    int n,t;
    cin>>n>>t;

    int a[n];
    int i;

    for(i=0;i<n;i++)
    cin>>a[i];

    b_size=sqrt(n);

    int l,r;
    vector< pair<int,pair<int,int> > > q(t);
    vector< long long > ans(t);
    for(i=0;i<t;i++)
    {
      cin>>l>>r;
      l--;
      r--;
      q[i]=make_pair(i,make_pair(l,r));
    }

    sort(q.begin(),q.end(),cmp);
    memset(freq,0,sizeof(freq));

    int left=0,right=-1;
    i=0;
    sum=0;
    while(i<t)
    {
     l=q[i].second.first;
     r=q[i].second.second;

     while(right < r)
     {
      right++;
      inc(a[right]);
     }

     while(right > r)
     {
      dec(a[right]);
      right--;
     }

     while(left > l)
     {
      left--;
      inc(a[left]);
     }

     while(left < l)
     {
      dec(a[left]);
      left++;
     }

     ans[q[i].first]=sum;
     i++;
    }

    for(i=0;i<t;i++)
    cout<<ans[i]<<endl;

  return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;
int b_size;
long long freq[1000006];
long long sum;

bool cmp(const pair<int,pair<int,int> > a,const pair<int,pair<int,int> > b)
{
  int x=(a.second.first)/b_size;
  int y=(b.second.first)/b_size;

  if(x==y)
  return ((a.second.second) < (b.second.second));

  return (x < y);
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n,t;
    cin>>n>>t;

    int a[n];
    int i;

    for(i=0;i<n;i++)
    cin>>a[i];

    b_size=sqrt(n);

    int l,r;
    vector< pair<int,pair<int,int> > > q(t);
    vector< long long > ans(t);
    for(i=0;i<t;i++)
    {
      cin>>l>>r;
      l--;
      r--;
      q[i]=make_pair(i,make_pair(l,r));
    }

    sort(q.begin(),q.end(),cmp);
    //memset(freq,0,sizeof(freq));

    int left=0,right=-1,index;
    i=0;
    sum=0;
    while(i<t)
    {
     l=q[i].second.first;
     r=q[i].second.second;

     while(right < r)
     {
      right++;
       index=a[right];
       sum-= index*(freq[index])*(freq[index]);
       freq[index]++;
       sum+= index*(freq[index])*(freq[index]);
     }

     while(right > r)
     {
       index=a[right];
       sum-= index*(freq[index])*(freq[index]);
       freq[index]--;
       sum+= index*(freq[index])*(freq[index]);
      right--;
     }

     while(left > l)
     {
       left--;
       index=a[left];
       sum-= index*(freq[index])*(freq[index]);
       freq[index]++;
       sum+= index*(freq[index])*(freq[index]);
     }

     while(left < l)
     {

       index=a[left];
       sum-= index*(freq[index])*(freq[index]);
       freq[index]--;
       sum+= index*(freq[index])*(freq[index]);
       left++;
     }

     ans[q[i].first]=sum;
     i++;
    }

    for(i=0;i<t;i++)
    cout<<ans[i]<<endl;

  return 0;
}


#include <iostream>
#include <algorithm>
#include <utility>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
int b_size;
long long freq[1000006],ans[200005],a[200005];
long long sum;

struct query{
    int L,R,in;
}q[200005];

bool cmp(const struct query &a,const struct query &b)
{
 int x=(a.L)/b_size;
  int y=(b.L)/b_size;

  if(x==y)
  return ((a.R) < (b.R));

  return (x < y);

}

int main()
{
    std::ios_base::sync_with_stdio(0);
    std::cin.tie(0);
    std::cout.tie(0);
    int n,t;
    std::cin>>n>>t;
    int i;

    for(i=0;i<n;i++)
    std::cin>>a[i];

    b_size=sqrt(n);

    int l,r;

    for(i=0;i<t;i++)
    {
      std::cin>>l>>r;
      l--;
      r--;
      q[i].L=l;
      q[i].R=r;
      q[i].in=i;
    }

    std::sort(q,q+t,cmp);

    int left=0,right=-1;
    i=0;
    sum=0;

    while(i<t)
    {
     l=q[i].L;
     r=q[i].R;

     while(right < r)
     {
      right++;
      sum+= a[right]*(2*freq[a[right]]+1);
      freq[a[right]]++;

     }

     while(right > r)
     {
      sum-=a[right]*(2*freq[a[right]]-1);
      freq[a[right]]--;
      right--;
     }

     while(left > l)
     {
      left--;
      sum+= a[left]*(2*freq[a[left]]+1);
      freq[a[left]]++;
     }

     while(left < l)
     {
      sum-=a[left]*(2*freq[a[left]]-1);
      freq[a[left]]--;
      left++;
     }

     ans[q[i].in]=sum;
     i++;
    }

    for(i=0;i<t;i++)
    std::cout<<ans[i]<<"\n";

  return 0;
}


#include <iostream>
#include <algorithm>
#include <utility>
#include <cstdio>
#include <cmath>
#include <vector>
int b_size;
int freq[1000006],a[200005];
long long sum,ans[200005];

struct query
{
    int L,R,in;
}q[200005];

bool cmp(const struct query &a,const struct query &b)
{
 int x=(a.L)/b_size;
  int y=(b.L)/b_size;

  if(x==y)
  return ((a.R) < (b.R));
  else
  return (x < y);
}

int main()
{
    int n,t;
    scanf("%d%d",&n,&t);
    int i;

    for(i=0;i<n;i++)
    std::cin>>a[i];

    b_size=sqrt(n);

    int l,r;

    for(i=0;i<t;i++)
    {
      scanf("%d%d",&l,&r);
      l--;
      r--;
      q[i].L=l;
      q[i].R=r;
      q[i].in=i;
    }

    std::sort(q,q+t,cmp);

    int left=0,right=-1;
    i=0;
    sum=0;

    while(i<t)
    {
     l=q[i].L;
     r=q[i].R;

     while(right < r)
     {
      right++;
     sum+=(1LL*a[right])*(1LL*2*freq[a[right]]+1);
      freq[a[right]]++;

     }

     while(right > r)
     {
      sum-=(1LL*a[right])*(1LL*2*freq[a[right]]-1);
      freq[a[right]]--;
      right--;
     }

     while(left > l)
     {
      left--;
     sum+=(1LL*a[left])*(1LL*2*freq[a[left]]+1);
      freq[a[left]]++;
     }

     while(left < l)
     {
      sum-=(1LL*a[left])*(1LL*2*freq[a[left]]-1);
      freq[a[left]]--;
      left++;
     }

     ans[q[i].in]=sum;
     i++;
    }

    for(i=0;i<t;i++)
    printf("%lld\n",ans[i]);

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
int b_size;
long long freq[1000006];
long long sum;

bool cmp(const pair<int,pair<int,int> > a,const pair<int,pair<int,int> > b)
{
  int x=(a.second.first)/b_size;
  int y=(b.second.first)/b_size;

  if(x==y)
  return ((a.second.second) < (b.second.second));

  return (x < y);
}

void inc(int index)
{
  sum-= index*(freq[index])*(freq[index]);
  freq[index]++;
  sum+= index*(freq[index])*(freq[index]);
}

void dec(int index)
{
  sum-= index*(freq[index])*(freq[index]);
  freq[index]--;
  sum+= index*(freq[index])*(freq[index]);
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n,t;
    cin>>n>>t;

    int a[n];
    int i;

    for(i=0;i<n;i++)
    cin>>a[i];

    b_size=sqrt(n);

    int l,r;
    vector< pair<int,pair<int,int> > > q(t);
    vector< long long > ans(t);
    for(i=0;i<t;i++)
    {
      cin>>l>>r;
      l--;
      r--;
      q[i]=make_pair(i,make_pair(l,r));
    }

    sort(q.begin(),q.end(),cmp);
    memset(freq,0,sizeof(freq));

    int left=0,right=-1;
    i=0;
    sum=0;
    while(i<t)
    {
     l=q[i].second.first;
     r=q[i].second.second;

     while(right < r)
     {
      right++;
      inc(a[right]);
     }

     while(right > r)
     {
      dec(a[right]);
      right--;
     }

     while(left > l)
     {
      left--;
      inc(a[left]);
     }

     while(left < l)
     {
      dec(a[left]);
      left++;
     }

     ans[q[i].first]=sum;
     i++;
    }

    for(i=0;i<t;i++)
    cout<<ans[i]<<endl;

  return 0;
}


#include <iostream>
#include <algorithm>
#include <utility>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cmath>
using namespace std;
int b_size;
long long freq[1000006],ans[200005];
vector< pair<int,pair<int,int> > > q(200005);
long long sum;

bool cmp(const pair<int,pair<int,int> > &a,const pair<int,pair<int,int> > &b)
{
  int x=(a.second.first)/b_size;
  int y=(b.second.first)/b_size;

  if(x==y)
  return ((a.second.second) < (b.second.second));

  return (x < y);
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n,t;
    cin>>n>>t;

    int a[n];
    int i;

    for(i=0;i<n;i++)
    cin>>a[i];

    b_size=sqrt(n);

    int l,r;

    for(i=0;i<t;i++)
    {
      cin>>l>>r;
      l--;
      r--;
      q[i]=make_pair(i,make_pair(l,r));
    }

    sort(q.begin(),q.begin()+t,cmp);

    int left=0,right=-1;
    i=0;
    sum=0;

    while(i<t)
    {
     l=q[i].second.first;
     r=q[i].second.second;

     while(right < r)
     {
      right++;
      sum+= a[right]*(2*freq[a[right]]+1);
      freq[a[right]]++;

     }

     while(right > r)
     {
      sum-=a[right]*(2*freq[a[right]]-1);
      freq[a[right]]--;
      right--;
     }

     while(left > l)
     {
      left--;
      sum+= a[left]*(2*freq[a[left]]+1);
      freq[a[left]]++;
     }

     while(left < l)
     {
      sum-=a[left]*(2*freq[a[left]]-1);
      freq[a[left]]--;
      left++;
     }

     ans[q[i].first]=sum;
     i++;
    }

    for(i=0;i<t;i++)
    cout<<ans[i]<<endl;

  return 0;
}


#include <iostream>
#include <algorithm>
#include <utility>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
int b_size;
long long freq[1000006],ans[200005],a[200005];
std::vector< std::pair<int,std::pair<int,int> > > q(200005);
long long sum;

bool cmp(const std::pair<int,std::pair<int,int> > &a,const std::pair<int,std::pair<int,int> > &b)
{
  int x=(a.second.first)/b_size;
  int y=(b.second.first)/b_size;

  if(x==y)
  return ((a.second.second) < (b.second.second));

  return (x < y);
}

int main()
{
    std::ios_base::sync_with_stdio(0);
    std::cin.tie(0);
    std::cout.tie(0);
    int n,t;
    std::cin>>n>>t;
    int i;

    for(i=0;i<n;i++)
    std::cin>>a[i];

    b_size=sqrt(n);

    int l,r;

    for(i=0;i<t;i++)
    {
      std::cin>>l>>r;
      l--;
      r--;
      q[i]=std::make_pair(i,std::make_pair(l,r));
    }

    std::sort(q.begin(),q.begin()+t,cmp);

    int left=0,right=-1;
    i=0;
    sum=0;

    while(i<t)
    {
     l=q[i].second.first;
     r=q[i].second.second;

     while(right < r)
     {
      right++;
      sum+= a[right]*(2*freq[a[right]]+1);
      freq[a[right]]++;

     }

     while(right > r)
     {
      sum-=a[right]*(2*freq[a[right]]-1);
      freq[a[right]]--;
      right--;
     }

     while(left > l)
     {
      left--;
      sum+= a[left]*(2*freq[a[left]]+1);
      freq[a[left]]++;
     }

     while(left < l)
     {
      sum-=a[left]*(2*freq[a[left]]-1);
      freq[a[left]]--;
      left++;
     }

     ans[q[i].first]=sum;
     i++;
    }

    for(i=0;i<t;i++)
    std::cout<<ans[i]<<"\n";

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
int b_size;
long long freq[1000006];
long long sum;

bool cmp(const pair<int,pair<int,int> > a,const pair<int,pair<int,int> > b)
{
  int x=(a.second.first)/b_size;
  int y=(b.second.first)/b_size;

  if(x==y)
  return ((a.second.second) < (b.second.second));

  return (x < y);
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n,t;
    cin>>n>>t;

    int a[n];
    int i;

    for(i=0;i<n;i++)
    cin>>a[i];

    b_size=sqrt(n);

    int l,r;
    vector< pair<int,pair<int,int> > > q(t);
    vector< long long > ans(t);
    for(i=0;i<t;i++)
    {
      cin>>l>>r;
      l--;
      r--;
      q[i]=make_pair(i,make_pair(l,r));
    }

    sort(q.begin(),q.end(),cmp);
    memset(freq,0,sizeof(freq));

    int left=0,right=-1;
    i=0;
    sum=0;
    while(i<t)
    {
     l=q[i].second.first;
     r=q[i].second.second;

     while(right < r)
     {
      right++;
      sum+= a[right]*(2*freq[a[right]]+1);
      freq[a[right]]++;

     }

     while(right > r)
     {
      sum-=a[right]*(2*freq[a[right]]-1);
      freq[a[right]]--;
      right--;
     }

     while(left > l)
     {
      left--;
      sum+= a[left]*(2*freq[a[left]]+1);
      freq[a[left]]++;
     }

     while(left < l)
     {
      sum-=a[left]*(2*freq[a[left]]-1);
      freq[a[left]]--;
      left++;
     }

     ans[q[i].first]=sum;
     i++;
    }

    for(i=0;i<t;i++)
    cout<<ans[i]<<endl;

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
int b_size;
long long freq[1000006];
long long sum;

bool cmp(const pair<int,pair<int,int> > a,const pair<int,pair<int,int> > b)
{
  int x=(a.second.first)/b_size;
  int y=(b.second.first)/b_size;

  if(x==y)
  return ((a.second.second) < (b.second.second));

  return (x < y);
}

inline void inc(int index)
{
  sum-= index*(freq[index])*(freq[index]);
  freq[index]++;
  sum+= index*(freq[index])*(freq[index]);
}

inline void dec(int index)
{
  sum-= index*(freq[index])*(freq[index]);
  freq[index]--;
  sum+= index*(freq[index])*(freq[index]);
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n,t;
    cin>>n>>t;

    int a[n];
    int i;

    for(i=0;i<n;i++)
    cin>>a[i];

    b_size=sqrt(n);

    int l,r;
    vector< pair<int,pair<int,int> > > q(t);
    vector< long long > ans(t);
    for(i=0;i<t;i++)
    {
      cin>>l>>r;
      l--;
      r--;
      q[i]=make_pair(i,make_pair(l,r));
    }

    sort(q.begin(),q.end(),cmp);
    memset(freq,0,sizeof(freq));

    int left=0,right=-1;
    i=0;
    sum=0;
    while(i<t)
    {
     l=q[i].second.first;
     r=q[i].second.second;

     while(right < r)
     {
      right++;
      inc(a[right]);
     }

     while(right > r)
     {
      dec(a[right]);
      right--;
     }

     while(left > l)
     {
      left--;
      inc(a[left]);
     }

     while(left < l)
     {
      dec(a[left]);
      left++;
     }

     ans[q[i].first]=sum;
     i++;
    }

    for(i=0;i<t;i++)
    cout<<ans[i]<<endl;

  return 0;
}


#include <bits/stdc++.h>
#define MAX  200005
#define MAXS 325
#define MAXQ 200005
#define MAXA 1000005
using namespace std;
int a[MAX], n, q, s;
int qi[MAXQ], qj[MAXQ], p[MAXQ];
long long int ans[MAXQ];
int occur[MAXA] = {};
vector<int>group[MAXS];
bool comp(int a, int b);
bool comp2(int a, int b);
int main()
{
	scanf("%d %d", &n, &q);
	for(int i = 0;i < n;i++)
		scanf("%d", &a[i]);
	s = sqrt(n);
	if(s*s < n)
		s++;
	for(int i = 0;i < q;i++)
	{
		scanf("%d %d", &qi[i], &qj[i]);
		qi[i]--; qj[i]--;
		p[i] = i;
	}
	sort(p, p+q, comp);
	for(int i = 0;i < q;i++)
	{
		int gp = qi[p[i]]/s;
		group[gp].push_back(p[i]);
	}
	for(int i = 0;i < s;i++)
		sort(group[i].begin(), group[i].end(), comp2);
	int l = 0, r = 0;
	long long int cont = 0;
	bool flag = false;
	for(int i = 0;i < s;i++)
	{
		for(int j = 0;j < group[i].size();j++)
		{
			int ql = qi[group[i][j]], qr = qj[group[i][j]];
			if(!flag)
			{
				for(int k = ql;k <= qr;k++)
				{
					cont -= (long long int)occur[a[k]]*occur[a[k]]*a[k];
					occur[a[k]]++;
					cont += (long long int)occur[a[k]]*occur[a[k]]*a[k];
				}
				l = ql; r = qr;
				flag = true;
			}
			else
			{
				if(l < ql)
				{
					for(int k = l;k < ql;k++)
					{
						cont -= (long long int)occur[a[k]]*occur[a[k]]*a[k];
						occur[a[k]]--;
						cont += (long long int)occur[a[k]]*occur[a[k]]*a[k];
					}
				}
				else if(l > ql)
				{
					for(int k = l-1;k >= ql;k--)
					{
						cont -= (long long int)occur[a[k]]*occur[a[k]]*a[k];
						occur[a[k]]++;
						cont += (long long int)occur[a[k]]*occur[a[k]]*a[k];
					}
				}
				l = ql;
				if(r < qr)
				{
					for(int k = r+1;k <= qr;k++)
					{
						cont -= (long long int)occur[a[k]]*occur[a[k]]*a[k];
						occur[a[k]]++;
						cont += (long long int)occur[a[k]]*occur[a[k]]*a[k];
					}
				}
				else if(r > qr)
				{
					for(int k = r;k > qr;k--)
					{
						cont -= (long long int)occur[a[k]]*occur[a[k]]*a[k];
						occur[a[k]]--;
						cont += (long long int)occur[a[k]]*occur[a[k]]*a[k];
					}
				}
				r = qr;
			}
			ans[group[i][j]] = cont;
		}
	}
	for(int i = 0;i < q;i++)
		printf("%I64d\n", ans[i]);
	return 0;
}
bool comp(int a, int b)
{
	return qi[a] < qi[b];
}
bool comp2(int a, int b)
{
	return qj[a] < qj[b];
}
// 1478909713100


#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#define ll long long
using namespace std;

int n,q,len;
ll respostaAtual = 0;
struct que{
  int l, r;
  int index;
};

que querys[200005];
int array[200005];
ll ans[200005];
int freq[1000005];

bool comparator(que &x, que &y) {
  if(x.l / len != y.l / len) return x.l < y.l;
  return x.r < y.r;
}

inline void add(int p){
    respostaAtual -= ((ll)p * freq[p] * freq[p] );
    freq[p]++;
    respostaAtual += ((ll)p * freq[p] * freq[p] );
}

inline void remove(int p){
    respostaAtual -= ((ll)p * freq[p] * freq[p] );
    freq[p]--;
    respostaAtual += ((ll)p * freq[p] * freq[p] );
}

int main(){
  int n,q;
  scanf ("%d%d", &n,&q);
  for (int i = 0; i < n; i++)
    scanf ("%d",&  array[i]);
  //scanf ("%d", &q);
  len = sqrt(n) + 1;
  for (int i = 0; i < q; i++){
    scanf ("%d%d", &querys[i].l, &querys[i].r);
    querys[i].l--;
    querys[i].r--;
    querys[i].index = i;
  }
  sort (querys, querys + q, comparator);

  int atualR = 0, atualL = 0;

  for (int i = 0; i < q; i++){
    int L = querys[i].l, R = querys[i].r;
    while (atualL < L){
      remove(array[atualL]);
      atualL++;
    }
    while (atualL > L){
      add(array[atualL-1]);
      atualL--;
    }
    while (atualR <= R){
      add(array[atualR]);
      atualR++;
    }
    while(atualR > R+1){
      remove(array[atualR-1]);
      atualR--;
    }
    ans[querys[i].index] = respostaAtual;
  }

  for (int i = 0; i < q; i++){
    printf ("%lld\n", ans[i]);
  }

  return 0;
}
// 1479063816070


#include <bits/stdc++.h>
#define fr(a, b, c) for(int a = b; a < c; ++a)
#define pb push_back

using namespace std;

typedef long long int  ll;
typedef pair<ll, ll> ii;
typedef pair<ii, int> iii;

struct Q{
    ll l, r, ix;
} q[200005];

ll n, m, que;
ll arr[1000005];
ll ansi[200005];
vector<ll> vec;
ll k;
vector<ll>::iterator it;

bool cmp(Q a, Q b) {
    if (a.l/que != b.l/que) return a.l < b.l;
    else return a.r < b.r;
}

void add(ll x) {
    arr[x]++;
    k += (ll)((x<<1)*arr[x] - x);
}

void rem(ll x) {
    arr[x]--;
    k += (ll)(-(x<<1)*arr[x] - x);
}

int main() {
    cin >> n >> m;
    ll a, b;
    fr(i, 0, n) {
        scanf("%lld", &a);
        vec.pb(a);
    }

    que = ceil(sqrt(n));

    fr(i, 0, m) {
        scanf("%lld%lld", &a, &b);
        --a; --b;
        q[i].l = a;
        q[i].r = b;
        q[i].ix = i;
    }

    sort(q, q+m, cmp);
    memset(arr, 0, sizeof arr);

    ll curL = 0, curR = 0, l, r;
    k = 0;

    fr(i, 0, m) {
        l = q[i].l;
        r = q[i].r;

        while (curL < l) {
            rem(vec[curL]);
            curL++;
        }
        while (curL > l) {
            add(vec[curL-1]);
            curL--;
        }
        while (curR <= r) {
            add(vec[curR]);
            curR++;
        }
        while (curR > r+1) {
            rem(vec[curR-1]);
            curR--;
        }

        ansi[q[i].ix] = k;
    }

    fr(i, 0, m) printf("%lld\n", ansi[i]);
    return 0;
}
// 1470363941522


#include <bits/stdc++.h>

using namespace std;

int S;

struct Query{
	int l, r, i;
	Query(){}
	Query(int l, int r, int i): l(l), r(r), i(i){}
} q[200010];

bool cmp(const Query &A, const Query &B)
{
  if ( (A.l / S) != (B.l / S) ) return (A.l / S) < (B.l / S);
  return A.r < B.r;
}

int currentL = 0;
int currentR = 0;
int conta[1000100], v[200010];
long long ans[200010];

long long answer = 0;

void add(int position){
	long long temp = conta[v[position]];

	answer -= temp*temp*v[position];
	conta[v[position]]++;
	temp = conta[v[position]];

	answer += v[position]*temp*temp;

}

void remove(int position){
		long long temp = conta[v[position]];
		answer -= temp*temp*v[position];
		conta[v[position]]--;
		temp = conta[v[position]];
		answer += temp*temp*v[position];
}

int main(){

	int n, Q;
	scanf("%d %d", &n, &Q);
	for(int i = 0; i < n; i++)scanf("%d", &v[i]);

	for(int i = 0; i < Q; i++){
		scanf("%d %d", &q[i].l, &q[i].r);
		q[i].l--;
		q[i].r--;
		q[i].i = i;
	}

	S = sqrt(n);
	sort(q, q + Q, cmp);

	add(0);
	for(int i = 0; i < Q; i++){
		int L = q[i].l, R = q[i].r;

		while (currentL < L){
			remove(currentL);
			currentL++;
		}

		while(currentL > L){
			currentL--;
			add(currentL);
		}
		while (currentR <= R){
			currentR++;
			add(currentR);
		}
		while(currentR > R){
			remove(currentR);
			currentR--;
		}
		ans[q[i].i] = answer;
	}

	for(int i = 0; i < Q; i++) printf("%lld\n", ans[i]);
	return 0;
}
// 1470779119696


#include <bits/stdc++.h>

using namespace std;

#define fr(a, b, c) for(int a = b, __ = c; a < __; a++)

#define sd1(a) scanf("%d", &a)
#define sd2(a, b) scanf("%d%d", &a, &b)

#define dbg(x) cerr << ">>> " << x << endl;
#define _ << ", " <<

typedef long long ll;

const int maxn = 30011;
const int maxq = 200111;
const int BSZ  = 1000;

ll sqr(ll x) { return x * x; }

struct Freq {
	ll arr[1000111];
	ll ans;

	Freq() {
		memset(arr, 0, sizeof arr);
		ans = 0;
	}

	void add(int x) {
		ans -= sqr(arr[x]) * x;
		arr[x]++;
		ans += sqr(arr[x]) * x;
	}
	void rem(int x) {
		ans -= sqr(arr[x]) * x;
		arr[x]--;
		ans += sqr(arr[x]) * x;
	}
} freq;

int n, q;
int v[maxn];
int l[maxq];
int r[maxq];
int mo[maxq];
int ans[maxq];

bool cmp(int a, int b) {
	int Ba = l[a]/BSZ;
	int Bb = l[b]/BSZ;

	if(Ba != Bb) return Ba < Bb;

	return Ba & 1 ? r[a] < r[b] : r[a] > r[b];
}

int main() {
	sd2(n, q);

	fr(i, 0, n) sd1(v[i]);

	fr(i, 0, q) {
		sd1(l[i]);
		sd1(r[i]);
		l[i]--;
		r[i]--;
		mo[i] = i;
	}

	sort(mo, mo + q, cmp);

	int L = 0, R = -1;
	fr(i, 0, q) {
		int l = ::l[mo[i]];
		int r = ::r[mo[i]];

		while(l > L) freq.rem(v[L++]);
		while(l < L) freq.add(v[--L]);

		while(r > R) freq.add(v[++R]);
		while(r < R) freq.rem(v[R--]);

		ans[mo[i]] = freq.ans;
	}

	fr(i, 0, q) {
		printf("%d\n", ans[i]);
	}

	return 0;
}

// 1470680217209


#include <cstdio>
#include <algorithm>
using namespace std;

#define N 200010
#define A 2000010
#define BLOCK 400
#define ll long long

int cnt[A];
ll a[N], ans[N], answer = 0;

struct node {
	int L, R, i;
} q[N];

bool cmp(node x, node y) {
    if(x.L/BLOCK != y.L/BLOCK)
        return x.L/BLOCK < y.L/BLOCK;

    return x.R < y.R;
}

void add(ll position) {
    answer -= ((long long int)cnt[position] * cnt[position] * position);
    cnt[position]++;
    answer += ((long long int)cnt[position] * cnt[position] * position);
}

void remove_(ll position) {
	answer -= ((long long int)cnt[position] * cnt[position] * position);
    cnt[position]--;
    answer += ((long long int)cnt[position] * cnt[position] * position);
}

int main() {
	int n, m;
 	scanf("%d%d", &n, &m);
	for(int i=0; i<n; i++)
 		scanf("%I64d", &a[i]);

 	for(int i = 0; i < m; i++) {
 		scanf("%d%d", &q[i].L, &q[i].R);
 		q[i].L--;
 		q[i].R--;
 		q[i].i = i;
	}

 	sort(q, q + m, cmp);

 	int currentL = 0, currentR = 0;
 	for(int i = 0; i < m; i++) {
 		int L = q[i].L, R = q[i].R;
 		while(currentL < L) {
 			remove_(a[currentL]);
 			currentL++;
 		}
 		while(currentL > L) {
			add(a[currentL-1]);
 			currentL--;
 		}
		while(currentR <= R) {
 			add(a[currentR]);
 			currentR++;
 		}
 		while(currentR > R+1) {
 			remove_(a[currentR-1]);
			currentR--;
 		}
 		ans[q[i].i] = answer;
 	}

 	for(int i = 0; i < m; i++)
 		printf("%I64d\n", ans[i]);
}
// 1503619623295


#include <bits/stdc++.h>
#define tam 200001
#define max 1000011
using namespace std;

int bloco;
int ocorre[max];
int ans[tam];

struct query{
    int l, r, i;
};

bool compare(query x, query y){
    if(x.l/bloco == y.l/bloco) return x.r < y.r;
    else return x.l / bloco < y.l / bloco;
}

void resultadoQuery(int arr[], int n, query q[], int m){
     bloco = sqrt(n);

     sort(q, q+m, compare);

     int curL = 0, curR = 0;
     long long soma = 0;

     for(int i=0;i<m;i++){
         int l = q[i].l;
         int r = q[i].r;
         //cout << "query " << soma << endl;
         while(curL < l){
             soma -= (ocorre[arr[curL]] * ocorre[arr[curL]] * arr[curL]);
             ocorre[arr[curL]]--;
             soma += (ocorre[arr[curL]] * ocorre[arr[curL]] * arr[curL]);
             curL++;
         }
         //cout << "query " << soma << endl;
         while(curL > l){
             soma -= (ocorre[arr[curL-1]] * ocorre[arr[curL-1]] * arr[curL-1]);
             ocorre[arr[curL-1]]++;
             soma += (ocorre[arr[curL-1]] * ocorre[arr[curL-1]] * arr[curL-1]);
             curL--;
         }
         //cout << "query " << soma << endl;
         while(curR <= r){
             soma-= (ocorre[arr[curR]] * ocorre[arr[curR]] * arr[curR]);
             ocorre[arr[curR]]++;
             soma += (ocorre[arr[curR]] * ocorre[arr[curR]] * arr[curR]);
             curR++;
         }
         //cout << "query " << soma << endl;
         while(curR > r+1){
             soma -= (ocorre[arr[curR-1]] * ocorre[arr[curR-1]] * arr[curR-1]);
             ocorre[arr[curR-1]]--;
             soma += (ocorre[arr[curR-1]] * ocorre[arr[curR-1]] * arr[curR-1]);
             curR--;
         }
         //cout << "query " << soma << endl;
         ans[q[i].i] = soma;
     }
}

int main(){
    int n;

    int m;
    cin >> n >> m;
    int arr[n];
    for(int i=0;i<n;i++){
        cin >> arr[i];
    }
    query q[m];
    for(int i=0;i<m;i++){
        cin >> q[i].l >> q[i].r;
        q[i].l--; q[i].r--;
        q[i].i = i;
    }
    resultadoQuery(arr, n, q, m);
    for(int i=0;i<m;i++){
        //cout << q[i].l << " " << q[i].r << endl;
        cout << ans[i] << endl;
    }
    return 0;
}
// 1508955391670


#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#define ll long long
using namespace std;

ll n,q,len,respostaAtual = 0;
struct que{
  int l, r;
  int index;
};

que querys[200010];
ll array[200010];
ll ans[200010];
ll freq[2000100];

bool comparator(que x, que y){
  if (x.l/len != y.l/len)
    return x.l/len < y.l/len;
  return x.r < y.r;
}

void add(ll p){
    respostaAtual -= ((long long int)freq[p] * freq[p] * p);
    freq[p]++;
    respostaAtual += ((long long int)freq[p] * freq[p] * p);
}

void remove(long long p){
 respostaAtual -= ((long long int)freq[p] * freq[p] * p);
    freq[p]--;
    respostaAtual += ((long long int)freq[p] * freq[p] * p);
}

int main(){
  int n,q;
  scanf ("%d %d", &n,&q);
  for (int i = 0; i < n; i++)
    scanf ("%lld",&  array[i]);
  //scanf ("%d", &q);
  len = 400;
  int left, righ;
  for (int i = 0; i < q; i++){
    scanf ("%d %d", &left, &righ);
    left--;
    righ--;
    querys[i].l = left;
    querys[i].r =  righ;
    querys[i].index = i;
  }
  sort (querys, querys + q, comparator);

  int atualR = 0, atualL = 0;

  for (int i = 0; i < q; i++){
    int L = querys[i].l, R = querys[i].r;
    while (atualL < L){
      remove(array[atualL]);
      atualL++;
    }
    while (atualL > L){
      add(array[atualL-1]);
      atualL--;
    }
    while (atualR <= R){
      add(array[atualR]);
      atualR++;
    }
    while(atualR > R+1){
      remove(array[atualR-1]);
      atualR--;
    }
    ans[querys[i].index] = respostaAtual;
  }

  for (int i = 0; i < q; ++i){
    printf ("%lld\n", ans[i]);
  }

  return 0;
}
// 1478936233198


#include <bits/stdc++.h>

using namespace std;

struct cell{
	int l, r, id;
};

int cnt[1000050], ans;
int sq, a[2000010], resp[2000010], n, q;
cell qry[2000010];

void add(int pos){
	ans -= cnt[a[pos]] * cnt[a[pos]] * a[pos];
	cnt[a[pos]]++;
	ans += cnt[a[pos]] * cnt[a[pos]] * a[pos];
}

void remove(int pos){
	ans -= cnt[a[pos]] * cnt[a[pos]] * a[pos];
	cnt[a[pos]]--;
	ans += cnt[a[pos]] * cnt[a[pos]] * a[pos];
}

void mo(){
	int l, r, cL = 0, cR = 0;

	add(0);

	for(int i = 0; i < q; i++){
		l = qry[i].l, r = qry[i].r;

		while(cL < l){
			remove(cL);
			cL++;
		}
		while(cL > l){
			cL--;
			add(cL);
		}
		while(cR < r){
			cR++;
			add(cR);
		}
		while(cR > r){
			remove(cR);
			cR--;
		}

		resp[qry[i].id] = ans;
	}
}

bool cmp(const cell &a, const cell &b){
	if((a.l/sq) != (b.l/sq))
		return (a.l/sq) < (b.l/sq);

	return a.r < b.r;
}

int main(){
	int x, y;

	scanf("%d%d", &n, &q);
	sq = sqrt(n);

	for(int i = 0; i < n; i++) scanf("%d", &a[i]);

	for(int i = 0; i < q; i++){
		scanf("%d%d", &x, &y);
		qry[i].l = x - 1, qry[i].r = y - 1, qry[i].id = i;
	}

	sort(qry, qry + q, cmp);
	mo();

	for(int i = 0; i < q; i++){
		printf("%d\n", resp[i]);
	}

	return 0;
}
// 1470409012297


//
//  main.cpp
//  Powerful Array
//
//  Created by Lucas Franca do Nascimento Farias on 16/05/17.
//  Copyright © 2017 Lucas Franca do Nascimento Farias. All rights reserved.
//

#include <iostream>
#include <cmath>
#include <set>
#include <utility>

#define N_MAX 1000000

typedef std::pair<int, int> Query;
struct QueryStruct {
    Query q;
    int res;
    QueryStruct* next;
};

typedef std::multiset<QueryStruct*, bool(*)(QueryStruct*, QueryStruct*)> QuerySet;

int G;

// returns true if the first argument goes before the second argument
bool QueryComp(QueryStruct* q1, QueryStruct* q2) {
    int q1g = q1->q.first/G;
    int q2g = q2->q.first/G;
    return (q1g == q2g) ? (q1->q.second < q2->q.second) : (q1g < q2g);
}

int main(int argc, const char * argv[]) {
    int N;
    // 1 <= n <= 200000
    std::cin >> N;
    int Q;
    // 1 <= t <= 200000
    std::cin >> Q;

    int* Ns = new int[N];
    G = std::ceil(std::sqrt(double(N)));

    for (int i = 0; i < N; i++) {
        std::cin >> Ns[i];
    }

    QuerySet q_set(*QueryComp);
    QueryStruct* first = nullptr;
    QueryStruct* prev = nullptr;
    for (int i = 0; i < Q; i++) {
        int q_i; // q_i starts at 1 (for index 0 at an array)
        std::cin >> q_i;
        q_i--;
        int q_j; // q_j starts at 1 (for index 0 at an array)
        std::cin >> q_j;
        q_j--;
        QueryStruct* qx = new QueryStruct { Query(q_i, q_j), 0, nullptr };
        if (!first) {
            first = qx;
            prev = qx;
        }
        q_set.insert(qx);
        prev->next = qx;
        prev = qx;
    }

    QueryStruct* q_prev = new QueryStruct { Query(-1,-1), 0, nullptr };
    int* count = new int[N_MAX+1];
    int answer = 0;
    for (QuerySet::iterator itr = q_set.begin(); itr != q_set.end(); itr++) {
        Query q_cur = (*itr)->q;
        int q_i = q_cur.first;
        int q_j = q_cur.second;
        int prev_i = q_prev->q.first;
        int prev_j = q_prev->q.second;
        if ((q_i > prev_j) or (q_j < prev_i)) { // in case the new query is completily new
            for (int index = q_i; index <= q_j; index++) {
                int val = Ns[index];
                answer -= count[val]*count[val]*val;
                count[Ns[index]]++;
                answer += count[val]*count[val]*val;
            }
        } else {
            if (q_i > prev_i) { // removing the values before the start of the query
                // prev_i inclusive
                // q_i already counted
                for (int index = prev_i; index < q_i; index++) {
                    int val = Ns[index];
                    answer -= count[val]*count[val]*val;
                    count[Ns[index]]--;
                    answer += count[val]*count[val]*val;
                }
            } else { // adding the values at the start of the query not present on the previous
                // q_i inclusive
                // prev_i already counted
                for (int index = q_i; index < prev_i; index++) {
                    int val = Ns[index];
                    answer -= count[val]*count[val]*val;
                    count[val]++;
                    answer += count[val]*count[val]*val;
                }
            }
            if (q_j > prev_j) { // adding the values at the end of the query not present on the previous
                // prev_j already counted
                // q_j inclusive
                for (int index = prev_j+1; index <= q_j; index++) {
                    int val = Ns[index];
                    answer -= count[val]*count[val]*val;
                    count[val]++;
                    answer += count[val]*count[val]*val;
                }
            } else { // removing the values after the end of the query
                // q_j already counted
                // prev_j inclusive
                for (int index = q_j+1; index <= prev_j; index++) {
                    int val = Ns[index];
                    answer -= count[val]*count[val]*val;
                    count[val]--;
                    answer += count[val]*count[val]*val;
                }
            }
        }
        (*itr)->res = answer;
        q_prev = (*itr);
    }

    QueryStruct* qs = first;
    while (qs) {
        std::cout << qs->res << std::endl;
        qs = qs->next;
    }
    return 0;
}

// 1495579418842


#include <bits/stdc++.h>

using namespace std;

#define fr(a, b, c) for(int a = b, __ = c; a < __; a++)

#define sd1(a) scanf("%d", &a)
#define sd2(a, b) scanf("%d%d", &a, &b)

#define dbg(x) cerr << ">>> " << x << endl;
#define _ << ", " <<

typedef long long ll;

const int maxn = 200111;
const int maxq = 200111;
const int BSZ  = 500;

ll sqr(ll x) { return x * x; }

struct Freq {
	ll arr[1000111];
	ll ans;

	Freq() {
		memset(arr, 0, sizeof arr);
		ans = 0;
	}

	void add(int x) {
		ans -= sqr(arr[x]) * x;
		arr[x]++;
		ans += sqr(arr[x]) * x;
	}
	void rem(int x) {
		ans -= sqr(arr[x]) * x;
		arr[x]--;
		ans += sqr(arr[x]) * x;
	}
} freq;

int n, q;
int v[maxn];
int l[maxq];
int r[maxq];
int mo[maxq];
ll ans[maxq];

bool cmp(int a, int b) {
	int Ba = l[a]/BSZ;
	int Bb = l[b]/BSZ;

	if(Ba != Bb) return Ba < Bb;

	return Ba & 1 ? r[a] < r[b] : r[a] > r[b];
}

int main() {
	sd2(n, q);

	fr(i, 0, n) sd1(v[i]);

	fr(i, 0, q) {
		sd1(l[i]);
		sd1(r[i]);
		l[i]--;
		r[i]--;
		mo[i] = i;
	}

	sort(mo, mo + q, cmp);

	int L = 0, R = -1;
	fr(i, 0, q) {
		int l = ::l[mo[i]];
		int r = ::r[mo[i]];

		while(l < L) freq.add(v[--L]);
		while(r > R) freq.add(v[++R]);

		while(l > L) freq.rem(v[L++]);
		while(r < R) freq.rem(v[R--]);

		ans[mo[i]] = freq.ans;
	}

	fr(i, 0, q) {
		printf("%lld\n", ans[i]);
	}

	return 0;
}

// 1470680722528


#include <bits/stdc++.h>

using namespace std;

#define fr(a,b,c) for(int (a) = (b); (a) < (c); ++(a))
#define rp(a,b) fr(a,0,b)
#define fre(a,b) for(int a = adj[b]; ~a; a = ant[a])
#define cl(a,b) memset((a), (b), sizeof(a))
#define sc(a) scanf("%d", &a)
#define sc2(a,b) scanf("%d%d", &a, &b)
#define sc3(a,b,c) scanf("%d%d%d", &a, &b, &c)
#define scs(s) scanf("%s", s)
#define pri(x) printf("%d\n", x)

#define iter(a) __typeof((a).begin())
#define fore(a,b) for(iter(b) a = (b).begin(); a != (b).end(); ++a)

#define st first
#define nd second
#define mp make_pair
#define pb push_back

#define db(x) cerr << #x << " == " << x << endl
#define dbs(x) cerr << x << endl
#define _ << ", " <<

typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector< vi > vii;

const int oo = 0x3f3f3f3f;

#define V 1000009
#define N 200009
#define Q 200009

int n, sq, a[N], cnt[V];
int q, l[Q], r[Q], ord[Q];
ll sum, ans[Q];

bool cmp(int a, int b) {
	int ba = l[a]/sq, bb = l[b]/sq;
	if (ba != bb) return ba < bb;
	int df = r[a]-r[b];
	if (ba&1) df = -df;
	return df < 0;
}

void put(ll i) {
	i = a[i];
	sum += ll(cnt[i]*2 + 1)*i;
	cnt[i]++;
}

void out(ll i) {
	i = a[i];
	sum -= ll(cnt[i]*2 - 1)*i;
	cnt[i]--;
}

int main() {
	while (sc2(n, q) == 2) {
		sq = sqrt(n);
		rp(i, n) sc(a[i]);

		rp(i, q) {
			sc2(l[i], r[i]);
			l[i]--;
			ord[i] = i;
		}
		sort(ord, ord+q, cmp);

		cl(cnt, 0);
		sum = 0;

		int lf = l[ord[0]], rt = lf;
		rp(ii, q) {
			int i = ord[ii];
			while (lf > l[i]) put(--lf);
			while (rt < r[i]) put(rt++);
			while (lf < l[i]) out(lf++);
			while (rt > r[i]) out(--rt);

			ans[i] = sum;
		}

		rp(i, q) cout << ans[i] << '\n';
	}
	return 0;
}

// 1470860994063


#include <bits/stdc++.h>
#define MAXN 200010
#define MAXQ 200010
#define ll long long int
#define f first
#define s second
using namespace std;

int n, q, raiz;

struct query{
	int left, right, index;
};

bool compare(query a, query b){
	if (a.left/raiz != b.left/raiz) return a.left/raiz < b.left/raiz;
	return a.right > b.right;
}

ll line[MAXN], freq[1000010], qs[MAXQ];

int main(){
	scanf("%d %d", &n, &q);
	query atual[MAXQ];

	for (int i=0; i<n; i++){
		cin >> line[i];
	}

	raiz = sqrt(n) + 1;

	for (int i=0; i<q; i++){
		scanf("%d %d", &atual[i].left, &atual[i].right);
		atual[i].index = i;
	}

	sort(atual, atual + q, compare);

	int l=0, r=-1, k, objE, objD;

	// Ks*Ks*s, sendo Ks o número de vezes que S aparece no intervalo
	ll power=0;

	for (int i=0; i<q; i++){
		objD = atual[i].right-1;
		objE = atual[i].left-1;

		// printf("------------------\n");
		// printf("Query <%d, %d>\n", objE, objD);

		while (r < objD) {
			r++;
			freq[line[r]]++;
			k = freq[line[r]];
			power += ((k*k) - (k-1)*(k-1))*line[r];
			// printf("l = %d, ++R = %d power = ", l, r);
			// cout << power << endl;
		}

		while (objD < r) {
			k = freq[line[r]];
			freq[line[r]]--;
			power += ((k-1)*(k-1) - (k*k))*line[r];
			r--;
			// printf("l = %d, R-- = %d power = ", l, r);
			// cout << power << endl;
		}

		while (l < objE) {
			k = freq[line[l]];
			freq[line[l]]--;
			power += ((k-1)*(k-1) - k*k) * line[l];
			l++;
			// printf("++l = %d, r = %d power = ", l, r);
			// cout << power << endl;
		}

		while (objE < l){
			l--;
			freq[line[l]]++;
			k = freq[line[l]];
			// cout << k  << " " << line[l] << endl;
			// cout << "POWER = " << power << endl;
			power += ((k*k) - (k-1)*(k-1))*line[l];
			// cout << "POWER = " << power << endl;
			// cout << "POWER = " << power << endl;
			// printf("--l = %d, r = %d power = ", l, r);
			// cout << power << endl;
		}

		// printf("Final:  l = %d, r = %d power = ", l, r);
		// cout << power << endl;
		qs[atual[i].index] = power;
		// printf("------------------\n");
	}

	for (int i=0; i<q; i++){
		// cout << "> ";
		cout << qs[i] << endl;
	}

	return 0;
}
// 1508558296065


#include <bits/stdc++.h>

using namespace std;

const int N = 200005, SQRT = 500, Q = 200005;

struct Query {
  int l, r, id;
  Query() {}
  Query(int l, int r, int id) : l(l), r(r), id(id) {}

  bool operator < (const Query& q) const {
    if (l / SQRT != q.l / SQRT) return l / SQRT < q.l / SQRT;
    return r > q.r;
  }
} queries[Q];

int arr[N], n;
long long freq_mo[1000005], ans_mo = 0, answer[Q];

void add_mo(int x) {
  ans_mo -= freq_mo[x] * freq_mo[x] * x;
  ++freq_mo[x];
  ans_mo += freq_mo[x] * freq_mo[x] * x;
}

void rem_mo(int x) {
  ans_mo -= freq_mo[x] * freq_mo[x] * x;
  --freq_mo[x];
  ans_mo += freq_mo[x] * freq_mo[x] * x;
}

long long get_ans_mo() {
  return ans_mo;
}

int main() {
  int q; scanf("%d %d", &n, &q);
  for (int i = 0; i < n; ++i) scanf("%d", &arr[i]);

  for (int i = 0; i < q; ++i) {
    int l, r; scanf("%d %d", &l, &r); --l; --r;
    queries[i] = Query(l, r, i);
  }
  sort(queries, queries + q);

  int l = 0, r = 0;
  add_mo(arr[0]);
  for (int i = 0; i < q; ++i) {
    while (l < queries[i].l) rem_mo(arr[l++]);
    while (l > queries[i].l) add_mo(arr[--l]);
    while (r < queries[i].r) add_mo(arr[++r]);
    while (r > queries[i].r) rem_mo(arr[r--]);
    answer[queries[i].id] = get_ans_mo();
  }

  for (int i = 0; i < q; ++i) printf("%I64d\n", answer[i]);
  return 0;
}
// 1469916195785


#include <bits/stdc++.h>

using namespace std;

#define N 200001
#define MAX 1000001

typedef long long ll;

ll a[N], ans[N], c[MAX], answer;
int SQRT;

struct node {
  int l, r, i;
} q[N];

bool cmp(node a, node b) {
  if (a.l / SQRT != b.l / SQRT)
	  return a.l / SQRT < b.l / SQRT;
  return a.r < b.r;
}

inline void add(int i) {
  answer += a[i] * ((c[a[i]] << 1) + 1);

  c[a[i]]++;
}

inline void remove(int i) {
  answer -= a[i] * ((c[a[i]] << 1) - 1);

  c[a[i]]--;
}

int main() {
  int n, t;
  scanf("%d %d", &n, &t);

  SQRT = 1.514 * sqrt(n) + 0.5;

  for (int i = 0; i < n; i++)
  	scanf("%lld", &a[i]);

  for (int i = 0; i < t; i++) {
  	scanf("%d %d", &q[i].l, &q[i].r);

  	q[i].l--; q[i].r--;
  	q[i].i = i;
  }

  sort(q, q + t, cmp);

  int L = 0, R = 0;

  for (int i = 0; i < t; i++) {
  	int l = q[i].l, r = q[i].r;

  	while (L < l) {
  	  remove(L);
  	  L++;
  	}

  	while (L > l) {
  	  add(L - 1);
  	  L--;
  	}

  	while (R <= r) {
   	  add(R);
	    R++;
	  }

  	while (R > r + 1) {
  	  remove(R - 1);
  	  R--;
  	}

  	ans[q[i].i] = answer;
  }

  for (int i = 0; i < t; i++) {
  	printf("%lld\n", ans[i]);
  }

  return 0;
}
// 1479401745337


#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
#define V     200500
#define MaxN  1000500
struct node {
    int id, l, r;
}nod[V];
int n, t, num[V], L, R, sum[MaxN];
__int64 ans[V], now;
bool cmp(node a,node b){
    int m=sqrt(1.0*n);
    if(a.l/m!=b.l/m){
        return a.l<b.l;
    }
    return a.r<b.r;
}
void f(int l, int r) {
    while(L < l) {
        now-=num[L]*(2*sum[num[L]]-- -1);
        L++;
    }
    while(R > r) {
        now -= num[R] * (2 * sum[num[R]]-- -1);
        R--;
    }
    while(L > l) {
        L--;
        now += num[L] * (2 * sum[num[L]]++ +1);
    }
    while(R < r) {
        R++;
        now += num[R] *(2 *sum[num[R]]++ +1);
    }
}
int main(){
    scanf("%d%d",&n,&t);
    for(int i=1;i<=n;i++){
        scanf("%d",&num[i]);
    }
    for(int i=1;i<=t;i++){
        scanf("%d%d",&nod[i].l,&nod[i].r);
        nod[i].id=i;
    }
    sort(nod+1,nod+1+t,cmp);
    now=L=R=0;
    memset(sum,0,sizeof(sum));
    for(int i=1;i<=t;i++){
        f(nod[i].l,nod[i].r);
        ans[nod[i].id]=now;
    }
    for(int i=1;i<=t;i++){
        printf("%I64d\n",ans[i]);
    }
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <stack>
#include <cstring>
#include <math.h>
#include<cstdio>
#include<deque>
#include<sstream>
#define mp make_pair
#define eps 1e-6
using namespace std;
int dx[] = { 0, 0, 1, -1, 1, 1, -1, -1 };
int dy[] = { 1, -1, 0, 0, 1, -1, 1, -1 };
long long a[200004], qq[200004];
long long cnt[1000008];
int n, m, x, y, sq;
struct st {
	int x, y, ind;
};
vector<st> v;
bool cmp(st a, st b) {
	if (a.x / sq < b.x / sq)
		return 1;
	if (b.x / sq < a.x / sq)
		return 0;
	return a.y < b.y;
}
int main() {
//	freopen("b.txt", "rt", stdin);
	scanf("%d%d", &n, &m);

	for (int i = 1; i <= n; i++) {
		scanf("%I64d", &a[i]);
	}
	sq = sqrt(n);
	st tmp;

	for (int i = 0; i < m; i++) {
		scanf("%d%d", &tmp.x, &tmp.y);
		tmp.ind = i;
		v.push_back(tmp);
	}
	sort(v.begin(), v.end(), cmp);
	int xx = 0, yy = 0;
	long long res = 0;
	for (int i = 0; i < m; i++) {
		x = v[i].x, y = v[i].y;
		//cout << x <<  "  " << y <<endl;
		while (xx < x) {
			res -= (cnt[a[xx]] * cnt[a[xx]] * a[xx]);
			cnt[a[xx]]--;
			res += (cnt[a[xx]] * cnt[a[xx]] * a[xx]);
			xx++;
		}
		while (xx > x) {
			xx--;
			res -= (cnt[a[xx]] * cnt[a[xx]] * a[xx]);
			cnt[a[xx]]++;
			res += (cnt[a[xx]] * cnt[a[xx]] * a[xx]);
		}
		while (yy < y) {
			yy++;
			res -= (cnt[a[yy]] * cnt[a[yy]] * a[yy]);
			cnt[a[yy]]++;
			res += (cnt[a[yy]] * cnt[a[yy]] * a[yy]);
		}
		while (yy > y) {
			res -= (cnt[a[yy]] * cnt[a[yy]] * a[yy]);
			cnt[a[yy]]--;
			res += (cnt[a[yy]] * cnt[a[yy]] * a[yy]);
			yy--;
		}
		qq[v[i].ind] = res;
	}
	for (int i = 0; i < m; i++)
		printf("%I64d\n",qq[i]);
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <stack>
#include <cstring>
#include <math.h>
#include<cstdio>
#include<deque>
#include<sstream>
#define mp make_pair
#define eps 1e-6
using namespace std;
int dx[] = { 0, 0, 1, -1, 1, 1, -1, -1 };
int dy[] = { 1, -1, 0, 0, 1, -1, 1, -1 };
int a[200004];
int cnt[1000002];
long long qq[200001];
int n, m, x, y, sq;
struct st {
	int x, y, ind;
} v[200001];
bool cmp(st a, st b) {
	if (a.x / sq != b.x / sq)
		return a.x / sq < b.x / sq;
	return a.y < b.y;
}
int main() {
	//freopen("b.txt", "rt", stdin);
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++)
		scanf("%d", &a[i]);
	sq = sqrt(n) + 1;
	for (int i = 0; i < m; i++) {
		scanf("%d%d", &v[i].x, &v[i].y);
		v[i].ind = i;
	}
	sort(v, v + m, cmp);
	int xx = 0, yy = 0;
	long long res = 0;
	for (int i = 0; i < m; i++) {
		x = v[i].x, y = v[i].y;
		while (xx < x) {
			res -= (long long) cnt[a[xx]] * (long long) cnt[a[xx]]
					* (long long) a[xx];
			cnt[a[xx]]--;
			res += (long long) (cnt[a[xx]] * (long long) cnt[a[xx]]
					* (long long) a[xx]);
			xx++;
		}
		while (xx > x) {
			xx--;
			res -= (long long) cnt[a[xx]] * (long long) cnt[a[xx]]
					* (long long) a[xx];
			cnt[a[xx]]++;
			res += (long long) (cnt[a[xx]] * (long long) cnt[a[xx]]
					* (long long) a[xx]);
		}
		while (yy < y) {
			yy++;
			res -= (long long) cnt[a[yy]] * (long long) cnt[a[yy]]
					* (long long) a[yy];
			cnt[a[yy]]++;
			res += (long long) cnt[a[yy]] * (long long) cnt[a[yy]]
					* (long long) a[yy];
		}
		while (yy > y) {
			res -= (long long) cnt[a[yy]] * (long long) cnt[a[yy]]
					* (long long) a[yy];
			cnt[a[yy]]--;
			res += (long long) cnt[a[yy]] * (long long) cnt[a[yy]]
					* (long long) a[yy];
			yy--;
		}
		qq[v[i].ind] = res;
		//cout << res << endl;
	}
	for (int i = 0; i < m; i++)
		printf("%I64d\n", qq[i]);

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
long long a[200005];
long long f[1000005];
long long ans[200005];
vector<pair<pair<int, int>, int> > v;
int blk;
bool comp(const pair<pair<int, int>, int> &a,
		const pair<pair<int, int>, int> &b) {
	int la = a.first.first / blk;
	int lb = b.first.first / blk;
	if (la != lb)
		return la < lb;
	int ra = a.first.second / blk;
	int rb = b.first.second / blk;
	return ra < rb;
}
int main(int argc, char **argv) {
	//freopen("a.txt", "r", stdin);
	int n, m;
	scanf("%d%d", &n, &m);
	blk = ceil(sqrt(n));
	for (int i = 0; i < n; i++) {
		scanf("%lld", &a[i]);
	}
	int x, y;
	for (int k = 0; k < m; k++) {
		scanf("%d%d", &x, &y);
		x--, y--;
		v.push_back(mp(mp(x, y), k));
	}
	sort(v.begin(), v.end(), comp);
	int st = 0, ed = 0;
	long long sum = 0;

	for (int k = 0; k < m; k++) {
		x = v[k].first.first;
		y = v[k].first.second;
		int ind = v[k].second;
		if (k == 0) {
			for (int i = x; i <= y; i++) {
				sum -= f[a[i]] * f[a[i]] * a[i];
				f[a[i]]++;
				sum += f[a[i]] * f[a[i]] * a[i];
			}
		} else {
			if (x > st) {
				for (int i = st; i < x; i++) {
					sum -= f[a[i]] * f[a[i]] * a[i];
					f[a[i]]--;
					sum += f[a[i]] * f[a[i]] * a[i];
				}

			} else if (x < st) {
				for (int i = x; i < st; i++) {
					sum -= f[a[i]] * f[a[i]] * a[i];
					f[a[i]]++;
					sum += f[a[i]] * f[a[i]] * a[i];
				}
			}

			if (y > ed) {
				for (int i = ed + 1; i <= y; i++) {
					sum -= f[a[i]] * f[a[i]] * a[i];
					f[a[i]]++;
					sum += f[a[i]] * f[a[i]] * a[i];
				}

			} else if (y < ed) {
				for (int i = y + 1; i <= ed; i++) {
					sum -= f[a[i]] * f[a[i]] * a[i];
					f[a[i]]--;
					sum += f[a[i]] * f[a[i]] * a[i];
				}

			}
		}
		st = x, ed = y;
		ans[ind] = sum;
	}

	for (int i = 0; i < m; i++) {
		printf("%I64d\n", ans[i]);
	}

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <stack>
#include <cstring>
#include <math.h>
#include<cstdio>
#include<deque>
#include<sstream>
#define mp make_pair
#define eps 1e-6
using namespace std;
int dx[] = { 0, 0, 1, -1, 1, 1, -1, -1 };
int dy[] = { 1, -1, 0, 0, 1, -1, 1, -1 };
int a[200004];
long long cnt[1000008], qq[200004];
int n, m, x, y, sq;
pair<pair<int, int>, int> v[200004];
bool cmp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) {
	if (a.first.first / sq != b.first.first / sq)
		return a.first.first / sq < b.first.first / sq;
	return a.first.second < b.first.second;
}
int main() {
	//freopen("b.txt", "rt", stdin);
	scanf("%d%d", &n, &m);

	for (int i = 1; i <= n; i++)
		scanf("%d", &a[i]);
	sq = sqrt(n);
	for (int i = 0; i < m; i++) {
		scanf("%d%d", &v[i].first.first, &v[i].first.second);
		v[i].second = i;
	}
	sort(v, v + m, cmp);
	int xx = 0, yy = 0;
	long long res = 0;
	for (int i = 0; i < m; i++) {
		x = v[i].first.first, y = v[i].first.second;
		while (xx < x) {
			res -= (cnt[a[xx]] * cnt[a[xx]] * a[xx]);
			cnt[a[xx]]--;
			res += (cnt[a[xx]] * cnt[a[xx]] * a[xx]);
			xx++;
		}
		while (xx > x) {
			xx--;
			res -= (cnt[a[xx]] * cnt[a[xx]] * a[xx]);
			cnt[a[xx]]++;
			res += (cnt[a[xx]] * cnt[a[xx]] * a[xx]);
		}
		while (yy < y) {
			yy++;
			res -= (cnt[a[yy]] * cnt[a[yy]] * a[yy]);
			cnt[a[yy]]++;
			res += (cnt[a[yy]] * cnt[a[yy]] * a[yy]);
		}
		while (yy > y) {
			res -= (cnt[a[yy]] * cnt[a[yy]] * a[yy]);
			cnt[a[yy]]--;
			res += (cnt[a[yy]] * cnt[a[yy]] * a[yy]);
			yy--;
		}
		qq[v[i].second] = res;
		//cout << res << endl;
	}
	for (int i = 0; i < m; i++)
		printf("%I64d\n", qq[i]);

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <stack>
#include <cstring>
#include <math.h>
#include<cstdio>
#include<deque>
#include<sstream>
#define mp make_pair
#define eps 1e-6
using namespace std;
int dx[] = { 0, 0, 1, -1, 1, 1, -1, -1 };
int dy[] = { 1, -1, 0, 0, 1, -1, 1, -1 };
long long a[200004], qq[200004];
long long cnt[1000008];
int n, m, x, y, sq;
struct st {
	int x, y, ind;
};
st v[200004];
bool cmp(st a, st b) {
	if (a.x / sq != b.x / sq)
		return a.x / sq < b.x / sq;
	return a.y < b.y;
}
int main() {
	//freopen("b.txt", "rt", stdin);
	scanf("%d%d", &n, &m);

	for (int i = 1; i <= n; i++) {
		scanf("%I64d", &a[i]);
	}
	sq = sqrt(n);

	for (int i = 0; i < m; i++) {
		scanf("%d%d", &v[i].x, &v[i].y);
		v[i].ind = i;
	}
	sort(v, v + m, cmp);
	int xx = 0, yy = 0;
	long long res = 0;
	for (int i = 0; i < m; i++) {
		x = v[i].x, y = v[i].y;
		//cout << x << " " << y << endl;
		while (xx < x) {
			res -= (cnt[a[xx]] * cnt[a[xx]] * a[xx]);
			cnt[a[xx]]--;
			res += (cnt[a[xx]] * cnt[a[xx]] * a[xx]);
			xx++;
		}
		while (xx > x) {
			xx--;
			res -= (cnt[a[xx]] * cnt[a[xx]] * a[xx]);
			cnt[a[xx]]++;
			res += (cnt[a[xx]] * cnt[a[xx]] * a[xx]);
		}
		while (yy < y) {
			yy++;
			res -= (cnt[a[yy]] * cnt[a[yy]] * a[yy]);
			cnt[a[yy]]++;
			res += (cnt[a[yy]] * cnt[a[yy]] * a[yy]);
		}
		while (yy > y) {
			res -= (cnt[a[yy]] * cnt[a[yy]] * a[yy]);
			cnt[a[yy]]--;
			res += (cnt[a[yy]] * cnt[a[yy]] * a[yy]);
			yy--;
		}
		qq[v[i].ind] = res;
		//cout << res << endl;
	}
	for (int i = 0; i < m; i++)
		printf("%I64d\n", qq[i]);

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <stack>
#include <cstring>
#include <math.h>
#include<cstdio>
#include<deque>
#include<sstream>
#define mp make_pair
#define eps 1e-6
using namespace std;
int dx[] = { 0, 0, 1, -1, 1, 1, -1, -1 };
int dy[] = { 1, -1, 0, 0, 1, -1, 1, -1 };
long long a[200004], qq[200004];
long long cnt[1000008];
int n, m, x, y, sq;
struct st {
	int x, y, ind;
};
vector<st> v;
bool cmp(st a, st b) {
	if (a.x / sq < b.x / sq)
		return 1;
	if (b.x / sq < a.x / sq)
		return 0;
	return a.y < b.y;
}
int main() {
//	freopen("b.txt", "rt", stdin);
	scanf("%d%d", &n, &m);

	for (int i = 1; i <= n; i++) {
		scanf("%I64d", &a[i]);
	}
	sq = sqrt(n);
	st tmp;

	for (int i = 0; i < m; i++) {
		scanf("%d%d", &tmp.x, &tmp.y);
		tmp.ind = i;
		v.push_back(tmp);
	}
	sort(v.begin(), v.end(), cmp);
	int xx = 0, yy = 0;
	long long res = 0;
	for (int i = 0; i < m; i++) {
		x = v[i].x, y = v[i].y;
		//cout << x <<  "  " << y <<endl;
		while (xx < x) {
			res -= (cnt[a[xx]] * cnt[a[xx]] * a[xx]);
			cnt[a[xx]]--;
			res += (cnt[a[xx]] * cnt[a[xx]] * a[xx]);
			xx++;
		}
		while (xx > x) {
			xx--;
			res -= (cnt[a[xx]] * cnt[a[xx]] * a[xx]);
			cnt[a[xx]]++;
			res += (cnt[a[xx]] * cnt[a[xx]] * a[xx]);
		}
		while (yy < y) {
			yy++;
			res -= (cnt[a[yy]] * cnt[a[yy]] * a[yy]);
			cnt[a[yy]]++;
			res += (cnt[a[yy]] * cnt[a[yy]] * a[yy]);
		}
		while (yy > y) {
			res -= (cnt[a[yy]] * cnt[a[yy]] * a[yy]);
			cnt[a[yy]]--;
			res += (cnt[a[yy]] * cnt[a[yy]] * a[yy]);
			yy--;
		}
		qq[v[i].ind] = res;
	}
	for (int i = 0; i < m; i++)
		cout << qq[i] << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
int a[200005];
long long f[1000005];
long long ans[200005];
struct st {
	int x, y, ind;
} v[200005];
int blk;
inline bool comp(const st &a, const st &b) {
	int la = a.x / blk;
	int lb = b.x / blk;
	if (la != lb)
		return la < lb;
	int ra = a.y / blk;
	int rb = b.y / blk;
	return ra < rb;
}
int main(int argc, char **argv) {
	//freopen("a.txt", "r", stdin);
	int n, m;
	scanf("%d%d", &n, &m);
	blk = ceil(sqrt(n));
	for (int i = 0; i < n; i++) {
		scanf("%d", &a[i]);
	}
	int x, y;
	for (int k = 0; k < m; k++) {
		scanf("%d%d", &x, &y);
		x--, y--;
		v[k].x = x;
		v[k].y = y;
		v[k].ind = k;
	}
	sort(v, v + m, comp);
	int st = 0, ed = -1;
	long long sum = 0;

	for (int k = 0; k < m; k++) {
		x = v[k].x;
		y = v[k].y;
		int ind = v[k].ind;
		if (x > st) {
			for (int i = st; i < x; i++) {
				sum -= f[a[i]] * f[a[i]] * a[i];
				f[a[i]]--;
				sum += f[a[i]] * f[a[i]] * a[i];
			}

		} else if (x < st) {
			for (int i = x; i < st; i++) {
				sum -= f[a[i]] * f[a[i]] * a[i];
				f[a[i]]++;
				sum += f[a[i]] * f[a[i]] * a[i];
			}
		}

		if (y > ed) {
			for (int i = ed + 1; i <= y; i++) {
				sum -= f[a[i]] * f[a[i]] * a[i];
				f[a[i]]++;
				sum += f[a[i]] * f[a[i]] * a[i];
			}

		} else if (y < ed) {
			for (int i = y + 1; i <= ed; i++) {
				sum -= f[a[i]] * f[a[i]] * a[i];
				f[a[i]]--;
				sum += f[a[i]] * f[a[i]] * a[i];
			}

		}
		st = x, ed = y;
		ans[ind] = sum;
	}

	for (int i = 0; i < m; i++) {
		printf("%lld\n", ans[i]);
	}

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <stack>
#include <cstring>
#include <math.h>
#include<cstdio>
#include<deque>
#include<sstream>
#define mp make_pair
#define eps 1e-6
using namespace std;
int dx[] = { 0, 0, 1, -1, 1, 1, -1, -1 };
int dy[] = { 1, -1, 0, 0, 1, -1, 1, -1 };
long long a[200004], qq[200004];
long long cnt[1000008];
int n, m, x, y, sq;
struct st {
	int x, y, ind;
};
vector<st> v;
bool cmp(st a, st b) {
	if (a.x / sq != b.x / sq)
		return a.x < b.x;
	return a.y < b.y;
}
int main() {
//	freopen("b.txt", "rt", stdin);
	scanf("%d%d", &n, &m);

	for (int i = 1; i <= n; i++) {
		scanf("%I64d", &a[i]);
	}
	sq = sqrt(n);
	st tmp;

	for (int i = 0; i < m; i++) {
		scanf("%d%d", &tmp.x, &tmp.y);
		tmp.ind = i;
		v.push_back(tmp);
	}
	sort(v.begin(), v.end(), cmp);
	int xx = 0, yy = 0;
	long long res = 0;
	for (int i = 0; i < m; i++) {
		x = v[i].x, y = v[i].y;
		//cout << x <<  "  " << y <<endl;
		while (xx < x) {
			res -= (cnt[a[xx]] * cnt[a[xx]] * a[xx]);
			cnt[a[xx]]--;
			res += (cnt[a[xx]] * cnt[a[xx]] * a[xx]);
			xx++;
		}
		while (xx > x) {
			xx--;
			res -= (cnt[a[xx]] * cnt[a[xx]] * a[xx]);
			cnt[a[xx]]++;
			res += (cnt[a[xx]] * cnt[a[xx]] * a[xx]);
		}
		while (yy < y) {
			yy++;
			res -= (cnt[a[yy]] * cnt[a[yy]] * a[yy]);
			cnt[a[yy]]++;
			res += (cnt[a[yy]] * cnt[a[yy]] * a[yy]);
		}
		while (yy > y) {
			res -= (cnt[a[yy]] * cnt[a[yy]] * a[yy]);
			cnt[a[yy]]--;
			res += (cnt[a[yy]] * cnt[a[yy]] * a[yy]);
			yy--;
		}
		qq[v[i].ind] = res;
	}
	for (int i = 0; i < m; i++)
		printf("%I64d\n", qq[i]);
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <stack>
#include <cstring>
#include <math.h>
#include<cstdio>
#include<deque>
#include<sstream>
#define mp make_pair
#define eps 1e-6
using namespace std;
int dx[] = { 0, 0, 1, -1, 1, 1, -1, -1 };
int dy[] = { 1, -1, 0, 0, 1, -1, 1, -1 };
long long a[200004];
long long cnt[1000002], qq[200001];
int n, m, x, y, sq;
struct st {
	int x, y, ind;
} v[200001];
bool cmp(st a, st b) {
	if (a.x / sq != b.x / sq)
		return a.x / sq < b.x / sq;
	return a.y < b.y;
}
int main() {
	//freopen("b.txt", "rt", stdin);
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++)
		scanf("%d", &a[i]);
	sq = sqrt(n) + 1;
	for (int i = 0; i < m; i++) {
		scanf("%d%d", &v[i].x, &v[i].y);
		v[i].ind = i;
	}
	sort(v, v + m, cmp);
	int xx = 0, yy = 0;
	long long res = 0;
	for (int i = 0; i < m; i++) {
		x = v[i].x, y = v[i].y;
		while (xx < x) {
			res -= (cnt[a[xx]] * cnt[a[xx]] * a[xx]);
			cnt[a[xx]]--;
			res += (cnt[a[xx]] * cnt[a[xx]] * a[xx]);
			xx++;
		}
		while (xx > x) {
			xx--;
			res -= (cnt[a[xx]] * cnt[a[xx]] * a[xx]);
			cnt[a[xx]]++;
			res += (cnt[a[xx]] * cnt[a[xx]] * a[xx]);
		}
		while (yy < y) {
			yy++;
			res -= (cnt[a[yy]] * cnt[a[yy]] * a[yy]);
			cnt[a[yy]]++;
			res += (cnt[a[yy]] * cnt[a[yy]] * a[yy]);
		}
		while (yy > y) {
			res -= (cnt[a[yy]] * cnt[a[yy]] * a[yy]);
			cnt[a[yy]]--;
			res += (cnt[a[yy]] * cnt[a[yy]] * a[yy]);
			yy--;
		}
		qq[v[i].ind] = res;
		//cout << res << endl;
	}
	for (int i = 0; i < m; i++)
		printf("%I64d\n", qq[i]);

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <stack>
#include <cstring>
#include <math.h>
#include<cstdio>
#include<deque>
#include<sstream>
#define mp make_pair
#define eps 1e-6
using namespace std;
int dx[] = { 0, 0, 1, -1, 1, 1, -1, -1 };
int dy[] = { 1, -1, 0, 0, 1, -1, 1, -1 };
int a[200004];
long long cnt[1000008], qq[200004];
int n, m, x, y, sq;
vector<int> v[200004];
bool cmp(vector<int> a, vector<int> b) {
	if (a[0] / sq != b[0] / sq)
		return a[0] / sq < b[0] / sq;
	return a[1] < b[1];
}
int main() {
	//freopen("b.txt", "rt", stdin);
	scanf("%d%d", &n, &m);

	for (int i = 1; i <= n; i++)
		scanf("%d", &a[i]);
	sq = sqrt(n);
	for (int i = 0; i < m; i++) {
		v[i].resize(3);
		scanf("%d%d", &v[i][0], &v[i][1]);
		v[i][2] = i;
	}
	sort(v, v + m, cmp);
	int xx = 0, yy = 0;
	long long res = 0;
	for (int i = 0; i < m; i++) {
		x = v[i][0], y = v[i][1];
		while (xx < x) {
			res -= (cnt[a[xx]] * cnt[a[xx]] * a[xx]);
			cnt[a[xx]]--;
			res += (cnt[a[xx]] * cnt[a[xx]] * a[xx]);
			xx++;
		}
		while (xx > x) {
			xx--;
			res -= (cnt[a[xx]] * cnt[a[xx]] * a[xx]);
			cnt[a[xx]]++;
			res += (cnt[a[xx]] * cnt[a[xx]] * a[xx]);
		}
		while (yy < y) {
			yy++;
			res -= (cnt[a[yy]] * cnt[a[yy]] * a[yy]);
			cnt[a[yy]]++;
			res += (cnt[a[yy]] * cnt[a[yy]] * a[yy]);
		}
		while (yy > y) {
			res -= (cnt[a[yy]] * cnt[a[yy]] * a[yy]);
			cnt[a[yy]]--;
			res += (cnt[a[yy]] * cnt[a[yy]] * a[yy]);
			yy--;
		}
		qq[v[i][2]] = res;
		//cout << res << endl;
	}
	for (int i = 0; i < m; i++)
		printf("%I64d\n", qq[i]);

	return 0;
}