#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;

struct edge {
    int to;
    long long c;
    long long d;
};

ll get_cost(ll c, ll d, ll t_now) {
    ll t = sqrt(d);
    ll dt = max(0LL, t - t_now);
    ll res = c + (d / (t_now + dt + 1)) + dt;
    res = min(res, c + (d / (t_now + dt + 1 + 1)) + dt + 1);
    if (dt) res = min(res, c + (d / (t_now + dt - 1 + 1)) + dt - 1);
    return res;
}

const long long INF = 10e16;

// 0-indexed
vector<long long> dijkstra(int start, vector<vector<edge>> &G) {
    int N = G.size();

    vector<long long> dist(N, INF);
    priority_queue<P, vector<P>, greater<P>> pq;

    dist.at(start) = 0;
    pq.emplace(P{dist.at(start), start});

    while (!pq.empty()) {
        P p = pq.top();
        pq.pop();
        int cur = p.second;
        ll t_now = p.first;
        if (dist.at(cur) < p.first) continue;
        for (auto &x : G.at(cur)) {
            ll cost = get_cost(x.c, x.d, t_now);
            if (dist.at(x.to) <= dist.at(cur) + cost) continue;
            dist.at(x.to) = dist.at(cur) + cost;
            pq.emplace(P{dist.at(x.to), x.to});
        }
    }
    return dist;
}

int main() {
    int n, m;
    cin >> n >> m;
    vector<vector<edge>> G(n);
    rep(i, m) {
        ll a, b, c, d;
        cin >> a >> b >> c >> d;
        a--, b--;
        G[a].emplace_back(edge{(int)b, c, d});
        G[b].emplace_back(edge{(int)a, c, d});
    }

    const auto &res = dijkstra(0, G);
    if (res[n - 1] == INF) {
        cout << -1 << endl;
    } else {
        cout << res[n - 1] << endl;
    }
}

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int mod = 1'000'000'007;

