#include "bits/stdc++.h"
#define int long long
#define endl '\n'
using namespace std;
 
typedef long long ll;
typedef long double ld;
#define db(x) cerr << #x << ": " << x << '\n';
#define read(a) int a; cin >> a;
#define reads(s) string s; cin >> s;
#define readb(a, b) int a, b; cin >> a >> b;
#define readc(a, b, c) int a, b, c; cin >> a >> b >> c;
#define readarr(a, n) int a[(n) + 1] = {}; FOR(i, 1, (n)) {cin >> a[i];}
#define readmat(a, n, m) int a[n + 1][m + 1] = {}; FOR(i, 1, n) {FOR(j, 1, m) cin >> a[i][j];}

#define print(a) cout << a << endl;
#define printarr(a, n) FOR (i, 1, n) cout << a[i] << " "; cout << endl;
#define printv(v) for (int i: v) cout << i << " "; cout << endl;
#define printmat(a, n, m) FOR (i, 1, n) {FOR (j, 1, m) cout << a[i][j] << " "; cout << endl;}
#define all(v) v.begin(), v.end()
#define sz(v) (int)(v.size())
#define rz(v, n) v.resize((n) + 1);
#define pb push_back
#define fi first
#define se second
#define vi vector <int>
#define pi pair <int, int>
#define vpi vector <pi>
#define vvi vector <vi>
#define setprec cout << fixed << showpoint << setprecision(20);
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const ll N = 2e5 + 1;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int power (int a, int b = mod - 2)
{
    int res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}

//cp-algo
int gcd(int a, int b, int& x, int& y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    int x1, y1;
    int d = gcd(b, a % b, x1, y1);
    x = y1;
    y = x1 - y1 * (a / b);
    return d;
}

bool find_any_solution(int a, int b, int c, int &x0, int &y0, int &g) {
    g = gcd(abs(a), abs(b), x0, y0);
    if (c % g) {
        return false;
    }

    x0 *= c / g;
    y0 *= c / g;
    if (a < 0) x0 = -x0;
    if (b < 0) y0 = -y0;
    return true;
}

signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	
	read(t);
	while (t--)
	{
	    readc(n, s, k);
	    int x, y, g;
	    bool status = find_any_solution(n, -k, s, x, y, g);
	    if (!status) {cout << -1 << endl; continue;}
	    
	    n /= g;
	    int z = y/n, ans = inf;
	    if (y - z*n > 0) ans = min(ans, y - z*n);
	    if (y - (z - 1)*n > 0) ans = min(ans, y - (z - 1)*n);
	    print(ans);
	}
}



















// Problem: C - Squared Error
// Contest: AtCoder - AtCoder Beginner Contest 194
// URL: https://atcoder.jp/contests/abc194/tasks/abc194_c
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include<bits/stdc++.h>
typedef long long int ll;
#define pb push_back
#define pi  3.141592653589793238462643383279
#define int long long
#define ff first
#define ss second
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define rep(i,x,y) for(int i=(int)x; i<y; i++)
#define high_functioning_sociopath  ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
const unsigned int M = 998244353;
/*_____________________________THE GAME IS ON_____________________________*/
void solve()
{
      int n;
      cin >> n;
      vector<int>v(n);
      for(int i=0;i<n;i++) cin >> v[i];
      int suf1[n]={0};
      int suf2[n]={0};
      suf1[n-1] = v[n-1];
      suf2[n-1] = (v[n-1]*v[n-1]);
      for(int i=n-2;i>=0;i--)
      {
      	  suf1[i] = suf1[i+1] + v[i];
      	  suf2[i] = suf2[i+1] + (v[i]*v[i]);
      }
      int res = 0;
      int j = n-1;
      for(int i=0;i<n-1;i++)
      {
      	   int a = v[i]*v[i];
      	   a*=j;
      	   //cout << suf2[i+1] << " " << suf1[i+1] << endl;
      	   int b = suf2[i+1] - (2*v[i]*suf1[i+1]) + a;
      	   res+=b;
      	   j--;
      }
      cout << res ;
}
int32_t main()
{
   high_functioning_sociopath
   //pre();
   ll t=1;
  // cin>>t;
   while(t--)
   {
      solve();
   }
}