int main() {
    int r1, c1, r2, c2, ans;
    cin >> r1 >> c1 >> r2 >> c2;
    int r_diff = r1 - r2, c_diff = c1 - c2;

    if (r1==r2 && c1==c2) ans=0;
    else if (r1+c1==r2+c2 || r1-c1==r2-c2 || abs(r1-r2)+abs(c1-c2)<=3) ans=1;
    else if (r1-r_diff+c1-r_diff==r2+c2) ans=2;
    else if (r1-c1==r2-c2) ans=2;
    else if (abs(c1+r_diff-c2)<=3) ans=2;
    else if (abs(r1-r_diff-r2)+abs(c1-r_diff-c2)<=3) ans=2;
    else if (abs(r1-r2)+abs(c1-c2)<=6) ans=2;
    else if ((r1+c1)%2==(r2+c2)%2) ans=2;
    else ans=3;
    cout << ans << endl;
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

/*long long gcd(long long a,long long b){
	if(b==0){
		return a;
	}
	return(gcd(b,a%b));
}
bool prime(long long n){
	if(n<2){
		return false;
	}else{
		for(int i=2;i*i<=n;i++){
			if(n%i==0){
				return false;
			}
		}
	}
	return true;
}*/
/* PRIME FACTOR ARRAY
vector<int> pfactor(long long n){
	vector<int>f;
	for(int i=2;i*i<=n;i++){
		while(n%i==0){
			f.push_back(i);
			n/=i;
		}
	}if(n>1){
		f.push_back(n);
	}
	return f;
}*/
 //SIEVE OF ERSATHANOS
/*bool Sieve(long long n)
{

    bool prime[n+1];
    memset(prime, true, sizeof(prime));

    for (int p=2; p*p<=n; p++)
    {

        if (prime[p] == true)
        {

            for (int i=p*p; i<=n; i += p)
                prime[i] = false;
        }
    }

   	bool a=false;
    for (int p=2; p<=n; p++){

       if (prime[p] && n%p==0)
          a= true;
          break;
    }
	return a;
}
long long Xor(int x,int y){
	return (x | y) & (~x | ~y);
}
string dbinary(long long n){
	string a;
	for (int i = 64; i >= 0; i--) {
        int k = n >> i;
        if (k & 1)
            a+='1';
        else
            a+='0';
    }
    return a;
}*/

/*long long gcd(long long a,long long b){
	if(b==0){
		return a;
	}
	return(gcd(b,a%b));
}
bool prime(long long n){
	if(n<2){
		return false;
	}else{
		for(int i=2;i*i<=n;i++){
			if(n%i==0){
				return false;
			}
		}
	}
	return true;
}*/
/* PRIME FACTOR ARRAY
vector<int> pfactor(long long n){
	vector<int>f;
	for(int i=2;i*i<=n;i++){
		while(n%i==0){
			f.push_back(i);
			n/=i;
		}
	}if(n>1){
		f.push_back(n);
	}
	return f;
}*/
 //SIEVE OF ERSATHANOS
/*bool Sieve(long long n)
{

    bool prime[n+1];
    memset(prime, true, sizeof(prime));

    for (int p=2; p*p<=n; p++)
    {

        if (prime[p] == true)
        {

            for (int i=p*p; i<=n; i += p)
                prime[i] = false;
        }
    }

   	bool a=false;
    for (int p=2; p<=n; p++){

       if (prime[p] && n%p==0)
          a= true;
          break;
    }
	return a;
}
long long Xor(int x,int y){
	return (x | y) & (~x | ~y);
}
string dbinary(long long n){
	string a;
	for (int i = 64; i >= 0; i--) {
        int k = n >> i;
        if (k & 1)
            a+='1';
        else
            a+='0';
    }
    return a;
}*/
long long octo(long long deciNum)
{

    int octalNum = 0, countval = 1;
    int dNo = deciNum;

    while (deciNum != 0) {

        // decimals remainder is calculated
        int remainder = deciNum % 8;

        // storing the octalvalue
        octalNum += remainder * countval;

        // storing exponential value
        countval = countval * 10;
        deciNum /= 8;
    }
    return octalNum;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	long long n;
	cin>>n;
	long long count=0;
	long long j=1;
	while(j<=n){
		string a=to_string(j);int c=0;
		for(int i=0;i<a.length();i++){
			if(a.at(i)=='7'){
				c=1;
				count+=1;
				break;
			}
		}
		if(c==0){
			long long ans=octo(j);
			string d=to_string(ans);
			for(int i=0;i<d.length();i++){
				if(d.at(i)=='7'){
					count+=1;
					break;
				}
			}
		}
		j++;
	}
	cout<<n-count<<"\n";

	return 0;
}

#include <bits/stdc++.h>

#define int long long int
#define all(x) x.begin(),x.end()
#define allr(x) x.rbegin(),x.rend()

using namespace std;

signed main(){
  ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
  int n;
  cin >> n;
  int ans = 0;
  for(int i = 1;i <= n;i++){
    int temp = i;
    bool flag = true;
    while(temp){
      if(temp%10 == 7) flag = false;
      temp /= 10;
    }
    temp = i;
    while(temp){
      if(temp%8 == 7) flag = false;
      temp /= 8;
    }
    ans += flag;
  }
  cout << ans;
}


#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
#define pb push_back
#define mpr make_pair
#define pii pair<int, int>
#define ll long long
#define dl long double
#define all(arr) arr.begin(), arr.end()
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define pie 3.14159265358979323846264338327950L
#define mid(l, r) l + (r - l) / 2
#define mem0(arr) memset(arr, 0,sizeof(arr));
#define mem1(arr) memset(arr,-1,sizeof(arr));
#define vec vector<int>
#define yes cout << "YES" << endl;
#define no cout << "NO" << endl;
#define endl "\n"
#define int long long
#define sz(x) (int)x.size()
#define template_array_size (int)1e6 + 6
#define INF (int)1e18
#define trace1(x)                cerr << #x << ": " << x << endl;
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;
using namespace std;

int gcd(int a,int b){ if(!a)return b;return gcd(b%a,a);}
int lcm(int a, int b) { return (a*b)/ gcd(a,b);}

const int MOD = 1e9 + 7;
const int mod = 1e9 + 7;

void sout(){
	cout<<endl;
}
template <typename T,typename... Types>
void sout(T var1,Types... var2){
	cout<<var1<<" ";
	sout(var2...);
}
template<typename T>
void sout(vector<T> arr) {
    for(auto it : arr) {
        cout<<it<<" ";
    }
    sout();
}

int n,m;
int temp;
int cnt;
int ans;
int ind;
int i,j;
int a,b;

vector<int> arr;
vector<int> brr;
int k;

void test_case () {

    int n;
    cin>>n;

    int sum = (n+1);

    int l = 1;
    int r = 1e10;

    int ans = 1;

    while(l<=r) {
        int mid =mid(l,r);

        int cnt = (mid) * (mid + 1);
        cnt= cnt / 2;

        if(cnt <= sum) {
            l = mid + 1;
            ans = max(ans, mid);
        } else {
            r = mid - 1;
        }
    }

    int fin = n - ans + 1;

    cout<<fin;

}

int32_t main ()
{
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cout << fixed << setprecision(10);

    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        // freopen("output.txt","w",stdout);
    #endif

    int t = 1;
    // cin >> t;
    while (t--)
    {
        test_case();
    }

    cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
}

/* stuff you should look for
	* int overflow, array bounds
	* special cases (n=1?)
	* do smth instead of nothing and stay organized
	* WRITE STUFF DOWN
	* DON'T GET STUCK ON ONE APPROACH
*/

#include <bits/stdc++.h>
using namespace std;
int main() {
  int64_t n;
  cin >> n;
  int64_t bound = 2*(n+1);
  int64_t m = sqrt(n);

  while((m+1)*(m+2)<=bound){ //次のやつが上界を超えない
  	m++;
  }
  cout << n+1-m;
}


#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using pii = pair<int, int>;

int n, m;
vector<ll> a, b;
vector<vector<int>> g;
vector<bool> check;

ll dfs(int v) {
	check[v] = true;
	ll ret = b[v] - a[v];
	for (auto nv : g[v]) {
		if (check[nv]) continue;
		ret += dfs(nv);
	}
	return ret;
}

int main() {
	cin >> n >> m;
	a.resize(n);
	b.resize(n);
	rep(i, n) cin >> a[i];
	rep(i, n) cin >> b[i];
	g.resize(n);
	rep(i, m) {
		int c, d;
		cin >> c >> d;
		c--, d--;
		g[c].push_back(d);
		g[d].push_back(c);
	}
	check.resize(n);
	bool ok = true;
	for (int i = 0; i < n; i++) {
		if (check[i]) continue;
		if (dfs(i) != 0) ok = false;
	}
	if (ok) cout << "Yes" << endl;
	else cout << "No" << endl;
	return 0;
}

#define ll long long
#define mod 1000000007

#include<bits/stdc++.h>

using namespace std;

const int maxn = 200000;
ll n, m;
ll a[maxn], b[maxn];
ll ss, st; // ssはグラフの実際の合計, stはグラフの理想の合計(ss=stなら"Yes")
ll mark[maxn]; // すでに見た点か判断する
vector<int> adj[maxn]; // グラフ

// 深さ優先探索
void dfs(int v) {
    ss += a[v];
    st += b[v];
    mark[v] = 1;
    for (auto u : adj[v]) {
        if (!mark[u]) dfs(u);
    }
}

int main()
{
	// 入力処理
    cin >> n >> m;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= n; i++) cin >> b[i];
    for (int i = 1; i <= m; i++) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    for (int i = 1; i <= n; i++) {
		// 一つの島(隣接ノードの和)ごとに判定する
		// 一つでもss!=stの島があれば"No"
        if (!mark[i]) {
            ss = 0;
            st = 0;
            dfs(i);
            if (ss != st) {
                cout << "No" << endl;
                return 0;
            }
        }
    }
    cout << "Yes" << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

bool checkValidStr(int N, string T) {
  string startsWith = T.substr(0, 3);
  if(startsWith == "110") {
    for(int i = 0; i < N; i++) {
      char c = T.at(i);
      int rest = i % 3;
      if(rest == 0 || rest == 1) {
        if(c != '1') {
          return false;
        }
      } else {
        if(c != '0') {
          return false;
        }
      }
    }
  }else if(startsWith == "101") {
    for(int i = 0; i < N; i++) {
      char c = T.at(i);
      int rest = i % 3;
      if(rest == 0 || rest == 2) {
        if(c != '1') {
          return false;
        }
      } else {
        if(c != '0') {
          return false;
        }
      }
    }
  } else if(startsWith == "011") {
    for(int i = 0; i < N; i++) {
      char c = T.at(i);
      int rest = i % 3;
      if(rest == 1 || rest == 2) {
        if(c != '1') {
          return false;
        }
      } else {
        if(c != '0') {
          return false;
        }
      }
    }
  }
  return true;
}

int64_t count110(int N, string T) {
  if(N == 1) {
    if(T == "0") {
      return pow(10, 10);
    } else {
      return pow(10, 10) * 2;
    }
  }
  if(N == 2) {
    if(T == "00") {
      return 0;
    } else if(T == "01") {
      return pow(10, 10) - 1;
    } else {
      return pow(10, 10); // "11"のみ
    }
  }
  if(!checkValidStr(N, T)) {
    return 0;
  }
  string startsWith = T.substr(0, 3);
  int64_t result = 0;
  if(startsWith == "110") {
    int rest = (N - 1) / 3;
    result = pow(10,10) - rest;
  }else if(startsWith == "101") {
    int rest = (N - 3) / 3;
    result = pow(10,10) - 1 - rest;
  } else if(startsWith == "011") {
    int rest = (N - 2) / 3;
    result = pow(10,10) - 1 - rest;
  }
  return result;
}

int main()
{
  int N;
  cin >> N;
  string T;
  cin >> T;
  int64_t count = count110(N, T);
  cout << count << endl;
  return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define mod 998244353
#define all(a) a.begin(),a.end()
#define dbg(n) cout<<#n<<' '<<n<<endl;
#define dbg_v(v) cout<<#v<<":";for(auto x:v) cout<<" "<<x; cout<<endl;
ll power(ll x,ll y){ll res = 1;while(y>0){if(y&1)res = (res*x)%mod;y=y>>1;x=(x*x)%mod;}return res;}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll n;
	cin>>n;
	string s;
	cin>>s;
	ll tmp=1e10;
	if(n==1){
		if(s=="1")
			cout<<tmp*2<<endl;
		else
			cout<<tmp<<endl;
		return 0;
	}
	if(n==2){
		if(s=="01"){
			cout<<tmp-1<<endl;
		}
		else if(s=="10")
			cout<<tmp<<endl;
		else if(s=="11")
			cout<<tmp<<endl;
		else
			cout<<0<<endl;
		return 0;
	}
	int f=0;
	string t=s.substr(0,3);
	for(int i=0;i<n-3;i+=3){
		if(s.substr(i,3)!=t)
			f=1;
	}
	ll ans=1e10;
	ans*=3LL;
	string end=s.substr(n-2,2);
	if(t=="011"&&!f){
		ans-=2;
	}
	else if(t=="101"&&!f){
		ans-=1;
	}
	else if(t=="110"&&!f){
	}
	else{
		cout<<0<<endl;
		return 0;
	}
	if(end=="01"){
		ans-=2;
	}
	else if(end=="11"){
		ans-=1;
	}
	else{
	}
	ans=(ans-n)/3LL;
	ans++;
	cout<<ans<<endl;
}


#include "bits/stdc++.h"
#include <chrono>
#include <random>

#define INF 1000000007
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(i,a,b) for (int i = a; i < b; i++)

#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,fma,mmx,avx,tune=native")

using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef unordered_map<int,int> umi ;
typedef unordered_set<int> usi ;
typedef pair<int,int> pi;

struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

bool ldequal(ld  a ,ld b){
    return abs(a-b) < 1e-9  ;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// shuffle(permutation.begin(), permutation.end(), rng); for permute
// uniform_int_distribution<int>(0, i)(rng) for rand

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

const int N  =2e5+1 ;

bool tests= false ;
void gen(){} ;

void solve(){
 double a,b ;
 cin>>a>>b ;
 cout<<(a*b)/100 ;
}

int main(){

    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    gen() ;
    long long test =1 ;
    if(tests) cin>>test ;
    REP(i,1,test+1){
        debug(i) ;
        //cout<<"Case #"<<i<<": " ;

        solve() ;
        cout<<"\n" ;
    }
    return 0 ;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
using P = pair<int, int>;

int main(){

    int n; cin>>n;
    vector<P> a(n),b(n);
    rep(i,n) {
        int x,y; cin>>x>>y;
        a[i] = {x,i};
        b[i] = {y,i};
    }
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    int ans = 1e9;
    if (a[0].second == b[0].second) ans = a[0].first + b[0].first;
    else{
        ans = max(a[0].first, b[0].first);
    }
    ans = min({ans, max(a[0].first,b[1].first), max(a[1].first, b[0].first)});

    cout << ans << endl;
    return 0;
}


/*
            Bismillahir Rahmanir Rahim
            Read in the name of Allah, who created you!
            Al Mashruf Tonoy
            Department of CSE, Daffodil Internatinal University.
*/
/*
            Bismillahir Rahmanir Rahim
            Read in the name of Allah, who created you!
            Al Mashruf Tonoy
            Department of CSE, Daffodil Internatinal University.
*/
#include <bits/stdc++.h>
using namespace std;

int d(int n)
{
    if(n==1)
	{
		return 6;
	}
	else if(n==6)
	{
		return 1;
	}
	else if(n==2)
	{
		return 5;
	}
	else if(n==5)
	{
		return 2;
	}
	else if(n==3)
	{
		return 4;
	}
	else if(n==4)
	{
		return 3;
	}
}

int main()
{
	for(int i=0; i<3; i++)
	{
		int a,b,c;
		cin>>a>>b>>c;
		cout<<d(a)+d(b)+d(c);
		return 0;
	}

}

#include<bits/stdc++.h>

using namespace std;

int main()
{
    int arr[7] = {0,6,5,4,3,2,1};
    int a ,b,c;
    cin >> a >> b >> c;
    cout << (arr[a]+arr[b]+arr[c]) << "\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define debug(x)      cerr << "L" << __LINE__ << ": " << #x << " = " << (x) << endl
#define debugvec(v)   rep(i, v.size()) cerr << "L" << __LINE__ << ": " << #v << "[" << i << "] = " << v[i] << endl;
#define debugvec2(v)  cerr << "L" << __LINE__ << ": " << #v << " = { "; rep(i, v.size()) cerr << v[i] << (i + 1 == v.size() ? "" : ", "); cerr << " }" << endl;
typedef long long ll;  /* 10^18 くらいまでいける */

int main(){
  char c1, c2, c3;
  cin >> c1 >> c2 >> c3;
  if (c1 == c2 && c2 == c3)
	cout << "Won" << endl;
  else
	cout << "Lost" << endl;
}

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
typedef long long ll;
const int maxn=1e6+5;

int main(){
    char s[5];
    cin>>s+1;
    if(s[1]==s[2]&&s[3]==s[2]){
        cout<<"Won"<<endl;

    }
    else{
        cout<<"Lost"<<endl;
    }

    return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define forn(i,a,b) for(int i =a;i<b;i++)
#define fi first
#define se second
#define fast ios_base::sync_with_stdio(false);
using namespace std;

//for debugging
/*
g++ -D_GLIBCXX_ASSERTIONS -DDEBUG -ggdb3 -std=c++14
*/
int recur_depth = 0;
#ifdef DEBUG
#define dbg(x) {++recur_depth; auto x_=x; --recur_depth; cerr<<string(recur_depth, '\t')<<"\e[91m"<<__func__<<":"<<__LINE__<<"\t"<<#x<<" = "<<x_<<"\e[39m"<<endl;}
#else
#define dbg(x)
#endif
template<typename Ostream, typename Cont>
typename enable_if<is_same<Ostream,ostream>::value, Ostream&>::type operator<<(Ostream& os,  const Cont& v){
	os<<"[";
	for(auto& x:v){os<<x<<", ";}
	return os<<"]";
}
template<typename Ostream, typename ...Ts>
Ostream& operator<<(Ostream& os,  const pair<Ts...>& p){
	return os<<"{"<<p.first<<", "<<p.second<<"}";
}
// debugging ends here

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

const int mod = 998244353;

int gcd(int a, int b) {
	return (b?gcd(b,a%b):a);
}

int gcdExtended(int a,int b, int *x , int *y) {
	if(a==0) {
		*x = 0;*y=1;
		return b;
	}
	int x1, y1;
	int g = gcdExtended(b%a,a,&x1,&y1);

	*x = y1- (b/a)*x1;
	*y = x1;

	return g;
}

void solve() {
	int n,s,k;
	cin >> n >> s >> k;
	int g = gcd(n,k);
	if(s%g !=0) {
		cout << -1 << endl;
		return;
	}
	s = n-s;
	n/=g,s/=g,k/=g;

	int x,y;
	g = gcdExtended(k,n,&x,&y);
	x = (x+n)%n;
	int ans = (long long )(s)*x%n;
	cout << ans << endl;
}

int main(){
	fast;
	#ifndef ONLINE_JUDGE
		freopen("input.txt","r",stdin);
		freopen("output.txt","w",stdout);
	#endif

	int t;cin >> t;while(t--)
		solve();
}

#pragma GCC optimize("Ofast")
#pragma GCC target("avx")
#include <bits/stdc++.h>
#define ll long long
#define all(x) x.begin(),x.end() //vector
#define rep(i,n) for(ll i=0; i<n; ++i)
#define REP(i,n) for(ll i=1; i<=n; ++i)
#define Rep(i,s,n) for(ll i=s; i<n; ++i)
using namespace std;

//最大公約数
ll gcd (ll a, ll b){
    if (b==0) return 0;
    return gcd(b,a%b);
}
//最小公倍数
ll lcm (ll a, ll b){
    return (ll)a/gcd(a,b)*b;
}
//入力はすべて正とする
//素数判定
bool is_prime (ll n){
    for (ll i=2; i*i<=n; i++){
        if (n%i==0) return false;
    }
    return true;
}

int main(){
  int n;
  cin >> n;
  vector<string> s(n);
  vector<ll> t(n);
  vector<ll> cp(n);
  rep(i,n){
    cin >> s.at(i) >> t.at(i);
    cp.at(i)=t.at(i);
  }
  sort(t.begin(), t.end());
  reverse(t.begin(), t.end());
  rep(i,n){
    if (t.at(1)==cp.at(i)){
      cout << s.at(i) << endl;
      return 0;
    }
  }
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define f first
#define s second
#define pb push_back
int a[300005];
signed main()
{
   int n;
   cin>>n;
   for(int i=0;i<n;i++)
   {
       cin>>a[i];
   }
   sort(a,a+n);
   int cur=0;
   int ans=0;
   for(int i=0;i<n;i++)
   {
       if(!i||a[i]==a[i-1])
       {
           cur++;
       }
       else
       {
           ans+=cur*(n-cur);
           cur=1;
       }
   }
   ans+=cur*(n-cur);
   ans/=2;
   cout<<ans<<endl;
    return 0;
}

/**
 *    author: Ayush
 *    From: Earth
**/
#include <bits/stdc++.h>

using namespace std;
#define ll long long int
#define dbg(x)  cerr << #x << ": "<< x <<endl
#define dbg2(x,y) cerr<< #x <<": "<< x <<" || "<< #y << ": " << y << endl

int main() {

   ios::sync_with_stdio(false);
   cin.tie(0);

   ll n;
   cin >> n;
   ll ans = 0;
   ll a[n];
   for (ll i = 0 ; i < n ; ++i) {
      cin >> a[i];
   }

   map <ll, ll> mp;
   for (ll i = 0 ; i < n ; ++i) {
      ans += (i) - (mp[a[i]]);
      mp[a[i]]++;
   }

   cout << ans;
   return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <set>
#include <queue>
#include <stack>
using namespace std;
typedef long long int ll;
typedef pair<int,int> Pai;

ll gcd(ll a, ll b) { return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) { return a/gcd(a,b)*b;}

void P(int x) {cout << x << endl;}
void P(long x) {cout << x << endl;}
void P(double x) {cout << x << endl;}
void P(ll x) {cout << x << endl;}
void P(string x) {cout << x << endl;}
void P(char x) {cout << x << endl;}

#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define drep(i, n) for(int i = n - 1; i >= 0; i--)

const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;

int main() {
  int k;
  string s, t;
  cin >> k >> s >> t;
  map<int, int> mp;
  for (int i = 1; i <= 9; i++) mp[i] = k;
  rep(i, 4) {
    mp[s[i] - '0']--;
    mp[t[i] - '0']--;
  }

  vector<Pai> couple;
  for (int i = 1; i <= 9; i++) { // takahashi card
    mp[i]--;
    if (mp[i] < 0) {
      mp[i]++;
      continue;
    }

    vector<int> v(10);
    rep (ti, 4) v[s[ti] - '0']++;
    v[i]++;
    ll tScore = 0;
    rep (ti, 10) tScore += ti * pow(10, v[ti]);

    for (int j = 1; j <= 9; j++) { // aoki card
      mp[j]--;
      if (mp[j] < 0) {
        mp[j]++;
        continue;
      }

      vector<int> v(10);
      rep (ti, 4) v[t[ti] - '0']++;
      v[j]++;
      ll aScore = 0;
      rep (ti, 10) aScore += ti * pow(10, v[ti]);

      if (tScore > aScore) {
        couple.push_back(make_pair(i, j));
      }
      mp[j]++;
    }

    mp[i]++;
  }
  // rep (i, couple.size()) {
  //   cout << couple[i].first << " / " << couple[i].second << endl;
  // }
  // return 0;

  double ans = 0;
  ll total = 9 * k - 8;
  rep (i, couple.size()) {
    double one = mp[couple[i].first];
    mp[couple[i].first]--;
    ans += (one * mp[couple[i].second]);
    mp[couple[i].first]++;
  }
  ans = ans / (total * (total - 1));
  printf("%.10f\n", ans);
  return 0;
}


#include<bits/stdc++.h> //Ithea Myse Valgulious
namespace chtholly{
typedef long long ll;
#define re0 register int
#define rel register ll
#define rec register char
#define gc getchar
//#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<23,stdin),p1==p2)?-1:*p1++)
#define pc putchar
#define p32 pc(' ')
#define pl puts("")
/*By Citrus*/
char buf[1<<23],*p1=buf,*p2=buf;
inline int read(){
  int x=0,f=1;char c=gc();
  for (;!isdigit(c);c=gc()) f^=c=='-';
  for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0');
  return f?x:-x;
  }
template <typename mitsuha>
inline bool read(mitsuha &x){
  x=0;int f=1;char c=gc();
  for (;!isdigit(c)&&~c;c=gc()) f^=c=='-';
  if (!~c) return 0;
  for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0');
  return x=f?x:-x,1;
  }
template <typename mitsuha>
inline int write(mitsuha x){
  if (!x) return 0&pc(48);
  if (x<0) pc('-'),x=-x;
  int bit[20],i,p=0;
  for (;x;x/=10) bit[++p]=x%10;
  for (i=p;i;--i) pc(bit[i]+48);
  return 0;
  }
inline char fuhao(){
  char c=gc();
  for (;isspace(c);c=gc());
  return c;
  }
}using namespace chtholly;
using namespace std;
const int yuzu=1e6;
typedef int fuko[yuzu|10];
fuko vis;
int main() {
  ll n,m,i;
  auto kasumi=[&](ll a,ll b,int mod) {
    ll zw=1;
    for (;b;b>>=1,a=a*a%mod) if (b&1) zw=zw*a%mod;
    return zw;
  };
  read(n),read(m);
  printf("%lld\n",kasumi(10,n,m*m)/m%m);
}

#include <cstdio>

int po(int a, long long b, int m)
{
	int r = 1;
	while (b) {
		if (b & 1) r = (long long)r * a % m;
		a = (long long)a * a % m;
		b >>= 1;
	}
	return r;
}

int m;
long long n;

int main()
{
	scanf("%lld%d", &n, &m);
	printf("%d\n", (po(10, n, m * m) - po(10, n, m)) / m);
	return 0;
}


/*
Written By mafailure
*/
#define ill
//In the name of God
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
using namespace std;
using namespace __gnu_pbds;
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl "\n"
#ifdef ill
#define int long long
#endif
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; }
template<typename T, size_t size> ostream& operator<<(ostream &os, const array<T, size> &arr) { os << '{'; string sep; for (const auto &x : arr) os << sep << x, sep = ", "; return os << '}'; }
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }

void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }

#ifdef AATIF_DEBUG
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
//#define int long long
// int dx[]={-1,1,0,0}; int dy[]={0,0,1,-1};
// int dx[]={2,2,-2,-2,1,1,-1,-1}; int dy[]={1,-1,1,-1,2,-2,2,-2};
const long long mod = 1e9 + 7;
const double eps=1e-9;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef pair<int, int> ii;
typedef vector< pair< int, int > > vii;
typedef map<int, int> mii;
typedef pair<int, ii> pip;
typedef pair<ii, int> ppi;
#define arrinp(arr,init,final,size,type) type* arr=new type[size];for(int i=init;i<final;i++)cin>>arr[i];
#define cr2d(arr,n,m,t) t**arr=new t*[n];for(int i=0;i<n;i++)arr[i]=new t[m];
#define w(t) int t;cin>>t; while(t--)
#define takeInp(n) int n;cin>>n;
#define fr(i,init,final) for(int i=init;i<final;i++)
#define frr(i,init,final) for(int i=init;i>=final;i--)
#define Fr(i,final) for(int i=0;i<final;i++)
#define Frr(i,first) for(int i=first;i>=0;i--)
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define debug(x) cerr<<">value ("<<#x<<") : "<<x<<endl;
#define setb __builtin_popcount
#define lsone(n) (n&(-n))
#define rlsone(n) (n&(n-1))
#define clr(a,b) memset(a,b,sizeof(a))
const int inf= INT_MAX;
//struct point_i{int x,y;};
struct point_i{int x,y;
point_i(){ x=0; y=0;}
point_i(int x_,int y_){x=(x_);y=(y_) ;}
};

struct point
{
	float x,y;
	point (){x=y=0.0;}
	point (float x_,float y_){x=(x_); y=(y_);}
	bool operator < (point other) const{
	  if(fabs(x-other.x)>eps)return x<other.x;
	  return y<other.y;

		}
	bool operator == (point other) const {
		return fabs(other.x-x)<=eps&& fabs(other.y-y)<=eps;
		}

} ;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>  ordered_set;
vi init(string s)
{
	istringstream sin(s);
	int n;
	vi arr;
	while(sin>>n)arr.push_back(n);
	return arr;
}
int power(int x, int y)
{
	if(y==0)return 1;
	int u=power(x,y/2);
	u=(u*u)%mod;
	if(y%2)u=(x*u)%mod;
	return u;

}
int gcd(int a,int b)
{
	if(a<b)return gcd(b,a);
	return (b==0?a:(a%b?gcd(b,a%b):b));
}
int gcd_e(int a,int b,int &x,int &y)
{

	if(b==0){x=1; y=0; return a;}
	int x1,y1;
	int p=gcd_e(b,a%b,x1,y1);
	x=y1;
	y=x1-(a/b)*y1;
	return p;
}
void solve()
{
	int n; cin>>n;int t; cin>>t;  vi a(n); fr(i,0,n)cin>>a[i];
	vector<int> v;
	int d1=n/2; int d2=n-d1;
	fr(i,0,(1LL<<d1))
	{
		int s=0;
		fr(j,0,d1+1){if((i>>j)&1)s+=a[d2+j];}
		v.pb(s);
	}
	sort(all(v));
	int ans=0;
	fr(i,0,(1LL<<d2))
	{
		int s=0;
		fr(j,0,d2+1)
		{
			if((i>>j)&1)s+=a[j];
		}
		auto id=upper_bound(all(v),t-s)-v.begin();
		if(id)ans=max(s+v[--id],ans);
	}
	cout<<ans<<endl;
}
signed main()
{
	IOS
	solve();

}

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

typedef long long ll;

int Szukaj(vector<ll>& a, ll x)
{
    int l = 0, p = a.size() - 1, s, r = -1;
    while (l <= p)
    {
        s = (l + p) / 2;
        if (a[s] <= x)
        {
            r = s;
            l = s + 1;
        }
        else
            p = s - 1;
    }
    return r;
}

int main()
{
    ios_base::sync_with_stdio(0);
    int N, T;
    cin >> N >> T;
    vector<ll> t(N);
    for (int i = 0; i < N; ++i)
        cin >> t[i];
    int n = N / 2;
    int m = N - n;
    vector<ll> a, b;
    for (int i = 0; i < N; ++i)
    {
        if (i < n)
            a.push_back(t[i]);
        else
            b.push_back(t[i]);
    }
    vector<ll> sumy;
    for (int mask = 0; mask < (1 << m); ++mask)
    {
        ll s = 0;
        for (int j = 0; j < m; ++j)
            if ((mask & (1 << j)) > 0)
                s += b[j];
        sumy.push_back(s);
    }
    sort(sumy.begin(), sumy.end());
    ll odp = 0;
    for (int mask = 0; mask < (1 << n); ++mask)
    {
        ll s = 0;
        for (int j = 0; j < n; ++j)
            if ((mask & (1 << j)) > 0)
                s += a[j];
        int g = Szukaj(sumy, T - s);
        if (g != -1)
            odp = max(odp, s + sumy[g]);
    }
    cout << odp;
    return 0;
}


#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <limits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <tuple>
#include <stack>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <vector>
#include <type_traits>
/* This header mainly deals while taking input
 */

#include <cstdint>

 // namespace Rangers

#include <ios>

#include <istream>

#include <utility>

namespace Inputs {

inline std::string readString1(std::istream &in) {
  // string input when first taken
  std::string s;
  std::getline(in, s);
  return s;
}

} // namespace Inputs

#include <sstream>
#include <iomanip>
using namespace Inputs;
using namespace std;
// Powered by caide (code generator, tester, and library code inliner)
// @author Dj

class Solution {
public:
  void solve(std::istream &in, std::ostream &out) {
    string s = readString1(in);
    out << (s[0] == s[1] && s[1] == s[2] && s[0] == s[2] ? "Won" : "Lost");
  }
};

void solve(std::istream& in, std::ostream& out)
{
    out << std::setprecision(12);
    Solution solution;
    solution.solve(in, out);
}

#include <fstream>

int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(0);

    istream& in = cin;

    ostream& out = cout;

    solve(in, out);
    return 0;
}

#include<bits/stdc++.h>
using  namespace  std;

#define endl         "\n"
#define ll           long long
#define PI           acos(-1.0)
#define GCD(a,b)     __gcd(a , b)
#define LCM(a,b)     ((a/__gcd(a,b))*b)
#define READ(f)      freopen(f,"r",stdin)
#define WRITE(f)     freopen(f,"w",stdout)
#define test         cout<<"\n*************\n"
#define mem(arr,val) memset(arr,val,sizeof(arr))
#define precise(c)   fixed(cout);cout<<setprecision(c)
#define valid(x,y)   (x>=1 && x<=row && y>=1 && y<=column)
#define fast         ios_base :: sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)

int main(){

    string str;
    cin>>str;

    if(str[0]==str[1] && str[1]==str[2]){
        cout<<"Won";
    } else{
        cout<<"Lost";
    }

        return 0;
}


#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back
#define eb emplace_back
using namespace std;
template <class T = int>
using V = vector<T>;
template <class T = int>
using VV = V<V<T>>;
using ll = long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using pdd = pair<ld,ld>;

const int INF = 1e9;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    VV<int> g(n);
    rep(i,m){
        int a, b;
        cin >> a >> b;
        a--, b--;
        g[a].eb(b);
        g[b].eb(a);
    }
    int k;
    cin >> k;
    V<int> c(k);
    rep(i,k) cin >> c[i], c[i]--;
    VV<int> dist(k, V<int>(n, -1));
    rep(i,k){
        queue<int> que;
        dist[i][c[i]] = 0;
        que.push(c[i]);
        while(!que.empty()){
            int v = que.front();
            que.pop();
            for(int nv : g[v]){
                if(dist[i][nv] != -1) continue;
                dist[i][nv] = dist[i][v] + 1;
                que.push(nv);
            }
        }
    }
    rep(i,k) if(dist[0][c[i]] == -1){
        cout << -1 << endl;
        return 0;
    }
    VV<int> dp(1<<k, V<int>(k, INF));
    rep(i,k) dp[0][i] = 0;
    rep(i,(1<<k)-1){
        rep(j,k){
            rep(l,k){
                if(!(i>>l&1)){
                    dp[i|(1<<l)][l] = min(dp[i|(1<<l)][l], dp[i][j] + dist[j][c[l]]);
                }
            }
        }
    }
    int ans = INF;
    rep(i,k) ans = min(ans, dp[(1<<k)-1][i]+1);
    cout << ans << endl;
}

        #include<bits/stdc++.h>
#define loop(i, n) for(int i = 0; i < n; i++)
#define pb          push_back
#define ll          long long int
#define vi          vector<int>
#define pi          pair<int, int>
#define W(t)        int t; cin >> t; while(t--)
#define FIO         ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ff          first
#define ss          second
#define rep(i, a, b)for(ll i = a; i <= b; i++)
#define dep(i, a, b)for(ll i = a; i >= b; i--)
#define all(x)      x.begin(), x.end()
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI                3.14159265
using namespace std;
const ll mod = (ll)(998244353);
ll test = 1;
vector<ll> read(int n){

    vector<ll> v(n);

    loop(i, n)
        cin >> v[i];
    return v;
}
std::vector<ll> v;
ll OR(ll i, ll j){
    ll left = v[i];

    if(i+1 <= j-1){
        rep(k, i+1, j-1){

            left = left|v[k];
        }
    }

    return left;
}
ll calculate(std::vector<ll> cnt){

    std::vector<ll> k;

    rep(i, 0, cnt.size()-1){
        if(cnt[i] == 1)k.pb(i);
    }
    ll ans = OR(k[0], k[1]);
    rep(i, 1, k.size()-2){

        ans ^= OR(k[i], k[i+1]);
    }

    return ans;
}
void solve(){
    ll i, j, k, m,n, cnt = 0, ans = 0, sum = 0;

    cin >> n;

    v.resize(n);

    for(auto &it : v)cin >> it;

    ans = INT_MAX;
    for(ll i = 0; i < 1LL<<(n-1); i++){

        ll t = i;

        std::vector<ll> cnt(n+1, 0);

        cnt[n] = 1;

        cnt[0] = 1;

        rep(j, 0, n-2){
            if(t&1LL){
                cnt[j+1] = 1;
            }
            else{
                cnt[j+1] = 0;
            }
            t/=2;
        }
        ans = min(ans, calculate(cnt));
    }

    cout <<ans << "\n";

    test++;
}

int main(){
    FIO

    #ifndef ONLINE_JUDGE
    freopen("C:/Codeforces/input.txt", "r", stdin);
    freopen("C:/Codeforces/output.txt", "w", stdout);
    #endif
    ll i = 1;
    //W(t)
    solve();

    return 0;

}


// atcoder/hhkb2020/D/main.cpp
// author: @___Johniel
// github: https://github.com/johniel/

#include <bits/stdc++.h>

#define each(i, c) for (auto& i : c)
#define unless(cond) if (!(cond))

using namespace std;

template<typename P, typename Q> ostream& operator << (ostream& os, pair<P, Q> p) { os << "(" << p.first << "," << p.second << ")"; return os; }
template<typename P, typename Q> istream& operator >> (istream& is, pair<P, Q>& p) { is >> p.first >> p.second; return is; }
template<typename T> ostream& operator << (ostream& os, vector<T> v) { os << "("; for (auto& i: v) os << i << ","; os << ")"; return os; }
template<typename T> istream& operator >> (istream& is, vector<T>& v) { for (auto& i: v) is >> i; return is; }
template<typename T> ostream& operator << (ostream& os, set<T> s) { os << "#{"; for (auto& i: s) os << i << ","; os << "}"; return os; }
template<typename K, typename V> ostream& operator << (ostream& os, map<K, V> m) { os << "{"; for (auto& i: m) os << i << ","; os << "}"; return os; }

template<typename T> inline T setmax(T& a, T b) { return a = std::max(a, b); }
template<typename T> inline T setmin(T& a, T b) { return a = std::min(a, b); }

using lli = long long int;
using ull = unsigned long long;
using point = complex<double>;
using str = string;
template<typename T> using vec = vector<T>;

constexpr array<int, 8> di({0, 1, -1, 0, 1, -1, 1, -1});
constexpr array<int, 8> dj({1, 0, 0, -1, 1, -1, -1, 1});
constexpr lli mod = 1e9 + 7;

// from: https://qiita.com/drken/items/3b4fdf0a78e7a138cd9a
template<int MOD> struct Fp {
  long long val;
  constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
    if (val < 0) val += MOD;
  }
  constexpr int getmod() { return MOD; }
  constexpr Fp operator - () const noexcept {
    return val ? MOD - val : 0;
  }
  constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; }
  constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; }
  constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; }
  constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; }
  constexpr Fp& operator += (const Fp& r) noexcept {
    val += r.val;
    if (val >= MOD) val -= MOD;
    return *this;
  }
  constexpr Fp& operator -= (const Fp& r) noexcept {
    val -= r.val;
    if (val < 0) val += MOD;
    return *this;
  }
  constexpr Fp& operator *= (const Fp& r) noexcept {
    val = val * r.val % MOD;
    return *this;
  }
  constexpr Fp& operator /= (const Fp& r) noexcept {
    long long a = r.val, b = MOD, u = 1, v = 0;
    while (b) {
      long long t = a / b;
      a -= t * b; swap(a, b);
      u -= t * v; swap(u, v);
    }
    val = val * u % MOD;
    if (val < 0) val += MOD;
    return *this;
  }
  constexpr bool operator == (const Fp& r) const noexcept {
    return this->val == r.val;
  }
  constexpr bool operator != (const Fp& r) const noexcept {
    return this->val != r.val;
  }
  friend constexpr ostream& operator << (ostream &os, const Fp<MOD>& x) noexcept {
    return os << x.val;
  }
  friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
    if (n == 0) return 1;
    auto t = modpow(a, n / 2);
    t = t * t;
    if (n & 1) t = t * a;
    return t;
  }
};
// constexpr lli mod = 1e9 + 7;
using mint = Fp<mod>;

int main(int argc, char *argv[])
{
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.setf(ios_base::fixed);
  cout.precision(15);

  int _;
  cin >> _;

  lli _n, _a, _b;
  while (cin >> _n >> _a >> _b) {
    if (_n - _a - _b < 0) {
      cout << 0 << endl;
      continue;
    }

    mint n(_n);
    mint a(_a);
    mint b(_b);

    mint x = (n - a + 1) * (n - b + 1);
    mint y = n - a - b + 1;
    mint z = (y + 1) * y / 2;

    cout << (x * z * 4) - (z * z * 4) << endl;
  }

  return 0;
}

#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>
#include <istream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <climits>

using namespace std;

int max(int, int);
int min(int, int);
int positiveNum(int);
unsigned long long factorial(int);
unsigned long long combination(int, int);
long long power(int, int);
bool between(int, int, int);
vector<string> split(string, char);
int digit_sum(int);
vector<long long> divisors(long long);
//cout << fixed << setprecision(15) << y << endl;

int main()
{
  long long n;
  cin >> n;

  vector<long long> div = divisors(n*2);

  long long count = 0;
  for(long long i = 0; div[i] * div[i] <= n*2; i++)
  {
      if((n*2/div[i] - div[i]) % 2 == 1)
      {
        count++;
      }
  }

  cout << count*2 << endl;

  return 0;
}

//2つの整数のうち大きい方を返す
int max(int a, int b)
{
  if(a > b)
  {
    return a;
  }
  else
  {
    return b;
  }
}

//2つの整数のうち小さい方を返す
int min(int a, int b)
{
  if(a < b)
  {
    return a;
  }
  else
  {
    return b;
  }
}

//入力が正ならその値，負なら0を返す
int positiveNum(int x)
{
  if(x >= 0)
  {
    return x;
  }
  else
  {
    return 0;
  }
}

//階乗
unsigned long long factorial(int n)
{
  unsigned long long ans = 1;

  for(int i = 1; i <= n; i++)
  {
    ans *= i;
  }

  return ans;
}

//mの中からn取る組み合わせの数
unsigned long long combination(int m, int n)
{
  if(m == n)
  {
    return 1;
  }
  else if(n == 0)
  {
    return 1;
  }
  else
  {
    return combination(m-1, n-1) + combination(m-1, n);
  }

}

//cがaとbの間にあるか
bool between(int a, int b, int c)
{
  if(a > b)
  {
    int swap;
    swap = a;
    a = b;
    b = swap;
  }
  if(a <= c && c <= b)
  {
    return true;
  }
  else
  {
    return false;
  }
}

//累乗(long long版)
long long power(int x, int y)
{
  long long num = 1;

  for(int i = 0; i < y; i++)
  {
    num *= x;
  }

  return num;
}

//カンマ等で区切られた文字列を要素で分ける
vector<string> split(string s, char c)
{
  vector<string> v;

  stringstream ss{s};
  string buf;

  while(getline(ss, buf, c))
  {
    v.push_back(buf);
  }

  return v;

}

//各桁の和を求める
int digit_sum(int x)
{
  int sum = 0;

  while(x > 0)
  {
    sum += x % 10;
    x /= 10;
  }

  return sum;
}

//Nの約数を求める
vector<long long> divisors(long long N)
{
  vector<long long> a;

  for (long long i = 1; i * i <= N; i++)
  {
    if (N % i == 0)
    {
      a.push_back(i);
      if (N/i != i)
      {
        a.push_back(N/i);
      }
    }
  }

  sort(a.begin(), a.end());

  return a;
}


#include <bits/stdc++.h>
#define fir first
#define sec second
#define PB push_back
#define MP make_pair

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep1(i, n) for (int i = 1; i < (int)(n); ++i)
#define foreach(itr, c) for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); ++itr)

template <class T> void chkmax(T& x, T y) { x = (x > y ? x : y); }
template <class T> void chkmin(T& x, T y) { x = (x < y ? x : y); }

typedef long long LL;
typedef pair <int, int> pii;
typedef vector <int> vi;

const int N = 105;
const int M = 1e4 + 5;

int n, m, a[M], b[M], c[N];
vi e[N];
set <int> e2[N];
bool vis[N];

void dfs(int now, int from){
    vis[now] = 1;
    for (auto u : e[now]) if (u != from && c[u] == c[now]){
        if (e2[u].find(now) != e2[u].end()) continue;
        e2[now].insert(u);
        if (!vis[u]) dfs(u, now);
    }
}

int main(){
    scanf("%d%d", &n, &m);
    rep(i, m){
        scanf("%d%d", &a[i], &b[i]), a[i]--, b[i]--;
        e[a[i]].PB(b[i]);
        e[b[i]].PB(a[i]);
    }
    rep(i, n) scanf("%d", &c[i]);
    rep(i, m){
        if (c[a[i]] > c[b[i]]) e2[a[i]].insert(b[i]);
        else if (c[a[i]] < c[b[i]]) e2[b[i]].insert(a[i]);
        else {
            if (e2[a[i]].find(b[i]) != e2[a[i]].end() || e2[b[i]].find(a[i]) != e2[b[i]].end())
                continue;
            dfs(b[i], a[i]);
        }
    }

    rep(i, m){
        if (e2[a[i]].find(b[i]) != e2[a[i]].end()) printf("->\n");
        else printf("<-\n");
    }
    return 0;
}

#include<bits/stdc++.h>
#define it register int
#define ct const int
#define il inline
using namespace std;
typedef long long ll;
#define rll register ll
#define cll const ll
typedef double db;
#define rdb register db
#define cdb const db
typedef long double ldb;
typedef unsigned long long ull;
#define pb push_back
#define mkp make_pair
#define pl pair<ll,int>
#define pi pair<int,int>
#define fir first
#define sec second
namespace io{
	il char nc(){
		static char buf[100000],*p1=buf,*p2=buf;
		return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
	}
	template <class I>
	il void fr(I &num){
		num=0;register char c=nc();it p=1;
		while(c<'0'||c>'9') c=='-'?p=-1,c=nc():c=nc();
		while(c>='0'&&c<='9') num=num*10+c-'0',c=nc();
		num*=p;
	}
	char obuf[1000001],*os=obuf, *ot=obuf+1000001;
	il void flush(){fwrite(obuf,1,os-obuf,stdout),os=obuf;}
	il void ptc(const char x){*os++=x,os==ot?flush():void();}
	template<class I>
	il void wr(I x){
		x<0?ptc('-'),x=-x:0;
		static char qu[65];
		char *tmp=qu;
		do *tmp++=(x%10)^'0';while(x/=10);
		while(tmp--!=qu) ptc(*tmp);
	}
	struct flusher{ ~flusher() { flush(); } }_;
	template <class I>
	il I Max(I p,I q){return p>q?p:q;}
	template <class I>
	il I Min(I p,I q){return p<q?p:q;}
	template <class I>
	il I A(I x){return x<0?-x:x;}
	template <class I>
	il void sp(I&p,I&q){I x=p;p=q,q=x;}
	template <class I>
	il void ckMax(I&p,I q){p=(p>q?p:q);}
	template <class I>
	il void ckMin(I&p,I q){p=(p<q?p:q);}
};
using io :: fr;
using io :: nc;
using io :: wr;
using io :: ptc;
using io :: Max;
using io :: Min;
using io :: A;
using io :: sp;
using io :: ckMax;
using io :: ckMin;
const int N=1000005;
int h[N],nxt[N],adj[N],x[N],y[N],t=1,u,v,dfn[N],a[N],fa[N],tag[N],ans[N],ti,n,m,d[N],s[N],top,ins[N],id[N];
il void add(ct u,ct v,ct i){nxt[++t]=h[u],h[u]=t,adj[t]=v,id[t]=i,nxt[++t]=h[v],h[v]=t,adj[t]=u,id[t]=i;}
il void dfs(ct x){
	dfn[x]=++ti;//s[++top]=x,d[x]=top,ins[x]=1;
	//printf("x=%d\n",x);
	for(it i=h[x],j;i;i=nxt[i])
		if((j=adj[i])^fa[x]){
			if(dfn[j]) tag[id[i]]=x;
			if(!dfn[j]) tag[id[i]]=j,fa[j]=x,dfs(j);
		//	printf("x=%d x[i]=%d y[i]=%d tag[i]=%d\n",x,::x[i>>1],y[i>>1],tag[i>>1]);
		//	if(tag[i>>1]==1&&(!ans[i>>1])) (::x[i>>1]==x)?ans[i>>1]=1:ans[i>>1]=-1;
		//	if(tag[i>>1]==2&&(!ans[i>>1])) (::x[i>>1]==x)?ans[i>>1]=1:ans[i>>1]=-1;
		}
	//--top,ins[x]=0;
}
int main(){
	scanf("%d%d",&n,&m);it i;
	for(i=1;i<=m;++i) scanf("%d%d",&x[i],&y[i]);
	for(i=1;i<=n;++i) scanf("%d",&a[i]);
	for(i=1;i<=m;++i) if(a[x[i]]==a[y[i]]) add(x[i],y[i],i);
	for(i=1;i<=n;++i) if(!dfn[i]) ti=0,dfs(i);
	for(i=1;i<=m;++i){
		if(a[x[i]]>a[y[i]]){puts("->");continue;}
		if(a[x[i]]<a[y[i]]){puts("<-");continue;}
	//	printf("tag[i]=%d x[i]=%d y[i]=%d %d %d\n",tag[i],x[i],y[i],d[x[i]],d[y[i]]);
		//if(tag[i]==1) (dfn[x[i]]<dfn[y[i]])?puts("->"):puts("<-");
		//if(tag[i]==2) (dfn[x[i]]>dfn[y[i]])?puts("->"):puts("<-");
		if(tag[i]==x[i]) puts("<-");
		else puts("->");
		//if(ans[i]>0) puts("->");
		//else puts("<-");
	}
	return 0;
}


// (n+1,r) = (n,r)+(n,r-1)

#include<bits/stdc++.h>
using namespace std;

#define ll long long

int main(){
	ll l;
	cin >> l;
	ll dp[l][12];
	for(int i = 0;i <= l-1;i++){
		for(int j = 0;j <= 11;j++){
			if(j == 0 || i == j){
				dp[i][j] = 1LL;
				continue;
			}
			if(i < j){
				dp[i][j] = 0LL;
				continue;
			}
			dp[i][j] = dp[i-1][j]+((j)?(dp[i-1][j-1]):0);
		}
	}
	cout << dp[l-1][11];
}

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	ll l;
	cin>>l;
	__int128 a=1,b=1,c=1;
	for(int i=1;i<=11;i++){
		a*=(l-i);
	}
	for(int i=1;i<=11;i++){
		b*=i;
	}
	c=a/b;
	cout<<ll(c)<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define int long long
#define rep(i,n) for(int i=0;i<n;i++)

int N, Q;
int A[200010], T[200010];

int unit(int x)
{
    rep(i, N)
    {
        if(T[i] == 1)x = x + A[i];
        if(T[i] == 2)x = max(x, A[i]);
        if(T[i] == 3)x = min(x, A[i]);
    }
    return x;
}

signed main()
{
    cin >> N;
    rep(i, N)cin >> A[i] >> T[i];

    int LL = -1e15, RR = 1e15;
    LL = unit(LL);
    RR = unit(RR);

    int ll, rr;
    int lv, rv;
    {
        int l = -1e9 - 10, r = 1e9 + 10;
        rep(_, 60)
        {
            int mid = (l + r) / 2;
            int mid_val = mid_val = unit(mid);
            if(LL == mid_val)l = mid;
            else r = mid;
        }
        ll = l;
        lv = unit(ll);

        l = -1e9 - 10, r = 1e9 + 10;
        rep(_, 60)
        {
            int mid = (l + r) / 2;
            int mid_val = unit(mid);
            if(mid_val == RR)r = mid;
            else l = mid;
        }
        rr = r;
        rv = unit(rr);
    }

    cin >> Q;
    rep(i, Q)
    {
        int x;
        cin >> x;
        if(x <= ll)cout << lv << endl;
        else if(rr <= x)cout << rv << endl;
        else cout << lv + (x - ll) << endl;
    }

    return 0;
}

// Bismillahir Rahmanir Rahim
//============================
#include<bits/stdc++.h>
using namespace std;

#define ll          long long
#define endl        "\n"
#define infL        LLONG_MAX
#define infI        INT_MAX
#define pb          push_back
#define fo(i,a,b)   for(int i=a;i<b;i++)
#define fo2(i,a,b)  for(int i=a;i<=b;i++)
#define rfo(i,a,b)  for(int i=a; i>=b;i--)
#define precise(x,y)cout<<fixed<<setprecision(y)<<x<<endl;
#define MAX         100010
#define yes         cout<<"Yes"<<endl
#define no          cout<<"No"<<endl
#define bye         return 0
#define debug       cout<<"\nDebug\n";
#define debug2      cout<<"\nDebug2\n";
#define PEREGRINE_FALCON {ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);}

typedef pair<ll,ll>PII;
double epsilon = 0.0000001f;
ll mod=1e9+7;
double Pi =acos(-1);

void print(vector<ll>V)
{
      cout<<"\n---------------Vector checking start-------------------\n";
      ll sz=V.size();
      fo(i,0,sz){cout<<V[i]<<"  ";}
      cout<<"\n----------------Vector checking end--------------------\n";
}

void print(ll arr[], ll n)
{
      cout<<"\n---------------Array checking start-------------------\n";
      fo(i,0,n){cout<<arr[i]<<" ";}
      cout<<"\n----------------Array checking end--------------------\n";
}

int main()
{
      PEREGRINE_FALCON
      //freopen("inputNew.txt", "r", stdin);

      ll testCase=1;
      //cin>>testCase;
      while(testCase--){
            ll n,m,k,x,y,z,a,b,c,d,mn,mx,temp,cnt,ans,sum,sz,len;

            ll arr[3];
            fo(i,0,3){cin>>arr[i];}
            sort(arr,arr+3);
            if(arr[2]-arr[1] == arr[1]-arr[0]) yes;
            else no;
      }

      bye;
}


#pragma GCC optimize("O3")
#pragma GCC target("sse4")

#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <typeinfo>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;

template <class T>
using Tree =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i, a, b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--)

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define shandom_ruffle random_shuffle

const int MOD = 1000000007;
const ll INF = 1e18;
const int MX = 100001;  // check the limits, dummy

typedef long long ll;
const int inf = INT_MAX / 2;
const ll infl = 1LL << 60;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

template <class T>
void print(const vector<T> v) {
  cout << "[ ";
  F0R(i, v.size()) { cout << v[i] << ' '; }
  cout << "]" << '\n';
}

int mod(int i, int j) {
  return (i % j) < 0 ? (i % j) + 0 + (j < 0 ? -j : j) : (i % j + 0);
}

// --------------------------------------------------------
// This is tool
// --------------------------------------------------------
void Main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  ll S, P, ans = 0;
  cin >> S >> P;
  // cout << sqrt(P);

  for (ll i = 1; i < (sqrt(P)) + 1; i++) {
    if (P % i == 0) {
      if (i + P / i == S) {
        ans++;
      }
    }
  }
  // cout << ans << endl;
  if (ans != 0) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  Main();
}

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
typedef long long ll;
int main() {
	ll s, p;
	cin >> s >> p;
	for (ll i = 0; i <= sqrt(p); ++i) {
		if (i * (s - i) == p) {
			cout << "Yes";
			return 0;
		}
	}
	cout << "No";
}


#include<bits/stdc++.h>
#define PB emplace_back
#define MP make_pair
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
template<typename T>
void read(T &x){
	int ch = getchar(); x = 0; bool f = false;
	for(;ch < '0' || ch > '9';ch = getchar());
	for(;ch >= '0' && ch <= '9';ch = getchar()) x = x * 10 + ch - '0';
	if(f) x = -x;
}
template<typename T>
bool chmax(T &a, const T &b){if(a < b) return a = b, 1; return 0;}
template<typename T>
bool chmin(T &a, const T &b){if(a > b) return a = b, 1; return 0;}
int n, lim;
int main(){
	read(n); lim = 1<<n; printf("%d\n", lim-1);
	for(int k = 1;k < lim;++ k){
		for(int i = 0;i < lim;++ i)
			putchar((__builtin_popcount(i&k) & 1) + 'A');
		putchar('\n');
	}
}

#include <iostream>
#include <vector>

using namespace std;
vector<vector<int>> solve(int n){
    if(n==1){
        vector<vector<int>> v;
        v.resize(1);
        v[0].push_back(0); v[0].push_back(1);
        return v;
    }
    vector<vector<int>> u = solve(n - 1);
    vector<vector<int>> v;
    int x = (1<<n) - 1;
    v.resize(x);
    int s = 0;
    for(int i=0;i<u.size();i++){
        for(int k=0;k<2;k++){
            for(int j=0;j<u[i].size();j++){
                v[s].push_back(u[i][j]);
            }
            for(int j=0;j<u[i].size();j++){
                v[s].push_back(u[i][j]^k);
            }
            s++;
        }
    }
    for(int i=0;i<(1<<n);i++){
        if(i<(1<<(n - 1))) v[s].push_back(0);
        else v[s].push_back(1);
    }
    return v;
}

int main(){
    int i,n; cin >> n;
    vector<vector<int>> v = solve(n);
    cout << (1<<n) - 1 << endl;
    for(i=0;i<(1<<n) - 1;i++){
        for(int j: v[i]){
            if(j==0) cout << "A";
            else cout << "B";
        }
        cout << endl;
    }
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <complex>
#include <queue>
#include <set>
#include <unordered_set>
#include <list>
#include <chrono>
#include <random>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <stack>
#include <iomanip>
#include <fstream>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef vector<ll> vll;
typedef vector<int> vi;
ll MOD = 998244353;
double eps = 1e-12;
#define forn(i, e) for (ll i = 0; i < e; i++)
#define forsn(i, s, e) for (ll i = s; i < e; i++)
#define rforn(i, s) for (ll i = s; i >= 0; i--)
#define rforsn(i, s, e) for (ll i = s; i >= e; i--)
#define ln "\n"
#define dbg(x) cout << #x << " = " << x << ln
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define INF 2e18
#define fast_cin()                    \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL)
#define all(x) (x).begin(), (x).end()
#define allrev(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())

void solve()
{
    int n;
    cin >> n;

    vi arr(n), b(n);
    forn(i, n)
    {
        cin >> arr[i];
    }
    forn(i, n)
    {
        cin >> b[i];
    }

    map<int, int> m;

    forn(i, n)
    {
        int x;
        cin >> x;
        --x;
        int val = b[x];
        m[val]++;
    }

    ll cnt = 0;

    forn(i, n)
    {
        if (m.find(arr[i]) != m.end())
        {
            cnt += m[arr[i]];
        }
    }

    cout << cnt << ln;
}
int main()
{
    fast_cin();
    ll t = 1;
    // cin >> t;
    for (int it = 0; it < t; it++)
    {
        // cout << "Case #" << it + 1 << ": ";
        solve();
    }
    return 0;
}

#include <iostream>
#include <map>

using namespace std;

const int MAX = 1e5;

int main() {
  int N; cin >> N;
  map<int, long long> a, bc;
  int B[MAX], C[MAX];
  for (int i = 0; i < N; i++) {
    int A; cin >> A;
    a[A]++;
  }
  for (int i = 0; i < N; i++) {
    cin >> B[i];
  }

  for (int i = 0; i < N; i++) {
    int C; cin >> C;
    bc[B[--C]]++;
  }

  long long ans = 0;
  for (int i = 1; i <= N; i++) {
    ans += a[i] * bc[i];
  }
  cout << ans << endl;
}


#include<stdio.h>
#include<iostream>
#include<vector>
#include<math.h>
#include<queue>
#include<map>
#include<algorithm>
#include<string.h>
#include<functional>
#include<limits.h>
#include<stdlib.h>
#include<string>
#include<unordered_map>
#include <iomanip>
using namespace std;

#define intmax INT_MAX
#define lmax LONG_MAX
#define uintmax UINT_MAX
#define ulmax ULONG_MAX
#define llmax LLONG_MAX
#define ll long long
#define rep(i,a,N) for((i)=(a);(i)<(N);(i)++)
#define rrp(i,N,a) for((i)=(N)-1;(i)>=(a);(i)--)
#define llfor ll i,j,k
#define sc(a) cin>>a
#define pr(a) cout<<a<<endl
#define pY puts("YES")
#define pN puts("NO")
#define py puts("Yes")
#define pn puts("No")
#define pnn printf("\n")
#define sort(a) sort(a.begin(),a.end())
#define reverse(a) reverse(a.begin(),a.end())
#define push(a,b) (a).push_back(b)
#define llvec vector<vector<ll>>
#define charvec vector<vector<char>>
#define sizeoof(a,b) (a,vector<ll>(b))
#define llpvec vector<pair<ll,ll>>
/*繰り上げ除算*/ll cei(ll x,ll y){ll ans=x/y;if(x%y!=0)ans++;return ans;}
/*最大公約数*/ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
/*最小公倍数*/ll lcm(ll x,ll y){return x/gcd(x,y)*y;}
/*n乗*/ll llpow(ll x,ll n){ll i,ans=1;rep(i,0,n)ans*=x;return ans;}
/*階乗*/ll fact(ll x){ll i,ans=1;rep(i,0,x)ans*=(x-i);return ans;}
/*nCr*/ll ncr(ll n,ll r){return fact(n)/fact(r)/fact(n-r);}
/*nPr*/ll npr(ll n,ll r){return fact(n)/fact(n-r);}
/*primejudge*/bool prime(ll a){if(a<=1)return false;ll i;for(i=2;i*i<=a;i++){if(a%i==0)return false;}return true;}
ll ans=0;llfor;///////////////////////////////////////////////////////////
int main(){
  int K;
  ll N,M,count=0;
  cin>>K>>N>>M;
  vector<ll> A(K+10);
  //vector<ll> B(K+10);
  vector<ll> C(K+10);
  vector<ll> D(K+10);
  vector<ll> E(K+10);
  for(int i=0;i<K;i++){
    int X;
    cin>>X;
    A[i]=M*X;
    C[i]=A[i]/N*N;
    count+=C[i]/N;
  }
  count=M-count;
  for(int i=0;i<K;i++){
    D[i]=abs(A[i]-C[i]);
    E[i]=D[i];
  }
  sort(D);
  reverse(D);
  for(int j=0;j<count;j++){
    for(int i=0;i<K;i++){
      if(D[j]==E[i]){
        C[i]+=N;
        E[i]=abs(E[i]-N);
        break;
      }
    }
  }
  for(int i=0;i<K;i++){
    cout<<C[i]/N<<" ";
  }
return 0;}

#include <bits/stdc++.h>
using namespace std;
template <typename T> void read(T &t) {
	t=0; char ch=getchar(); int f=1;
	while (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); }
	do { (t*=10)+=ch-'0'; ch=getchar(); } while ('0'<=ch&&ch<='9'); t*=f;
}
typedef long long ll;
const ll INF=1e18;
const int maxn=(1e5)+10;
int K;
ll n,m,a[maxn],L[maxn],R[maxn];
ll UP(ll x,ll y) {
	if (x%y==0) return x/y;
	return x/y+1;
}
ll suf[maxn];
bool solve(ll mid,bool flag) {
	ll s1=0,s2=0;
	for (int i=1;i<=K;i++) {
		L[i]=UP(max(0LL,m*a[i]-mid),n);
		R[i]=(mid+m*a[i])/n;
		if (L[i]>R[i]) return 0;
		s1+=L[i],s2+=R[i];
	}
	if (!flag) return s1<=m&&m<=s2;
	for (int i=K;i>=1;i--) {
		suf[i]=suf[i+1]+R[i];
		suf[i]=min(suf[i],INF);
	}
	ll sum=0,tmp;
	for (int i=1;i<=K;i++) {
		if (sum+suf[i+1]+L[i]>=m) tmp=L[i];
		else tmp=m-(sum+suf[i+1]);
		printf("%lld ",tmp);
		sum+=tmp;
	}
	puts("");
	return 1;
}
int main() {
//	freopen("1.txt","r",stdin);
	read(K);
	read(n),read(m);
	for (int i=1;i<=K;i++) read(a[i]);
	ll l=0,r=INF,res,mid;
	while (l<=r) {
		mid=(l+r)>>1;
		if (solve(mid,0)) res=mid,r=mid-1;
		else l=mid+1;
	}
	solve(res,1);

	return 0;
}
/*
  REMEMBER:
  1. Think TWICE, Code ONCE!
  Are there any counterexamples to your algo?

  2. Be careful about the BOUNDARIES!
  N=1? P=1? Something about 0?

  3. Do not make STUPID MISTAKES!
  Array size? Integer overflow? Time complexity? Memory usage? Precision error?
*/


// Problem: D - Kth Excluded
// Contest: AtCoder - AtCoder Beginner Contest 205
// URL: https://atcoder.jp/contests/abc205/tasks/abc205_d
// Memory Limit: 1024 MB
// Time Limit: 3000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll power(ll x,ll y,ll m){if(y==0)return 1;ll p=power(x,y/2,m)%m;p=(p*p)%m;return (y%2==0)?p:(x*p)%m;}
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());int get_rand(int l, int r){uniform_int_distribution<int> uid(l,r);return uid(rng);}
vector<ll> Fac(1,1);int Last_Index = 0;
ll nCr(ll n,ll r,ll m){if(r>n) return 0;if(n>Last_Index){for(ll i=Last_Index+1;i<=n;++i){Fac.push_back(Fac[i-1]*i);Fac[i]%=m;}Last_Index=n;}return (((Fac[n]*power(Fac[r],m-2,m))%m)*power(Fac[n-r],m-2,m))%m;}ll gcd(ll a, ll b) {return (b ? gcd(b, a % b) : a);}
#define Test int t;cin>>t;while(t--)
#define endl '\n'
int Google_Test;
#define int ll
void solve()
{
	// cout << "Case #" << ++Google_Test << ": ";
	int n,q;
	cin >> n >> q;
	vector<int> v(n);
	for(int i=0;i<n;++i)
	cin >> v[i];
	map<int,int> mp;
	sort(v.begin(),v.end());
	int last = 0;
	for(int i=0;i<n;++i)
	{
		mp[v[i]]=last+1;
		last++;
	}
	while(q-->0)
	{
		int k;
		cin >> k;
		auto it = mp.lower_bound(k+1);
		if(it==mp.begin())
		{
			cout << k << endl;
			continue;
		}
		--it;
		k+= (*it).second;
		int temp = (*it).second;
		auto it2 = mp.lower_bound(k+1);
		--it2;
		while(it != it2)
		{
			it = it2;
			k += (*it).second - temp;
			temp = (*it).second;
			it2 = mp.lower_bound(k+1);
			--it2;
		}
		cout << k << endl;
	}
}
int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	//Test
	solve();
	return 0;
}

/*
ID: tazhiba1
TASK: test
LANG: C++
*/
// author: IskhakKutBilim
#include <bits/stdc++.h>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

long long Sumofn(long long x){
	long long sum = 0;
	while(x > 0){
		sum+= x % 10;
		x = x / 10;
	}
	return sum;
}
long long gcd(long long a, long long b){
	while(b){
    	a %= b;
    	swap(a,b);
	}
	return a;
}

long long isprime(long long n){
        if( n < 2 )
            return false;
         int res = n / 2;
     while (n % res )
     res--;
    if( res > 1)
    return false;
    else
    return true;

}
long long lcm(long long a, long long b){
	return a / gcd(a,b)*b;
}
int f(int n){
	int i = 1;
	long long fc = 0;
	while(i <= n){
		fc = fc * i;
		i++;
	}
	return fc;
}

int main(int argv, char** argc){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	int n, m;
	cin >> n >> m;
	int a[n];
	int b[m];
	set<int> s;
	vector<int> res;
	vector<int> v;
	for(int i = 0;i < n; i++){
		cin >> a[i];
		v.push_back(a[i]);
		s.insert(a[i]);
	}
	for(int i = 0;i < m; i++){
		cin >> b[i];
		s.insert(b[i]);
		v.push_back(b[i]);
	}

	int cnt = 0;
	for(set<int>::iterator it = s.begin(); it != s.end(); it++){
		for(int i = 0;i < v.size(); i++){
			if(*it == v[i]){
				cnt++;
			}
		}
		if(cnt < 2){
				res.push_back(*it);
			}
		cnt = 0;
	}
	if(!v.empty())
	for(int i = 0;i < res.size(); i++) cout << res[i] << " ";

	return 0;
}


#include<bits/stdc++.h>
#define lcm(a,b) (a/__gcd(a,b))*b
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define pb push_back
#define F first
#define S second
#define mp make_pair
//salida rapida "\n"
//DECIMALES fixed<<sp(n)<<x<<endl;
//gcd(a,b)= ax + by
//lCB x&-x
using namespace std;
int P[505];
vector<pair<double,double> >v;
int n;
double y,x;
void init(){
    for(int i=0;i<505;i++){
        P[i]=i;
    }
}
int _find(int x){
    if(P[x]==x){
        return x;
    }
    return P[x]=_find(P[x]);
}
void _union(int x, int y){
    x=_find(x);
    y=_find(y);
    P[x]=y;
}
bool f(double r){
    for(int i=0;i<n;i++){
        for(int l=i+1;l<n;l++){
            if(sqrt(abs(v[i].F-v[l].F)*abs(v[i].F-v[l].F)+abs(v[i].S-v[l].S)*abs(v[i].S-v[l].S))<r){
                _union(i,l);
            }
        }
    }
    for(int i=0;i<n;i++){
        if(100-v[i].S<r){
            //cout<<"hrera"<<endl;
           // cout<<100<<" - "<<v[i].S<<"  "<<r<<endl;
            _union(i,500);
        }
        if(v[i].S+100<r){
            _union(i,501);
        }
    }
    if(_find(500)==_find(501)){
        return false;
    }
    return true;
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>x>>y;
        v.pb({x,y});
    }
    double b=0.000001,e=100;
    double res;
    while(e-b>0.00000001){
        init();
        double mid=(b+e)/2;
        if(f(mid*2)){
            res=mid;
            b=mid+0.00000001;
        }else{
            e=mid-0.00000001;
        }
    }
    cout<<res<<endl;

    return 0;
}

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(int)(n); i++)

using namespace std;
using LL = long long;
using P = pair<int,int>;

class UnionFind {
public:
    vector <LL> par;
    vector <LL> siz;

    UnionFind(LL sz_): par(sz_), siz(sz_, 1LL) {
        for (LL i = 0; i < sz_; ++i) par[i] = i;
    }
    void init(LL sz_) {
        par.resize(sz_);
        siz.assign(sz_, 1LL);
        for (LL i = 0; i < sz_; ++i) par[i] = i;
    }

    LL root(LL x) {
        while (par[x] != x) {
            x = par[x] = par[par[x]];
        }
        return x;
    }

    bool unite(LL x, LL y) {
        x = root(x);
        y = root(y);
        if (x == y) return false;
        if (siz[x] < siz[y]) swap(x, y);
        siz[x] += siz[y];
        par[y] = x;
        return true;
    }

    bool same(LL x, LL y) {
        return root(x) == root(y);
    }

    LL size(LL x) {
        return siz[root(x)];
    }
};

int main(){
	int N;
	cin >> N;
	vector<P> T(N);
	rep(i,N){
		int x, y;
		cin >> x >> y;
		T[i] = P(x,y);
	}
	rep(i,N){
		int x = T[i].first;
		if(i == 0 or T[i].first != T[i-1].first){
			T.emplace_back(x,100);
			T.emplace_back(x,-100);
		}
	}
	sort(T.begin(),T.end());
	int siz = T.size();
	//rep(i,siz) cout << T[i].second << endl;
	for(int r = 1; r <= 40000; r++){
		UnionFind uf(siz);
		rep(i,siz){
			for(int j=i+1; j<siz; j++){
				int dx = T[j].first - T[i].first;
				if(dx * dx >= r) break;
				int dy = T[j].second - T[i].second;
				if(dx*dx+dy*dy < r) uf.unite(i,j);
			}
		}
		rep(i,siz){
			if(T[i].second == 100){
				rep(j,siz){
					if(T[j].second != -100) continue;
					if(uf.same(i,j)){
						double ans = r - 1;
						cout << setprecision(15);
						cout << sqrt(ans)/2 << endl;
						return 0;
					}
				}
			}
			else if(T[i].second == -100){
				rep(j,siz){
					if(T[j].second != 100) continue;
					if(uf.same(i,j)){
						double ans = r - 1;
						cout << setprecision(15);
						cout << sqrt(ans)/2 << endl;
						return 0;
					}
				}
			}
			else continue;
		}
	}

	return 0;
}


#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);++i)
using namespace std;

int main(){
 int N, K, M;
 cin >> N >> K >> M;
 int tmp, sum = 0;
 int target = M * N;

 rep(i,N-1){
  cerr << "target " << target << endl;
  cin >> tmp;
  target -= tmp;
 }
 int ans;
 if(target <= 0){
  ans = 0;
 } else if(target <= K){
  ans = target;
 } else {
  ans = -1;
 }
 cout << ans << endl;

}

#include<iostream>
#include<algorithm>
using namespace std;
int main(){
	int N,K,M,A,sum=0;
	cin>>N>>K>>M;
	for(int i=0;i<N-1;i++){
		cin>>A;
		sum+=A;
	}
	if(N*M-sum<=K)cout<<max(N*M-sum,0);
	else cout<<-1;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define endl '\n'
typedef pair<int, int> P;
typedef long long ll;

int dp[300][300];
int n;
vector<int> bucket;

bool valid(vector<int> x, vector<int> y)
{
    if (x.size() == 0 || y.size() == 0)
    {
        return false;
    }
    if (x.size() != y.size())
    {
        return true;
    }
    rep(i, x.size())
    {
        if (x[i] != y[i])
        {
            return true;
        }
    }
    return false;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    rep(i, n)
    {
        int t;
        cin >> t;
        bucket.push_back(t % 200);
    }
    dp[0][0] = 1;
    rep(i, n)
    {
        rep(j, 200)
        {
            if (dp[i][j] > 0)
            {
                dp[i + 1][j] += dp[i][j];
                dp[i + 1][(j + bucket[i]) % 200] += dp[i][j];
            }
        }
    }
    while (true)
    {
        int s = 0, t = 0;
        bool cant = true;
        rep(i, n + 1)
        {
            rep(j, 200)
            {
                if (j == 0 && dp[i][j] > 2)
                {
                    s = i;
                    t = j;
                    cant = false;
                }
                if (j > 0 && dp[i][j] > 1)
                {
                    s = i;
                    t = j;
                    cant = false;
                }
            }
        }
        if (cant)
        {
            cout << "No" << endl;
            return 0;
        }
        int i = s;
        int j = t;
        vector<int> x;
        while (i != 0 && j != 0)
        {
            if (dp[i - 1][(j + 200 - bucket[i - 1]) % 200] > 0)
            {
                j = (j + 200 - bucket[i - 1]) % 200;
                x.push_back(i);
                i--;
            }
            else
            {
                i--;
            }
        }
        i = s;
        j = t;
        vector<int> y;
        while (i != 0 && j != 0)
        {
            if (y.empty() && dp[i - 1][j] > 1)
            {
                i--;
            }
            else if (y.empty() && dp[i - 1][(j + 200 - bucket[i - 1]) % 200] > 1)
            {
                j = (j + 200 - bucket[i - 1]) % 200;
                y.push_back(i);
                i--;
            }
            else if (dp[i - 1][j] > 0)
            {
                i--;
            }
            else
            {
                j = (j + 200 - bucket[i - 1]) % 200;
                y.push_back(i);
                i--;
            }
        }
        if (valid(x, y))
        {
            reverse(x.begin(), x.end());
            reverse(y.begin(), y.end());
            cout << "Yes" << endl;
            cout << x.size();
            for (auto xi : x)
            {
                cout << ' ' << xi;
            }
            cout << endl;
            cout << y.size();
            for (auto yi : y)
            {
                cout << ' ' << yi;
            }
            cout << endl;
            return 0;
        }
        else
        {
            dp[s][t] = 0;
        }
    }
}

#include <bits/stdc++.h>
#define pb(x) push_back(x)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define F(w,x,y) for(ll w=x; w<y; w++)
#define endl "\n"
#define mod 1000000007
typedef long long ll;
typedef long double ld;
using namespace std;

void play()
{
    ll n;
    cin>>n;

    vector <ll> v(n);

    F(i,0,n)
    cin>>v[i];

    map <ll,ll> mp;

    ll total=1<<(min(n,9LL));

    ll ans=-1;

    F(i,1,total)
    {
        ll sum=0;

        F(j,0,(min(n,9LL)))
        {
            if(i&(1<<j))
            {
                sum+=v[j];
            }
        }

        mp[sum%200]++;

        if(mp[sum%200]>1)
        {
            ans=(sum%200);
            break;
        }
    }

    if(ans==-1)
    {
        cout<<"No\n";
        return;
    }

    cout<<"Yes\n";
    ll f=0;

    F(i,1,total)
    {
        ll sum=0, c=0;

        F(j,0,(min(n,9LL)))
        {
            if(i&(1<<j))
            {
                sum+=v[j];
                c++;
            }
        }

        if(ans==sum%200)
        {
            cout<<c<<" ";
            F(j,0,(min(n,9LL)))
            {
                if(i&(1<<j))
                {
                    cout<<j+1<<" ";
                }
            }
            cout<<endl;
            f++;
            if(f==2)
                break;
        }
    }

}

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t;
    t=1;
    //cin>>t;
    F(i,1,t+1)
    {
        //cout<<"Case #"<<i<<": ";
        play();
    }
    return 0;
}


#include <bits/stdc++.h>
#define int long long
#define lowbit(x) x & (-x)
using namespace std;
constexpr int maxn = 1000010;
int tr[maxn];
bool vis[maxn];

struct Point {
	int x, y;
	bool operator < (const Point &W) const {
		if (x == W.x) return y < W.y;
		else return x < W.x;
	}

	bool operator == (const Point &W) const {
		return x == W.x && y == W.y;
	}
} p[maxn];

void add(int x, int n) {
	for (int i = x; i <= n; i += lowbit(i)) tr[i]++;
}

int query(int x) {
	int ans = 0;
	for (int i = x; i > 0; i -= lowbit(i)) ans += tr[i];
	return ans;
}

void work() {
	int H, W, M;
	cin >> H >> W >> M;
	int x_min = maxn, y_min = maxn;
	for (int i = 1; i <= M; i++) {
		cin >> p[i].x >> p[i].y;
		if (p[i].x == 1) x_min = min(x_min, p[i].y);
		if (p[i].y == 1) y_min = min(y_min, p[i].x);
	}
	// cout << M << endl;

	for (int i = x_min + 1; i <= W; i++) p[++M] = {1, i};
	for (int i = y_min + 1; i <= H; i++) p[++M] = {i, 1};

	sort(p + 1, p + 1 + M);
	M =  unique(p + 1, p + M + 1) - (p + 1);

	// for (int i = 1; i <= M; i++) cout << p[i].x << " " << p[i].y << endl;
	int tmp = 0, h = 1;
	for (int i = 1; i <= M; i++) {
		if (!vis[p[i].y]) {
			vis[p[i].y] = 1;
			add(p[i].y, W);
			// cout << "---" << endl;
		}

		if (i == M || p[i].x != p[i + 1].x) {
			tmp += query(W) - query(p[h].y - 1);
			h = i + 1;
		}
	}
	cout << 1ll * H * W - tmp << endl;
}

int32_t main() {
	work();
}

#include<bits/stdc++.h>
using namespace std;

#define int long long int
#define endl "\n"

const int N = 105;
int A[N], B[N], C[N], D[N];

int solve(int n, int m, int k){
    int i,j,ans=0;
    unordered_map<int, int> um;
    for(i=0;i<(1 << k);i++){
        um.clear();
        int cnt=0;
        for(j=0;j<k;j++){
            if(i & (1 << j)){
                um[D[j]]++;
            }else{
                um[C[j]]++;
            }
        }
        for(j=0;j<m;j++){
            if(um[A[j]] > 0 && um[B[j]] > 0){
                cnt++;
            }
        }
        ans = max(ans, cnt);
    }
    return ans;
}

int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int t;
    t=1;
    //cin>>t;

    while(t--){
        int i,n,m,k;
        cin>>n>>m;
        for(i=0;i<m;i++){
            cin>>A[i]>>B[i];
        }
        cin>>k;
        for(i=0;i<k;i++){
            cin>>C[i]>>D[i];
        }
        cout<<solve(n, m, k)<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
using ll = long long;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define reps(i, s, n) for (int i = (s); i < (int)(n); ++i)
#define ZERO {puts("0"); return 0;}
#define NO {puts("No"); return 0;}
#define ALL(obj) begin(obj), end(obj)
#define pb push_back

template<class T> void chmin(T& a, T b) { if (a > b) a = b; }
template<class T> void chmax(T& a, T b) { if (a < b) a = b; }

// {g,x,y}: ax+by=g
tuple<ll,ll,ll> extgcd(ll a, ll b)
{
  if (b == 0) return {a,1,0};
  ll g, x, y;
  tie(g,x,y) = extgcd(b, a%b);
  return {g, y, x-a/b*y};
}

void solve()
{
  ll n, s, k;
  cin >> n >> s >> k;
  ll g, x, y;
  tie(g,x,y) = extgcd(k,n);
  if (s%g != 0) {
    cout << -1 << endl;
    return;
  }
  n /= g;
  s /= g;
  k /= g;
  ll ans = ((x*-s)%n+n)%n;
  cout << ans << endl;
}

int main()
{
  int t; cin >> t;
  rep(i,t) solve();
}

/*  -*- coding: utf-8 -*-
 *
 * e.cc: E - Throne
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>

using namespace std;

/* constant */

/* typedef */

typedef long long ll;

/* global variables */

/* subroutines */

void exgcd(ll x, ll y, ll &a, ll &b, ll &c) {
  ll r0 = x, r1 = y;
  ll a0 = 1, a1 = 0;
  ll b0 = 0, b1 = 1;

  while (r1 > 0) {
    ll q1 = r0 / r1;
    ll r2 = r0 % r1;
    ll a2 = a0 - q1 * a1;
    ll b2 = b0 - q1 * b1;
    r0 = r1, r1 = r2;
    a0 = a1, a1 = a2;
    b0 = b1, b1 = b2;
  }
  c = r0;
  a = a0;
  b = b0;
}

/* main */

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

  while (tn--) {
    int n, s, k;
    scanf("%d%d%d", &n, &s, &k);

    // s + ik == 0(mod n) -> s+ik=jn -> jn-ik=s

    ll a, b, c;
    exgcd(n, k, a, b, c);
    //printf("%d*%lld+%d*%lld=%lld\n", n, a, k, b, c);

    if (s % c != 0) puts("-1");
    else {
      b *= -s / c;
      ll d = n / c;
      while (b < 0) b = b % d + d;
      while (b >= d) b %= d;
      printf("%lld\n", b);
    }
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for (int i = 0; i < n; i++)

vector<vector<int>> to;

vector<int> used;
vector<int> col;
vector<int> vs;

ll now;
int n;

void dfs(int v) {
  if (used[v]) return;
  used[v] = 1;
  vs.push_back(v);
  for (int u: to[v]) dfs(u);
}

void dfs2(int i) {
  if (i == vs.size()) { now++; return; }
  int v = vs[i];
  rep(c, 3) {
    col[v] = c;
    bool ok = true;
    for (int u: to[v]) {
      if (col[u] == c) ok = false;
    }
    if (!ok) continue;
    dfs2(i+1);
  }
  col[v] = -1;
}

int main() {
  int m;
  cin >> n >> m;
  to = vector<vector<int>>(n);
  rep(i, m) {
    int a,b;
    cin >> a >> b;
    --a, --b;
    to[a].push_back(b);
    to[b].push_back(a);
  }
  ll ans = 1;
  used = vector<int>(n);
  col = vector<int>(n, -1);
  rep(i,n) {
    if (used[i]) continue;
    vs = vector<int>();
    dfs(i);
    col[vs[0]] = 0;
    now = 0;
    dfs2(1);
    ans *= 3 * now;
  }
  cout << ans << endl;
}

#include <bits/stdc++.h>
using namespace std;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

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

	vector<vector<int>> adj(n, vector<int>(n));
	for (int i = 0, u, v; i < m; i++) {
		cin >> u >> v, --u, --v;
		adj[u][v] = adj[v][u] = 1;
	}

	vector<int> is_independent(1 << n, 1);
	for (int mask = 1; mask < (1 << n); mask++) {
		int &f = is_independent[mask];
		int msb = 32 - __builtin_clz(mask) - 1;

		f = is_independent[mask ^ (1 << msb)];

		for (int i = 0; i < msb; i++) {
			if (mask >> i & 1) {
				f &= !adj[i][msb];
			}
		}
	}

	long long ans = 0;
	const int all = (1 << n) - 1;

	for (int mask = 0; mask < (1 << n); mask++) {
		vector<int> col(n, -1);
		auto dfs = [&](int x, const auto &self) -> void {
			for (int i = 0; i < n; i++) {
				if ((mask >> i & 1) && adj[x][i] && col[i] == -1) {
					col[i] = 1 ^ col[x];
					self(i, self);
				}
			}
		};

		int comps = 0;
		for (int i = 0; i < n; i++) {
			if ((mask >> i & 1) && col[i] == -1) {
				col[i] = 0;
				dfs(i, dfs);
				comps += 1;
			}
		}

		bool is_bipartite = true;
		for (int i = 0; i < n; i++) {
			if ((mask >> i & 1)) {
				for (int j = 0; j < i; j++) {
					if ((mask >> j & 1) && adj[i][j]) {
						is_bipartite &= col[i] != col[j];
					}
				}
			}
		}

		ans += is_bipartite * is_independent[all ^ mask] * (1ll << comps);
	}
	cout << ans << '\n';
	return 0;
}


#include<bits/stdc++.h>
#define rep(i, n) for (int i=0;i<(n);i++)
using namespace std;
#define vecout(V) for(int i=0;i<V.size();i++) {cout<<V[i]<<endl;}
typedef long long ll;

int main(void){
    int n, k, m;
    cin>>n>>k>>m;
    vector<int> v(n-1);
    rep(i, n-1) cin>>v[i];
    int sum=accumulate(v.begin(), v.end(), 0);
    int ans=n*m-sum;
    if(ans<=k) cout<<max(0, ans)<<endl;
    else cout<<-1<<endl;
    return 0;
}

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
	int N, K, M;
	cin >> N >> K >> M;
	vector<int> A(N);
	int ans = 0, sum = 0;
	for (int i = 0; i < N - 1; i++)
	{
		cin >> A[i];
		sum += A[i];
	}
	ans = M * N - sum;
	if (ans > K) {
		cout << "-1" << endl;
	}
	else if (ans <= 0) {
		cout << "0" << endl;
	}
	else {
		cout << ans << endl;
	}
}


#include <bits/stdc++.h>

using namespace std;

typedef long long i64;
typedef unsigned long long ui64;
typedef vector<i64> vi;
typedef vector<vi> vvi;
typedef pair<i64, i64> pi;

#define pb push_back
#define sz(a) i64((a).size())
#define all(c) (c).begin(), (c).end()
#define REP(s, e, i) for(i=(s); i < (e); ++i)
inline void RI(i64 &i) {scanf("%lld", &(i));}
inline void RVI(vi &v) { for(i64 i=0;i<sz(v);++i) { RI(v[i]); } }
inline void RVVI(vvi &vv) { for(i64 i=0;i<sz(vv);++i) { RVI(vv[i]); } }
inline void WI(const i64 &i) {printf("%lld\n", i);}
inline void WVI(const vi &v, char sep=' ') { for(i64 i=0;i<sz(v);++i) { if(i != 0){ printf("%c", sep); } printf("%lld", v[i]);} printf("\n"); }
inline void WS(const string &s) { printf("%s\n", s.c_str()); }
inline void WB(bool b, const string &yes, const string &no) { if(b){ WS(yes);} else { WS(no);} }
inline void YESNO(bool b) { WB(b, "YES", "NO"); }
#define BUF_LENGTH 1000000
inline void RS(string &s) {static char buf[BUF_LENGTH]; scanf("%s", buf); s = buf;}
template<typename T> inline bool IN(T &S, const typename T::key_type &key) {
  return S.find(key) != S.end();
}

template<typename T>
vector<T> sieve(T max) {
  if(max < 2) {
    return {};
  }
  using flag_type = unsigned char;
  vector<flag_type> is_prime(max+1, 1);

  // 2
  vector<T> primes = {2};
  int q = 4;
  while(q <= max) {
    is_prime[q] = 0;
    q += 2;
  }

  // 3 or more
  for(int p=3;p<=max;p+=2) {
    if(is_prime[p]) {
      primes.push_back(p);
      int q = p * 3;
      while(q <= max) {
        is_prime[q] = 0;
        q += p * 2;
      }
    }
  }

  return primes;
}

int main(int argc, char *argv[]) {
  i64 i, j, k;
  i64 N; cin >> N;
  vi primes = sieve(N + 1);

  vi ans(N+1, 0);
  ans[1] = 1;
  REP(1, N+1, i) {
    for(auto &p: primes) {
      if(i*p > N) {
        break;
      }
      i64 n = ans[i] + 1;
      if(ans[i*p] == 0) {
        ans[i*p] = n;
      }
      else {
        assert(ans[i*p] >= n);
      }
    }
  }

  REP(1, N+1, i) {
    if(i != 1) {
      printf(" ");
    }
    printf("%lld", ans[i]);
  }
  printf("\n");

  return 0;
}

//a.9
#include<bits/stdc++.h>
#define ll long long
#define all(x) x.begin(),x.end()
#define CASE(t) cout<<"Case #"<<(t)<<": ";
#define endll endl
#define endl '\n'
#define mod 1000000007
#define INF 1e18
#define maxN 500005
#define ios ios_base::sync_with_stdio(false); cin.tie(NULL)
using namespace std;
ll ceill(ll a,ll b){return a/b+bool(a%b);}
ll gcd(ll a,ll b){if(b==0)return a;return gcd(b, a % b);}
ll lcm(ll a,ll b){return (a*b)/gcd(a,b);}
ll power(ll x,ll y){ll res=1;while(y>0){if(y&1)res=(res*x)%mod;x=(x*x)%mod;y>>=1;}return res;}
bool isPrime(ll n)
{
    if(n<=1)return false;if(n<=3)return true;if(n%2==0 || n%3==0)return false;
    for(ll i=5;i*i<=n;i=i+6)if(n%i==0 || n%(i+2)==0) return false; return true;
}
void primeFactors(ll n, vector<ll> &vec)
{
    while(n%2==0)vec.push_back(2),n/=2;
    for(ll i=3;i<=sqrt(n);i+=2)while(n%i==0)vec.push_back(i),n/=i;
    if(n>2)vec.push_back(n);
}
//vector<ll>fact(maxN);
//void factpre(){fact[0]=1;for(ll i=1;i<maxN;i++)fact[i]=(fact[i-1]*1LL*i);}
//ll mul(ll a,ll b){return (a*1LL*b)%mod;}
//ll nCr(ll n,ll k){return mul(fact[n],power(mul(fact[k],fact[n-k]),mod-2));}
bool chk(ll n)
{
    return n&&(!(n&(n-1)));
}
void solve()
{
    ll n;cin>>n;
    ll tmp=0;
    for(ll i=1;i<=n;i++)
    {
        if(i==1 || chk(i))cout<<(++tmp)<<" ";
        else cout<<tmp<<" ";
    }
    cout<<endl;
}
signed main()
{
    ios;
    ll tc=1; //cin>>tc;
    for(ll TTT=1;TTT<=tc;TTT++)
    {
        //CASE(TTT);
        solve();
    }
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <map> //http://vivi.dyndns.org/tech/cpp/map.html
#include <set> //http://vivi.dyndns.org/tech/cpp/set.html
#include <vector>
#include <deque>
#include <queue>
#include <numeric> //gcd,lcm c++17
#include <tuple>
#include <iomanip> //setprecision
#include <unordered_map>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;

#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define ALL(a) (a).begin(), (a).end()
#define sz(x) (int)(x).size()
template <class T1, class T2, class Pred = std::less<T2>>
struct sort_pair_second
{
    bool operator()(const std::pair<T1, T2> &left, const std::pair<T1, T2> &right)
    {
        Pred p;
        return p(left.second, right.second);
    }
};
int MOD = 1e9 + 7;
double EPS = 1e-9;
int INF = 1000000005;
long long INFF = 1000000000000000005LL;
double PI = acos(-1);
int dirx[8] = {-1, 0, 0, 1, -1, -1, 1, 1};
int diry[8] = {0, 1, -1, 0, -1, 1, -1, 1};

int main()
{
    double A, B;
    cin >> A >> B;
    cout << (1.0 - B / A) * (double)100 << endl;
}

#include<bits/stdc++.h>
#include<vector>
#include<set>
#include<algorithm>

using namespace std;

#define rep(i,n) for(int i=0; i<(n); i++)
#define T int t, q=0; scanf("%d", &t); while(q++<t)
#define ll long long
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
ll x,y,z,a=0,b=0,c=0,d=0,sum,ans,total,cnt,n,m,k,p,kase,i;
string s1,s2;
#define N 100001
#define MX 1000000001
vector <ll> v;
map<ll,ll> mp;
set<ll> s;
map<ll,ll> :: iterator itr1, ptr1;
set<ll> :: iterator itr, ptr;

ll tow(ll a, ll b) {
	ll ans = 1;
	for (int i = 0; i < b; i++) {
		ans *= a;
	}
	return ans;
}
ll lcm(ll a, ll b) {
	return (a * b) / __gcd(a, b);
}

//SET PRECIOSION   cout << fixed ;  cout << setprecision(n) << ans << endl;

int main()
{
    ll n;
    cin>>n;

    ll ans=1;
    ll p[]={2, 3, 5, 7, 11, 13, 17, 19, 23, 29};

    for(int i=0;i<10;i++)
    {
        ll temp=1;
        while(temp*p[i]<=n) temp*=p[i];

        ans*=temp;
    }
    cout<<ans+1<<endl;
}
// Happy Coding  (:


#include <iostream>
using namespace std;
string Save[3][125];
string erase(string n,int index){
	string a,b;
	a=b="";
	for(int i=0;i<index;i++){
		a+=n[i];
	}
	for(int i=index+1;i<n.length();i++){
		b+=n[i];
	}
	return a+b;
}
int main() {
	int c1,c2,c3;
	c1=c2=c3=0;
	for(int i=8;i<1000;i+=8){
		if(to_string(i).length()==3){
			Save[2][c3]=to_string(i);
			c3++;
		}else if(to_string(i).length()==2){
			Save[1][c2]=to_string(i);
			c2++;
		}else{
			Save[0][c1]=to_string(i);
			c1++;
		}
	}
	string S;
	cin>>S;
	string temp;//Temporary S, we need S!
	int find_i;//Finding the # index of the chart
	if(S.length()>=3){//If S has 3 or more digits
		for(int i=0;i<c3;i++){
			temp=S;
			find_i=0;
			for(int j=0;j<temp.length() and find_i<3;j++){
				if(temp[j]==Save[2][i][find_i]){
					find_i++;
					temp=erase(temp,j);
					j=-1;
				}
			}
			if(find_i==3){
				cout<<"Yes";
				break;
			}
			if(i==c3-1){
				cout<<"No";
				break;
			}
		}
	}else if(S.length()==2){//If S has 2 digits
		for(int i=0;i<c2;i++){
			temp=S;
			find_i=0;
			for(int j=0;j<temp.length() and find_i<2;j++){
				if(temp[j]==Save[1][i][find_i]){
					find_i++;
					temp=erase(temp,j);
					j=-1;
				}
			}
			if(find_i==2){
				cout<<"Yes";
				break;
			}
			if(i==c2-1){
				cout<<"No";
				break;
			}
		}
	}else{//If S has 1 digit
		if(S=="8"){
			cout<<"Yes";
		}else{
			cout<<"No";
		}
	}
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<cmath>
#include<iomanip>
#include<map>
#include<stack>
#include<queue>
#include<set>
#include<deque>
#include<list>
using namespace std;
using P = pair<int,int>;
using ll = long long;

const long double EPS = 1e-10;
const ll MOD = 1000000007LL;
const ll INF = 1LL << 60;

int main(){

  string S;
  list<char> T;
  bool rev = false;
  cin >> S;
  for(int i = 0 ; i < S.size() ; i++){
    if(S[i] == 'R')
      rev = !rev;
    else{
      if(rev){
        if(T.front() == S[i])
          T.pop_front();
        else
          T.push_front(S[i]);
      }
      else{
        if(T.back() == S[i])
          T.pop_back();
        else
          T.push_back(S[i]);
      }
    }
  }
  string ans;
  for(auto x : T)
    ans += x;
  if(rev)
    reverse(ans.begin(),ans.end());
  cout << ans << endl;

}

//红太阳zhouakngyang txdy!
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#include<bits/stdc++.h>
using namespace std;
//#define int long long
inline int read()
{
	int sum=0,nega=1;char ch=getchar();
	while(ch>'9'||ch<'0'){if(ch=='-')nega=-1;ch=getchar();}
	while(ch<='9'&&ch>='0')sum=sum*10+ch-'0',ch=getchar();
	return sum*nega;
}
int n,L=500001,R=500000,flag,top;
char a[500009],ans[1000009],res[1000009];
char out[1000009];
int main()
{
	scanf("%s",a+1);n=strlen(a+1);
	for(int i=1;i<=n;i++)
	{
		if(a[i]=='R')flag^=1;
		else
		{
			if(flag)L--,res[L]=a[i];
			else R++,res[R]=a[i];
		}
	}
	if(!flag)
		for(int i=1;i<=R-L+1;i++)ans[i]=res[i+L-1];
	else
		for(int i=1;i<=R-L+1;i++)ans[i]=res[R+1-i];
	for(int i=1;i<=R-L+1;i++)
	{
		out[++top]=ans[i];
		while(top!=0&&out[top]==out[top-1])top-=2;
	}
	for(int i=1;i<=top;i++)putchar(out[i]);
	return 0;

}


#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
const int SIZE = 200005;
const int BLOCK = 200005;

class SegTree{
public:
  int v[SIZE*4];

  SegTree(){
    memset(v,0,sizeof(v));
  }

  int get(int node, int L, int R, int LQ, int RQ){
    if(R<LQ || RQ<L) return 0;
    if(LQ<=L && R<=RQ) return v[node];
    int s = get(node*2+1,L,(L+R)/2,LQ,RQ);
    int t = get(node*2+2,(L+R)/2+1,R,LQ,RQ);
    return s+t;
  }

  int update(int node, int L, int R, int Q, int chVal){
    if(R<Q || Q<L) return v[node];
    if(L==R) return v[node] = chVal;
    int s = update(node*2+1,L,(L+R)/2,Q,chVal);
    int t = update(node*2+2,(L+R)/2+1,R,Q,chVal);
    return v[node] = s+t;
  }
};

int row, col;
int nBlock, blockPos[BLOCK][2];

void read() {
    cin >> row >> col >> nBlock;
    for (int i = 0; i < nBlock; ++i) {
        cin >> blockPos[i][0] >> blockPos[i][1];
        --blockPos[i][0];
        --blockPos[i][1];
    }
}

void work() {
    static vector<int> row2col[SIZE], col2row[SIZE];
    for (int i = 0; i < SIZE; ++i) {
        row2col[i].clear();
        col2row[i].clear();
    }
    for (int i = 0; i < nBlock; ++i) {
        row2col[blockPos[i][0]].push_back(blockPos[i][1]);
        col2row[blockPos[i][1]].push_back(blockPos[i][0]);
    }
    for (int i = 0; i < SIZE; ++i) {
        sort(row2col[i].begin(), row2col[i].end());
        sort(col2row[i].begin(), col2row[i].end());
    }

    long long sum = 0;

    // Add horizontal line
    for (int r = 0; r < (col2row[0].size() > 0 ? col2row[0][0] : row); ++r) {
        sum += row2col[r].size() > 0 ? row2col[r][0] : col;
    }

    // Add vertical line
    static SegTree stree;
    stree = SegTree();
    for (int c = 0; c < (row2col[0].size() > 0 ? row2col[0][0] : col); ++c) {
        int until = col2row[c].size() > 0 ? col2row[c][0] - 1 : row - 1;
        if (until >= 0)
            sum += stree.get(0, 0, row - 1, 0, until);

        if (c == 0) {
            for (int r = (col2row[0].size() > 0 ? col2row[0][0] : row); r < row; ++r) {
                stree.update(0, 0, row - 1, r, 1);
            }
        } else {
            for (int i = 0; i < col2row[c].size(); ++i) {
                stree.update(0, 0, row - 1, col2row[c][i], 1);
            }
        }
    }

    cout << sum << endl;
}

int main() {
    read();
    work();
    return 0;
}

#include <bits/stdc++.h>
// clang-format off
using namespace std; using ll = long long; using ull = unsigned long long; using pll = pair<ll,ll>; const ll INF = 4e18;
void print0() {}
template<typename Head,typename... Tail> void print0(Head head,Tail... tail){cout<<fixed<<setprecision(15)<<head;print0(tail...);}
void print() { print0("\n"); }
template<typename Head,typename... Tail>void print(Head head,Tail... tail){print0(head);if(sizeof...(Tail)>0)print0(" ");print(tail...);}
// clang-format on

string a, b, c;
ll na, nb, nc;
vector<ll> aa, bb, cc;
ll nmp[10];
inline ll getnum(char c) {
    if (nmp[0] == c) return 0;
    if (nmp[1] == c) return 1;
    if (nmp[2] == c) return 2;
    if (nmp[3] == c) return 3;
    if (nmp[4] == c) return 4;
    if (nmp[5] == c) return 5;
    if (nmp[6] == c) return 6;
    if (nmp[7] == c) return 7;
    if (nmp[8] == c) return 8;
    if (nmp[9] == c) return 9;
    return -1;
}
inline ll getchar(ll n) {
    return nmp[n];
}
void fail() {
    print("UNSOLVABLE");
    exit(0);
}
void success() {
    for (ll i = 0; i < na; i++) {
        print0(aa[na - i - 1]);
    }
    print();
    for (ll i = 0; i < nb; i++) {
        print0(bb[nb - i - 1]);
    }
    print();
    for (ll i = 0; i < nc; i++) {
        print0(cc[nc - i - 1]);
    }
    print();
    exit(0);
}
void rec(ll i, ll kuri, ll abc, vector<ll> &nvec, string &ts) {
    if (abc == 0 || abc == 1) {
        ll nn = ts.size();
        if (i >= nn) {
            rec(i, kuri, abc + 1, bb, b);
            return;
        }
        char d = ts[i];
        if (getnum(d) < 0) {
            for (ll j = 0; j < 10; j++) {
                if (nmp[j] >= 0) continue;
                nmp[j] = d;
                nvec.push_back(getnum(d));
                rec(i, kuri, abc + 1, bb, b);
                nvec.pop_back();
                nmp[j] = -1;
            }
        } else {
            nvec.push_back(getnum(d));
            rec(i, kuri, abc + 1, bb, b);
            nvec.pop_back();
        }
    }
    if (abc == 2) {
        char d = c[i];
        ll cv = kuri;
        if (i < na) cv += aa[i];
        if (i < nb) cv += bb[i];
        ll nkuri = 0;
        if (cv >= 10) {
            nkuri = 1;
            cv -= 10;
        }
        bool change = false;
        if (getnum(d) < 0) {
            if (getchar(cv) >= 0) {
                return;
            }
            nmp[cv] = d;
            change = true;
        }
        if (getnum(d) != cv) {
            return;
        }

        if (i == nc - 1) {
            cc.push_back(cv);
            if (!(nkuri > 0 || aa.back() == 0 || bb.back() == 0 || cc.back() == 0)) {
                success();
            }
            cc.pop_back();
        } else {
            cc.push_back(cv);
            rec(i + 1, nkuri, 0, aa, a);
            cc.pop_back();
        }

        if (change) {
            nmp[cv] = -1;
        }
    }
}
int main() {
    cin >> a >> b >> c;
    reverse(a.begin(), a.end());
    reverse(b.begin(), b.end());
    reverse(c.begin(), c.end());
    na = a.size();
    nb = b.size();
    nc = c.size();
    if (na > nc || nb > nc || nc > max(na, nb) + 1) {
        fail();
    }
    {
        set<char> chars;
        for (char x : a) {
            chars.insert(x);
        }
        for (char x : b) {
            chars.insert(x);
        }
        for (char x : c) {
            chars.insert(x);
        }
        if (chars.size() > 10) {
            fail();
        }
    }
    for(ll i=0;i<10;i++){
      nmp[i]=-1;
    }
    rec(0, 0, 0, aa, a);
    fail();
}