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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<string, string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<ll> vl;
typedef vector<vl> vvl;

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 };
const ll MOD = 1000000007;

ll sum() { return 0; }
template<typename T, typename... Args>
T sum(T a, Args... args) { return a + sum(args...); }

#define DEBUG fprintf(stderr, "====TESTING====\n")
#define VALUE(x) cerr << "The value of " << #x << " is " << x << endl
#define OUT(x) cout << x << endl
#define OUTH(x) cout << x << " "
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define READ(x) for(auto &(z):x) cin >> z;
#define FOR(a, b, c) for (int(a)=(b); (a) < (c); ++(a))
#define FORN(a, b, c) for (int(a)=(b); (a) <= (c); ++(a))
#define FORD(a, b, c) for (int(a)=(b); (a) >= (c); --(a))
#define FORSQ(a, b, c) for (int(a)=(b); (a) * (a) <= (c); ++(a))
#define FORC(a, b, c) for (char(a)=(b); (a) <= (c); ++(a))
#define EACH(a, b) for (auto&(a) : (b))
#define REP(i, n) FOR(i, 0, n)
#define REPN(i, n) FORN(i, 1, n)
#define MAX(a, b) a=max(a, b)
#define MIN(a, b) a=min(a, b)
#define SQR(x) ((ll)(x) * (x))
#define RESET(a, b) memset(a, b, sizeof(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ALL(v) v.begin(), v.end()
#define ALLA(arr, sz) arr, arr + sz
#define SIZE(v) (int)v.size()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr, sz) sort(ALLA(arr, sz))
#define REVERSEA(arr, sz) reverse(ALLA(arr, sz))
#define PERMUTE next_permutation
#define TC(t) while (t--)
#define FAST_INP  ios_base::sync_with_stdio(false);cin.tie(NULL)
#define what_is(x) cerr << #x << " is " << x << endl;

template <typename T = ll> bool extgcd(T a, T b, T &x, T &y, T d) {
    if(!b and d % a) return false;
    if(!b) {
        x = d / a, y = 0;
        return true;
    }
    if(extgcd(b, a % b, y, x, d)) {
        y -= a / b * x;
        return true;
    }
    return false;
}

void solve() {
	ll n, s, k;
	cin >> n >> s >> k;
	ll x, y;
	if(s % __gcd(n, k)) {
		OUT(-1);
		return;
	}
	if(extgcd(k, n, x, y, -s)) {
		x %= n / __gcd(n, k);
		if(x < 0) x += n / __gcd(k, n);
		OUT(x);
	} else {
		OUT(-1);
	}
}

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

    int tc; cin >> tc;
    TC(tc) solve();
//	solve();
    return 0;
}

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

long long int modinv(long long int a, long long int m) {
  long long int b = m, u = 1, v = 0;
  while (b) {
    long long int t = a / b;
    a -= t * b; swap(a, b);
    u -= t * v; swap(u, v);
  }
  u %= m;
  if (u < 0) u += m;
  return u;
}

int main() {
  int T;
  cin >> T;
  for (int i = 0; i < T; i++) {
    long long int N, S, K;
    cin >> N >> S >> K;
    long long int G = __gcd(K, N);
    if ((N - S) % G != 0) cout << -1 << endl;
    else {
      long long int A, B, n;
      A = K / G;
      B = (N - S) / G;
      n = N / G;
      long long int C = modinv(A, n);
      long long int ans = C * B % n;
      if (ans < 0) ans += n;
      cout << ans << endl;
    }
  }
}


#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 1003;

ll t, n;

bool c[N];

void solv()
{
    cin >> t >> n;

    for (int A = 1; A <= 200; ++A)
    {
        ll x = (100 + t) * A / 100;
        c[x] = true;
    }

    vector<int> v;
    for (int i = 1; i < 100 + t; ++i)
    {
        if (!c[i])
            v.push_back(i);
    }

    cout << ((n - 1) / sz(v)) * (100LL + t) + v[(n - 1) % sz(v)] << "\n";
}

int main()
{
    #ifdef SOMETHING
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    #endif // SOMETHING
    //ios_base::sync_with_stdio(false), cin.tie(0);

    int tt = 1;
    //cin >> tt;
    while (tt--)
    {
        solv();
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mkp make_pair
#define mt make_tuple
#define ff first
#define ss second
#define M1 1000000007
#define M2 998244353

#define fl(i,a,b)  for(ll i=a;i<b;i++)
#define bfl(i,a,b) for(ll i=b-1;i>=a;i--)
#define f(i,n)     for(int i=0;i<n;i++)
#define bf(i,n)    for(int i=n-1;i>=0;i--)
#define f1(i,n)    for(int i=1;i<=n;i++)
#define bf1(i,n)   for(int i=n;i>=1;i--)

#define all(v)      v.begin(),v.end()
#define pll         pair<long,long>
#define pii         pair<int,int>
#define vi          vector<int>
#define vll          vector<ll>

#define p0(a)       cout << a << " "
#define p1(a)       cout << a << bn
#define p2(a, b)    cout << a << " " << b << bn
#define p3(a, b, c) cout << a << " " << b << " " << c << bn
#define p4(a,b,c,d) cout << a << " " << b << " " << c << " " << d << bn
#define debug(x)    cerr << #x << " = " << x << endl;

#define max3(a,b,c) max(max(a,b),c)
#define min3(a,b,c) min(min(a,b),c)

#define bn          "\n"
#define pbn         cout<<"\n"

#define TRACE
#ifdef TRACE
    #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template <typename Arg1>
    void __f(const char* name, Arg1&& arg1){
        cerr << name << " : " << arg1 << endl;
    }
    template <typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names + 1, ',');
        cerr.write(names, comma - names) << " : " << arg1<<" | ";
        __f(comma+1, args...);
    }
#else
    #define trace(...) 1
#endif

ll modI(ll a,ll m);
ll gcd(ll a,ll b);
ll powM(ll x,ll y,ll m);

void py(int ok)
{
    if(ok==1){
        cout<<"YES"<<bn;
    }else{
        cout<<"NO"<<bn;
    }
}

void swap(ll& a,ll& b)
{
    ll tp=a;
    a=b;
    b=tp;
}

ll gcd(ll x,ll y)
{
    if(x==0) return y;
    return gcd(y%x,x);
}

ll lcm(ll x, ll y)
{
    if(x==0 || y==0){
        cout<<"Not valid lcm"<<bn;
    }

    ll LCM= x/gcd(x,y);
    LCM=LCM*y;

    return LCM;
}

ll powM(ll x,ll y,ll m)
{
    ll ans=1,r=1;
    x%=m;
    while(r>0&&r<=y)
    {
        if(r&y)
        {
            ans*=x;
            ans%=m;
        }
        r<<=1;
        x*=x;
        x%=m;
    }
    return ans;
}

ll modI(ll a, ll m)
{
    ll m0=m,y=0,x=1;
    if(m==1) return 0;
    while(a>1)
    {
        ll q=a/m;
        ll t=m;
        m=a%m;
        a=t;
        t=y;
        y=x-q*y;
        x=t;
    }
    if(x<0) x+=m0;
    return x;
}

//-----------------------------------------------------------------------------

int main()
{

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(0);
	int tt=1;
    // cin>>tt;
   f1(testcase_no,tt)
   {
       //   cout<<"test case no. "<<testcase_no<<bn;

      ll t,n;
      cin>>t>>n;

      ll below=100/t;
      ll rem=100%t;
    //   if(rem==0)

       ll ans=(below+1)*n;
       if((rem*n)%t==0)
       ans+=((rem*n)/t-1);
       else
        ans+=((rem*n)/t);

     p1(ans);

        // cout << fixed << setprecision(25);

   }

	return 0;
}


#include <iostream>
#include <vector>
#include <map> // map<string, int> mp;
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <queue>

using namespace std;

using Graph = vector<vector<int>>;

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

typedef long long ll;

int main() {
    int N, M; cin >> N >> M;

    Graph paths(N);

    ll ans = 0;

    rep (i, M) {
        ll A, B; cin >> A >> B;
        paths[A - 1].push_back(B - 1);
    }

    rep (i, N) {
        int cnt = 0;
        vector<bool> visited(N, false);
        visited[i] = true;
        queue<int> que;
        que.push(i);
        while(!que.empty()) {
            int q = que.front(); que.pop();
            for (const auto& next : paths[q]) {
                if (visited[next]) continue;
                visited[next] = true;
                que.push(next);
                ++cnt;
            }
        }
        ans += cnt;
    }

    cout << ans + N << endl;
}

#include <bits/stdc++.h>

using namespace std;

// clang-format off
/* accelration */
// 高速バイナリ生成
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
// cin cout の結びつけ解除, stdioと同期しない(入出力非同期化)
// cとstdの入出力を混在させるとバグるので注意
struct Fast {Fast() {std::cin.tie(0); ios::sync_with_stdio(false);}} fast;

/* alias */
using ull = unsigned long long;
using ll = long long;
using vi = vector<int>;
using vl = vector<long>;
using vll = vector<long long>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using vvll = vector<vll>;
using vs = vector<string>;
using pii = pair<int, int>;

/* define short */
#define pb push_back
#define mp make_pair
#define all(obj) (obj).begin(), (obj).end()
#define YESNO(bool) if(bool){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}
#define yesno(bool) if(bool){cout<<"yes"<<endl;}else{cout<<"no"<<endl;}
#define YesNo(bool) if(bool){cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}

/* REP macro */
#define reps(i, a, n) for (ll i = (a); i < (ll)(n); ++i)
#define rep(i, n) reps(i, 0, n)
#define rrep(i, n) reps(i, 1, n + 1)
#define repd(i,n) for(ll i=n-1;i>=0;i--)
#define rrepd(i,n) for(ll i=n;i>=1;i--)

/* debug */
// 標準エラー出力を含む提出はrejectされる場合もあるので注意
#define debug(x) cerr << "\033[33m(line:" << __LINE__ << ") " << #x << ": " << x << "\033[m" << endl;

/* func */
inline int in_int() {int x; cin >> x; return x;}
inline ll in_ll() {ll x; cin >> x; return x;}
inline string in_str() {string x; cin >> x; return x;}
// search_length: 走査するベクトル長の上限(先頭から何要素目までを検索対象とするか、1始まりで)
template <typename T> inline bool vector_finder(std::vector<T> vec, T element, unsigned int search_length) {
    auto itr = std::find(vec.begin(), vec.end(), element);
    size_t index = std::distance( vec.begin(), itr );
    if (index == vec.size() || index >= search_length) {return false;} else {return true;}
}
template <typename T> inline void print(const vector<T>& v, string s = " ")
    {rep(i, v.size()) cout << v[i] << (i != (ll)v.size() - 1 ? s : "\n");}
template <typename T, typename S> inline void print(const pair<T, S>& p)
    {cout << p.first << " " << p.second << endl;}
template <typename T> inline void print(const T& x) {cout << x << "\n";}
template <typename T, typename S> inline void print(const vector<pair<T, S>>& v)
    {for (auto&& p : v) print(p);}
// 第一引数と第二引数を比較し、第一引数(a)をより大きい/小さい値に上書き
template <typename T> inline bool chmin(T& a, const T& b) {bool compare = a > b; if (a > b) a = b; return compare;}
template <typename T> inline bool chmax(T& a, const T& b) {bool compare = a < b; if (a < b) a = b; return compare;}

using Graph = vector<vector<int>>;

vector<bool> seen;
void dfs(const Graph &G, int v) {
    seen[v] = true; // v を訪問済にする

    // v から行ける各頂点 next_v について
    for (auto next_v : G[v]) {
        if (seen[next_v]) continue; // next_v が探索済だったらスルー
        dfs(G, next_v); // 再帰的に探索
    }
}

int main(){

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

    Graph G(n);

    for (int i = 0; i < m; ++i) {
        int a, b;
        cin >> a >> b;
        a--; b--;
        G[a].push_back(b);
    }

    int count = 0;
    for(int i = 0; i < n; i++){
        seen.assign(n, false); // 全頂点を「未訪問」に初期化
        dfs(G, i);
        for(int j = 0; j < n; j++){
            // t に辿り着けるかどうか
            if (seen[j]) count++;
        }
    }

    cout << count << endl;

}


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

#define int int long long
#define fr(i, n) for (int i = 0; i < n; i++)
#define fr1(i, n) for (int i = 1; i <= n; i++)
#define S second
#define F first
#define pb(n) push_back(n)
#define endl "\n"
int mod = 1e9 + 7;
vector<int> v[1000001], tr[100001];
int level[1000001] = {0};
int in[1000001] = {0};
int out[1000001] = {0};
int low[1000001] = {0};
int vis[1000001] = {0};
int visit[1001][1001] = {0};
int dis[1001][1001] = {0};
int dx[] = {-1, 0, 1, 0, -1, 1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
int timer, n, m, t, k, q, eq = 0;

signed main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  t = 1;
  //
  while (t--)
  {
    cin >> n;
    double te = 0.01;
    pair<double, double> a[n];
    fr(i, n)
    {
      double x, l, r;
      cin >> x >> l >> r;
      if (x == 2)
      {
        r -= te;
      }
      if (x == 3)
      {
        l += te;
      }
      if (x == 4)
      {
        l += te, r -= te;
      }
      a[i] = {l, r};
    }
    int ans = 0;
    for (int i = 0; i < n; i++)
    {
      for (int j = i - 1; j >= 0; j--)
      {
        if (a[i].F > a[j].S || a[i].S < a[j].F)
        {
          continue;
        }
        else
        {
          ans++;
        }
      }
    }
    cout << ans << endl;
  }
}

#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
#define lli long long int
#define pi pair<lli,lli>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define FOR(i,a,b) for(lli i=a;i<b;i++)
#define all(x) x.begin(), x.end()
const lli inf=10000000000000000;
lli mod = 1e9+7;
//const long double pi = 3.14159265358979323846264338;
lli powmod(lli x,  lli y, lli p){lli res=1;x=x%p;if(x==0)return 0;while(y>0){if(y&1)res=(res*x)%p;y = y>>1;x=(x*x)%p;}return res;}
lli _pow(lli a, lli b){if(!b)return 1;lli temp = _pow(a, b / 2);temp = (temp * temp);if(b % 2)return (a * temp);return temp;}
bool isPrime(lli n){if(n<=1)return false;if(n<=3)return true;if(n%2==0||n%3==0)return false;for(lli i=5;i*i<=n;i=i+6)if(n%i==0||n%(i+2)==0)return false;return true;}
lli lcm(lli a, lli b){ return a * b / __gcd(a, b);}
lli  cl(lli  a,lli  x){if(a%x==0)return a/x;else return a/x+1;}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t=1;
    //cin >> t;
while(t--) {
    lli n,b,c;
    cin >> n;
    vector <lli> v(n);
    vector <pi> a(n);
    for(lli i=0;i<n;i++){
        cin >> v[i] >> b >> c;
        a[i] = (mp(b, c));
    }
    lli cn=0;
    for(lli i=0;i<n;i++){
        for(lli j=i+1;j<n;j++){
            lli q = a[i].fi,w = a[i].se,e = a[j].fi,r = a[j].se;
            if(max(q,e)<min(w,r)){
                cn++;
            }
            else if(max(q,e)==min(w,r)){
                if(q>e&&(v[i]==1||v[i]==2)&&(v[j]==1||v[j]==3))cn++;
                if(q<e&&(v[i]==1||v[i]==3)&&(v[j]==1||v[j]==2))cn++;

            }

        }
    }
    cout<<cn<<endl;
}
}


#include <bits/stdc++.h>
// #include <atcoder/all> // NOTE: AtCoderライブラリ
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define dbg(x) cerr << #x << ": " << x << endl;
#define dbg2(x, y) cerr << #x << ": " << x << ", " << #y << ": " << y << endl;
#define dbg3(x, y, z) cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << endl;
#define nl() cout << "========================================" << endl;
using namespace std;
// using namespace atcoder; // NOTE: AtCoderライブラリ
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vl;
typedef vector<vector<long long>> vvl;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
typedef pair<int, int> pii;
const long long LINF = 1001002003004005006ll;
const int INF = 1001001001;
template<typename T> void chmin(T &a, T b) { if (a > b) a = b; }
template<typename T> void chmax(T &a, T b) { if (a < b) a = b; }
#define yn { puts("Yes"); } else{ puts("No"); } // 使い方: if (条件) yn;

int main() {
    int N; cin >> N;
    vector<int> A(N); rep(i, N) cin >> A[i];

    int res, maxcnt = 0;
    for (int i = 2; i <= 1000; i++) {
        int cnt = 0;
        for (int j = 0; j < N; j++) {
            if (A[j] % i == 0) {
                cnt++;
            }
        }

        if (cnt > maxcnt) {
            maxcnt = cnt;
            res = i;
        }
    }

    cout << res << endl;
}


#include<bits/stdc++.h>
#define rep(i,a,b)    for(int i=a;i<b;i++)
#define ret(x) return cout<<x,0;
#define rety return cout<<"YES",0;
#define retn return cout<<"NO",0;
typedef long long ll;
typedef double db;
typedef long double ld;
#define stt string
#define ve vector<ll>
#define se set<ll>
#define ar array
ll mod=1e9+7;
ll mm=998244353;
using namespace std;
ll fac[10000000];
ll gcd(ll x, ll y)
{
   if(y==0)
       return x;
   return gcd(y,x%y);
}
ll fexp(ll a,ll b,ll m){ll ans = 1;while(b){if(b&1)ans=ans*a%m; b/=2;a=a*a%m;}return ans;}
ll inverse(ll a, ll p){return fexp(a, p-2,p);}
ll ncr(ll n, ll r,ll p)
{
   if (r==0) return 1;
   return (fac[n]*inverse(fac[n-r],p)%p*inverse(fac[r],p)%p)%p;
}
// ____Z-Algorithm_____
vector<ll> za(string s)
{
  ll n = s.size();
   vector<ll> z(n);
   ll x = 0, y = 0,p=0;
          for(ll i= 1; i < n; i++)
         {
          z[i] = max(p,min(z[i-x],y-i+1));
        while(i+z[i] < n && s[z[i]] == s[i+z[i]])
             {
              x = i; y = i+z[i]; z[i]++;
             }
         }
return z;
}
int subset(ll a[],ll k)
{
   for (int i = 1; i < pow(2, k); i++)
   {
       for (int j = 0; j < k; j++)
       {
           if (i & 1 << j)
           {
              cout<<a[j]<<" ";
           }
       }
       cout<<endl;
   }
   return 1;
}
vector<ll> pr(string s)
{
   ll n = s.length();
   vector<ll> pi(n);
   for (int i = 1; i < n; i++)
   {
       int j = pi[i-1];
       while (j > 0 && s[i] != s[j])
           j = pi[j-1];
       if (s[i] == s[j])
           j++;
       pi[i] = j;
   }
   return pi;
}
//---------------------------------------------------------------------/
int main()
{
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
   ll a,b,c;
   cin>>a>>b>>c;
   if(2*a==b+c || 2*c==b+a || 2*b==a+c)
          cout<<"Yes";
    else
      cout<<"No";
}

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

using ll = long long;

void solve(long long R, long long X, long long Y){
    ll dist = (X * X + Y * Y) / (R * R);;
    if(R * R > X * X + Y * Y){
        cout<<2<<endl;
        return;
    }
    if((X * X + Y * Y) % (R * R)){
        dist++;
    }
    ll ok = 0;
    ll ng = 1e9;
    while(ng - ok > 1){
        ll mid = (ok + ng) / 2;

        if(mid * mid <= dist){
            ok = mid;
        }else{
            ng = mid;
        }
    }
    if(ok * ok != dist){
        ok++;
    }
    //cout<<dist<<endl;
    cout<<ok<<endl;
}

int main(){
    long long R;
    scanf("%lld",&R);
    long long X;
    scanf("%lld",&X);
    long long Y;
    scanf("%lld",&Y);
    solve(R, X, Y);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) REP(i, 0, n)
#define rrep(i, n) for (int i = (int)(n-1); i >= 0; i--)
#define sz(x) int(x.size())
#define bitsz(x) int(__builtin_popcount(x))
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb(x) push_back(x)
#define INF 1e9
#define LINF 1e18
template<class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; }
template < typename T > inline string toString( const T &a ) { ostringstream oss; oss << a; return oss.str(); };
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
const int di[4] = {1,0,-1,0};
const int dj[4] = {0,1,0,-1};
const int mod = 1000000007;
//const int mod = 998244353;
//cout << fixed << setprecision(15);

int main() {
    int n; cin >> n;
    ll C; cin >> C;
    map<int,ll> events;

    rep(i,n) {
        int a, b, c;
        cin >> a >> b >> c;
        events[a] += c;
        events[b+1] -= c;
    }

    ll ans = 0;
    ll s = 0;
    int pre = 0;
    for (auto event : events) {
        ans += min(C, s)*(event.first-pre);
        s += event.second;
        pre = event.first;
    }
    cout << ans << endl;
}

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

const ll MOD = 1e9 + 7;
const int INF = 1e9;
const ll LLINF = 1e18;

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; }

int main() {
  // input
  vector<string> s(3);
  rep(i, 3) { cin >> s[i]; }

  // solve
  map<char, ll> mp;
  set<char> heads;
  rep(i, 3) {
    reverse(s[i].begin(), s[i].end());
    ll co = i == 2 ? -1 : 1;
    for (char c : s[i]) {
      mp[c] += co;
      co *= 10;
    }
    reverse(s[i].begin(), s[i].end());
    heads.insert(s[i][0]);
  }

  if (10 < mp.size()) {
    cout << "UNSOLVABLE" << endl;
    return 0;
  }

  vector<int> p(10);
  iota(p.begin(), p.end(), 0);
  do {
    int i = 0;
    ll tot = 0;
    for (auto x : mp) {
      char c = x.first;
      ll co = x.second;
      if (p[i] == 0 && heads.count(c)) {
        tot = 1e18;
      }
      tot += co * p[i];
      ++i;
    }
    if (tot == 0) {
      i = 0;
      for (auto& x : mp) {
        x.second = p[i];
        ++i;
      }
      rep(i, 3) {
        ll x = 0;
        for (char c : s[i]) {
          x = x * 10 + mp[c];
        }
        cout << x << endl;
      }
      return 0;
    }
  } while (next_permutation(p.begin(), p.end()));
  cout << "UNSOLVABLE" << endl;
}


#include<bits/stdc++.h>
#define ll long long

int main()
{
    using namespace std;
    int t;
    cin>>t;
    while(t--)
    {
    	ll n;
    	cin>>n;
    	if(n==1)
    	{
    		cout<<"Odd\n";
    		continue;
    	}
    	if(n==2)
    	{
    		cout<<"Same\n";
    		continue;
    	}
        if(n%2==1)cout<<"Odd\n";
        else if(n%4==0)cout<<"Even\n";
        else if(n%4==2)cout<<"Same\n";
    }
    return 0;
}

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define fastio ios_base::sync_with_stdio(0);cin.tie(0)
#define fp(i,a,b) for(ll i=a ; i<b ; i++)
#define fn(i,a,b) for(int i=a ; i>=b ; i--)
#define ones(x) __builtin_popcount(x)
#define pb push_back
#define mk make_pair
#define ff first
#define ss second
#define all(x) x.begin(),x.end()
#define dbg(x) cout << (#x) << " = " << x << " "
#define fini cout << "\n";
#define line cout << "-----------------------------------\n";
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef tree<
ll,
null_type,
less_equal<ll>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

int read (){
    int x = 0, f = 1; char s = getchar();while (s < '0' || s > '9') {if (s == '-') f = -1; s = getchar();}
    while (s >= '0' && s <= '9') x = x * 10 + s - '0', s = getchar();
    return x * f;
}
const ll M=2e5+7;
const ll N=2e7+7;
const ll inf=1e18;
const ll mod=1e9+7;
const ll mod2=998244353;

void go(int ide){
	ll n; cin >> n;

	if (n%2) cout << "Odd\n";
	else if (n%4) cout << "Same\n";
	else cout << "Even\n";
}

int main(){
    fastio;
    int tst=1;
    cin >> tst;
    // cout << fixed << setprecision(12);
    fp(i,0,tst) go(i+1);

    return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
int h,w,m;
int f[200005];
ll ans = 0;
int of[200005];
int fw[200005];
ii p[200005];
void up(int x, int v){
    while (x < 200005){
        fw[x] += v;
        x += x&-x;
    }
}
int qu(int x){
    int res = 0;
    while (x){
        res += fw[x];
        x -= x&-x;
    }
    return res;
}
int main(){
    cin >> h>>w>>m;
    for (int i = 1; i <= h; i++){
        f[i] = w+1;
    }
    for (int i = 0; i < m; i++) {
        cin >> p[i].fi >> p[i].se;
        f[p[i].fi] = min(f[p[i].fi],p[i].se);
    }
    sort(p,p+m);
    int id = 0;
    int cur = w;
    for (int i = 1; i <= h; i++){
        while (id < m && p[id].fi == i){
            if (i == 1){
                while (cur >= p[id].se){
                    if (of[cur] == 0){
                        of[cur] = 1;
                        up(cur, 1);
                    }
                    cur--;
                }
            }
            if (of[p[id].se] == 0){
                //printf("toggling %d column\n",p[id].se);
                of[p[id].se] = 1;
                up(p[id].se,1);
            }
            id++;
        }

        int cur;
        if (of[1]) {
            cur = qu(w);
        }
        else cur = qu(w) - qu(f[i]-1);
        //printf("first = %d, %d on\n",f[i],cur);
        ans += cur;
    }
    ll ANS = (ll)h*w;
    ANS -= ans;

    printf("%lld",ANS);
}

#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define ll long long
#define ALL(a) (a).begin(),(a).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define Pii pair<int,int>
#define Pll pair<long long,long long>
#define fout(num) cout << fixed << setprecision(20) << (num) << endl
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
//vector<vector<ll>> dp(n,vector<ll>(n))
//2-dim:vector<vector<Type>> vv(n, vector<Type>(m, d));
//3-dim:vector<vector<vector<Type>>> vvv(n, vector<vector<Type>>(m, vector<Type>(l, d)));

using namespace std;
template<typename T>
struct SegmentTree{
private:
    int n;
    vector<T> node;
    T identity;
public:
    T merge(T x,T y){
        return x+y;
    }
    SegmentTree(vector<T> v,T id){
        int sz=(int)v.size();
        n=1; while(n<sz)n*=2;
        identity=id;
        node.resize(2*n-1,id);
        for(int i=0;i<sz;i++) node[i+n-1]=v[i];
        for(int i=n-2;i>=0;i--){
            node[i]=merge(node[2*i+1],node[2*i+2]);
        }
    }
    void update(int x,T val){
        x+=(n-1);
        node[x]=val;
        while(x>0){
            x=(x-1)/2;
            node[x]=merge(node[2*x+1],node[2*x+2]);
        }
    }
    T query(int a,int b,int idx=0,int l=0,int r=-1){ /* get [a,b) */
        if(r<0) r=n;
        if(r<=a||b<=l) return identity;
        if(a<=l&&r<=b) return node[idx];
        T vl=query(a,b,2*idx+1,l,(l+r)/2);
        T vr=query(a,b,2*idx+2,(l+r)/2,r);
        return merge(vl,vr);
    };
    T operator[](int k){return node[k+n-1];}
};
signed main(){
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    ll h,w,m; cin >> h >> w >> m;

    vector<ll> x(m),y(m);
    vector<ll> mix(200200,h+1),miy(200200,w+1);
    vector<ll> cnt(200200,0);
    vector<vector<ll>> G(200200);
    rep(i,m){
        cin >> x[i] >> y[i];
        chmin(miy[x[i]],y[i]);
        chmin(mix[y[i]],x[i]);
        cnt[y[i]]++;
        G[y[i]].pb(x[i]);
    }
    vector<ll> v(h,1);
    SegmentTree<ll> seg(v,0);

    ll ans = 0;
    for(int i=1;i<miy[1];i++){
        ans += (mix[i]-1);
    }
    //cout << ans << endl;
    ll hh = min(h,mix[1]);
    for(int i=1;i<=w;i++){
        if(mix[i]==h+1 && i<miy[1]) continue;
        for(auto u:G[i]){
            seg.update(u-1,0);
        }
        if(i<miy[1]){
            ans += seg.query(mix[i],hh);
            //cout << seg.query(mix[i],hh) << endl;
        }
        else{
            ans += seg.query(0,hh);
            //cout << seg.query(0,hh) << endl;
        }
    }
    cout << ans << endl;

}
// g++ main.cpp -o a.out && ./a.out


#include <bits/stdc++.h>
#define fastio()                  \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0)
#define pb push_back
#define show(x) cout << (#x) << " : " << x << endl;
#define ll long long
#define ull unsigned long long
#define ld long double
#define pow power
#define mp make_pair
#define ff first
#define ss second
#define pii pair<ll, ll>
#define sq(x) ((x) * (x))
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define siz(a) int((a).size())
#define For(i,a,b) for(int (i)=(a);(i) < (b); ++(i))
#define endl "\n"
#define pi 3.14159265
const ll mod = 1000 * 1000 * 1000 + 7;
const ll mod1 = 998244353;
const ll INF     = 1ll*1000*1000*1000*1000*1000*1000 + 7;
using namespace std;

ll power(ll x, ll y)
{
   ll res = 1;

    while (y > 0)
    {

        if (y & 1)
            res = (long long)(res*x);

        y = y>>1;

        x = (long long)(x*x);

        //cout<<x<<'\n';
    }
    return res;
}

// Stolen Templates
template <typename T>
T MIN(T first) { return first; }

template <typename T, typename... Args>
T MIN(T first, Args... args) { return min(first, MIN(args...)); }

template <typename T>
T MAX(T first) { return first; }

template <typename T, typename... Args>

T MAX(T first, Args... args) { return max(first, MAX(args...)); }
// Stolen Templates end here

int main()
{

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

fastio();

    ll n,s,d;
    cin>>n>>s>>d;

    For(i,0,n) {
        ll x,y;
        cin>>x>>y;
        if (x<s and y>d) {
            cout<<"Yes";
            return 0;
        }
    }

    cout<<"No";

return 0;
}

// check all product based operations for integer overflow
// careful of renamed variables especially in loops

#include<iostream>
#define int long long
using namespace std;

signed main()
{
    int n,s,d;
    cin>>n>>s>>d;
    bool flag=0;
    for(int i=0;i<n;i++)
    {
    	int j,k;
    	cin>>j>>k;
    	if(j<s&&k>d)
    	{
    		flag=1;
    	}
    }
    if(flag)
    	cout<<"Yes";
    else
    	cout<<"No";
	return 0;
}


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

#define FASTio ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ll long long int
#define all(v) (v).begin(), (v).end()
#define getunique(v) {sort(all(v)); v.erase(unique(all(v)), v.end());}
#define printarr(v) {for(auto x : v) cout<<x<<" ";cout<<"\n";}
#define mp make_pair
#define pb push_back
#define pf push_front
#define ff first
#define ss second
#define lcm(a, b) ((a) * (b)) / __gcd(a, b)
#define umpit unordered_map<ll,ll>::iterator
#define mpit map<ll,ll>::iterator
#define setit set<ll>::iterator
#define mx(a) *max_element(all(a))
#define mn(a) *min_element(all(a))
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
const ll INF = LLONG_MAX / 2;
#define PI 3.1415926535897932384626433832795
#define mod 1000000007

int main()
{
   FASTio;
    ll t=1,i,j;
    //cin>>t;
    while(t--)
    {
        ll n;
        cin>>n;
        ll k = 1.08*n;
        if(k<206)
            cout<<"Yay!\n";
        else if(k == 206)
            cout<<"so-so\n";
        else
            cout<<":(\n";
    }
    return 0;
}

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

int main() {
  int n; cin >> n;
  cout << ((floor(n*1.08)<206)? "Yay!": ((floor(n*1.08)==206)? "so-so": ":(")) << endl;
}


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

typedef long long ll;

typedef pair <int, int> pii;
typedef pair <ll,ll> pll;

typedef vector <int> vint;
typedef vector <ll> vll;
typedef vector <pii> vpii;
typedef vector <pll> vpll;
typedef vector <string> vs;
typedef priority_queue <int> pqi;

#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 trav(x, a) for(auto& x : a)

#define pb push_back
#define mkp make_pair
#define vbb vector <bool>
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()

const int mod = 1000000007;
const long long inf = 1e18;
const char nl = '\n';

void solve_it(){
    vint a(3);
    F0R(i, 3) cin>>a[i];
    sort(all(a));
    cout<<a[3 - 1] + a[3 - 2];
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    solve_it();
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define rep(i,x,y) for(int i=(int)(x);i<(int)(y);i++)
#define print(A,x,n) rep(i,0,n){cout<<(i ? " ":"")<<A[i]x;}cout<<endl;
#define pprint(A,y,m,n) rep(j,0,m){print(A[j],y,n);}
const long mod=1e9+7;
const int siz=3e5;
const int inf=1e9;
int main(){
	int N; cin>>N;
	pair<long,int> vx[N], vy[N];
	long x[N], y[N];
	rep(i,0,N){
		cin>>x[i]>>y[i];
		vx[i] = {x[i], i};
		vy[i] = {y[i], i};
	}
	sort(vx, vx+N); sort(vy, vy+N);
	set<int> id;
	id.insert(vx[0].second);
	id.insert(vx[1].second);
	id.insert(vx[N-2].second);
	id.insert(vx[N-1].second);
	id.insert(vy[0].second);
	id.insert(vy[1].second);
	id.insert(vy[N-2].second);
	id.insert(vy[N-1].second);

	vector<long> dist;
	for(int v:id)for(int u:id){
		dist.push_back(max(abs(x[u] - x[v]), abs(y[u] - y[v])));
	}
	sort(dist.begin(), dist.end(), greater<long>());
	cout<<dist[2]<<endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
#define REP(i,n) for(ll i=0;i<(n);i++)
#define ALL(v) v.begin(),v.end()
constexpr ll MOD=998244353;
constexpr ll INF=2e18;

int main(){
    int n; cin >> n; n*=2;
    VI a(n); REP(i,n) cin >> a[i];
    vector<P> p(0);
    REP(i,n){
        p.push_back({a[i],i});
    }
    sort(ALL(p));
    VI f(n,0);
    int x=0;
    REP(i,n){
        if(x<n/2){
            f[p[i].second]=1;
            x++;
        }
    }
    vector<char> ans(n);
    stack<int> st;
    REP(i,n){
        if(st.size()==0){
            st.push(i);
        }
        else{
            if(f[st.top()]==f[i]){
                st.push(i);
            }
            else{
                ans[st.top()]='(';
                ans[i]=')';
                st.pop();
            }
        }
    }
    for(auto c:ans){
        cout << c;
    }
    cout << endl;
}

#include <iostream>

long long dp[201][12];

long long nCr(long long n, long long r) {
	if (r == n)
		return dp[n][r] = 1;
	if (r == 1)
		return dp[n][r] = n;
	if (r == 0)
		return dp[n][r] = 1;
	if (dp[n][r] == 0)
		return dp[n][r] = nCr(n-1, r) + nCr(n-1, r-1);
	else
		return dp[n][r];
}

int main() {
	int l;
	for (int i = 0; i < 201; i++) {
		for (int j = 0; j < 13; j++)
			dp[i][j] = 0;
	}
	std::cin >> l;
	std::cout << nCr(l-1, 11);
	return 0;
}


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

int main() {
  int a, b, c;
  cin >> a >> b >> c;

  if (a != b && b != c && c != a) cout << 0 << '\n';
  else cout << (a ^ b ^ c) << '\n';
}

#include "bits/stdc++.h"
#include <stdio.h>
#include <string.h>
#include<iostream>
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;
#define KP                            \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);
#define ll long long int
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define inp(a, n) rep(i, 0, n) cin >> a[i];
#define out(a, n) rep(i, 0, n) cout << a[i] << " ";
#define MIN -1e9
#define MAX 1e9
#define endl "\n"
#define vi vector<ll>
#define pb push_back
#define all(v) v.begin(), v.end()
#define output(x) cout << (x ? "Yes" : "No") << endl;
bool sortbysecdesc(const pair<ll, ll> &a, const pair<ll, ll> &b)
{
    return a.second > b.second;
}
int binpow(int a, int n)
{
    // // normal recurcive

    // if (n == 0)
    //     return 1;
    // if (n % 2 == 1)
    //     return binpow(a, n - 1) * a;
    // else
    // {
    //     int b = binpow(a, n / 2);
    //     return b * b;
    // }
    // //iterative

    // int res = 1;
    // while (n)
    //     if (n & 1)
    //     {
    //         res *= a;
    //         --n;
    //     }
    //     else
    //     {
    //         a *= a;
    //         n >>= 1;
    //     }
    // return res;

    // optimised iterative
    int res = 1;
    while (n)
    {
        if (n & 1)
            res *= a;
        a *= a;
        n >>= 1;
    }
    return res;
}
void solve()
{
    ll a,b,c;
    cin >> a>>b>>c;
    if(a==b){
        cout << c<<endl;
        return;
    }
    if(b==c){
        cout << a<<endl;
        return;
    }
    if(a==c){
        cout << b<<endl;
        return;
    }
    cout << "0" << endl;

}

int main()
{
    KP;
    // #ifndef ONLINE_JUDGE
    // 	freopen("input.txt", "r", stdin);
    // 	freopen("output.txt", "w", stdout);
    // #endif
    ll tt=1;
    //cin>>tt;
    // ll tt=1;
    //  cin >> tt;
    //  string s;
    //  cin >> s;
    while (tt--)
        solve();

    // cout << "\n\n"
    //      << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " seconds.\n";
    // cout << tt + tt*tt + 12432345 << " " << s<<endl<<tt*tt*tt<<endl ;

    return 0;
}


#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for (long long i = (a); i < (long long)(b); i++)
#define IFOR(i, a, b) for (long long i = (a); i <= (long long)(b); i++)
using namespace std;
using VL = vector<long long>;
using ll = int64_t;
ll mod(ll x,ll m){ll y=x%m;return (y>=0LL)?y:y+m;}
template<class T>bool chmin(T& a, T b){if(a>b){a=b;return true;}else return false;}
ll gcd(ll a,ll b,ll& x,ll& y)
{if(a){ll d=gcd(b%a,a,x,y);y-=(b/a)*x;swap(x,y);if(d<0LL){d=-d;x=-x;y=-y;}return d;}else{x=0;y=1;return b;}}

ll B(ll X, ll Y, ll n){
    return 2LL*(X+Y)*n+X;
}

ll W(ll P, ll Q, ll n){
    return (P+Q)*n+P;
}

int main(){
    int T;
    cin>>T;
    string ans = "";
    REP(i,T){
        ll t = LLONG_MAX;
        ll X,Y,P,Q;
        cin>>X>>Y>>P>>Q;
        ll x, y;
        ll d = gcd(2LL*(X+Y), -(P+Q), x, y);
        ll u = (P+Q)/d, v = 2LL*(X+Y)/d;
        IFOR(z, P-X, P-X+Q-1LL){
            if(mod(z,d)!=0LL)continue;
            ll n = mod(z/d*x,u);
            chmin(t,B(X,Y,n));
        }
        IFOR(z, P-X-Y+1LL, P-X){
            if(mod(z,d)!=0LL)continue;
            ll m = mod(z/d*y,v);
            chmin(t, W(P,Q,m));
        }
        if(t==LLONG_MAX){
            ans += "infinity\n";
        }else{
            ans += to_string(t) + "\n";
        }
    }

    cout << ans;

    return 0;
}

#include <bits/stdc++.h>
#define rei register int
#define int signed long long
#pragma GCC optimize(2)
using namespace std;
int T,X,Y,P,Q;
int ans,cnt;
void exgcd(int &x,int &y,int a,int b)
{
    if(!b)
    {
        x=1;
        y=0;
        return;
    }
    exgcd(x,y,b,a%b);
    int t=x;
    x=y;
    y=t-a/b*y;
} int gcd(int x,int y){
	return !y ? x : gcd(y,x % y);
} int mul(int x,int y,int mod){
	int res = 0;
	while(y){
		if(y & 1) res = (res + x) % mod;
		x = 2 * x % mod; y >>= 1;
	} return res;
}
void solve(int d1,int mod1,int d2,int mod2){
	int rec = gcd(mod1,mod2);
	if((d2 - d1) % rec != 0) return;
	int LCM = mod1 * mod2 / gcd(mod1,mod2);
	int rate = (d2 - d1) / gcd(mod1,mod2);
	int sol1,sol2; exgcd(sol1,sol2,mod1,mod2);
	sol1 = mul(sol1,(rate % LCM + LCM) % LCM,LCM);
	int res = ((mul(sol1,mod1,LCM) + d1) % LCM + LCM) % LCM;
	ans = min(ans,res);
}
signed main(){
	cin >> T; while(T--){
		cin >> X >> Y >> P >> Q;
		ans = (long long)9e18;
		for(rei i = X;i <= X + Y - 1;++i)
		 for(rei j = P;j <= P + Q - 1;++j)
			 solve(i,2 * X + 2 * Y,j,P + Q);
		if(ans == (long long)9e18) cout << "infinity" << endl; else cout << ans << endl;
	}  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
int helper(int l, int r)
{

	int j= r - (2*l);
	int temp = j*(j+1);
	temp = temp/2;
	temp = temp + j;
	temp = temp + 1;
	return temp;
}
void solve(){

	int l,r;
	cin>>l>>r;

    if(2*l <= r)
    cout<<helper(l,r)<<endl;
    else
    cout<<0<<endl;

}

int32_t main(){

	int t;
	cin>>t;

	while(t--){
		solve();
 }

	return 0;
}

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

vector<ll> solve(ll t, vector<ll> l, vector<ll> r) {
    vector<ll> res(t);

    for(ll i = 0ll; i < t; i++) {
        //cout << t << " -> " << i  << endl;
        ll count = max(r[i] - 2ll * l[i] + 1ll, 0ll);
        //cout << "count: " << count << endl;
        res[i] = count * (count + 1ll) / 2ll;
    }
    return res;
}
int main() {
    ll t;
    cin >> t;
    vector<ll> l(t), r(t);
    for(int i = 0; i < t; i++) {
        cin >> l[i] >> r[i];
    }
    vector<ll> res =  solve(t, l, r);
    for(ll i=0ll;i<t; i++) {
        cout << res[i] << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long

const ll p = 1000000009;
const ll nax = 200002;

int par[nax];

int pr(int v) {
	if (par[v] == v) return v;
	return par[v] = pr(par[v]);
}

int main() {

	for (int i = 0; i < nax; ++i) {
		par[i] = i;
	}
	int n;
	cin >> n;
	vector<int> v(n);
	for (auto& x : v) cin >> x;

	int ans = 0;

	for (int i = 0; i < n; ++i) {
		int a = pr(v[i]), b = pr(v[n - 1 - i]);
		if (a == b) continue;
		ans++;
		par[a] = b;
	}
	cout << ans;
}

#ifdef MYDEBUG
#include "pch.h"
#else
#include <bits/stdc++.h>
#define debug(...)
#endif
using namespace std;

#define int long long
#define endl '\n'

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

typedef vector<int> vi;
typedef map<int, int> mii;
typedef pair<int, int> pii;

template<typename T1, typename T2>istream& operator>>(istream& in, pair<T1, T2>& a) { return in >> a.first >> a.second; }
template<typename T1, typename T2>ostream& operator<<(ostream& out, pair<T1, T2> a) { return out << a.first << " " << a.second;}

template<typename T>void print(T t) { cout << t; }
template<typename T, typename... Args>void print(T t, Args... args) { cout << t << " "; print(args...); }
template<typename... Args>void printl(Args... args) { print(args...); cout << endl; }
template<typename T>void printa(T& a, bool ln = 0) { for (auto& e : a) print(e, ln?"\n":""); if (!ln) cout << endl; }

#define printr(...) { printl(__VA_ARGS__); return; }
#define inputa(a) for (auto& e : a) cin >> e

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int RAND(int lo = 0, int hi = LLONG_MAX) { return uniform_int_distribution<int>(lo, hi)(rng); }

void google() { static int __gtest__ = 1; cout << "Case #" << __gtest__++ << ": "; }

const double pi = 3.141592653589793;

signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cout << setprecision(15) << fixed;
	int t = 1;
	//cin >> t;
	void code();
	while (t--) code();
}

const int mod = 1e9 + 7;
const int mod1 = 998244353;
const int maxn = 2e5 + 1;

vi ar[maxn];
bool vis[maxn];

int dfs(int nd)
{
	vis[nd] = 1;
	int cc = 0;
	for (auto& ch : ar[nd])
	{
		if (vis[ch]) continue;
		cc += dfs(ch);
	}
	return cc + 1;
}

void  code()
{
	int n;
	cin >> n;
	vi a(n);
	inputa(a);

	for (size_t i = 0; i < n/2; i++)
	{
		if (a[i] == a[n - 1 - i]) continue;
		ar[a[i]].push_back(a[n - 1 - i]);
		ar[a[n - 1 - i]].push_back(a[i]);
	}

	int cc = 0;
	for (size_t i = 1; i < maxn; i++)
	{
		if (vis[i]) continue;
		cc += dfs(i) - 1;
	}

	printl(cc);
}


#include<algorithm> // min, max, sort,
#include<iostream>
#include<iomanip>   // fixed+setprecision,
#include<cstdint>   // int64_t,
#include<vector>
#include<map>

using namespace std;

const static int64_t MOD = 1000000007;

int main()
{
    int64_t a, b, c, d;

    cin>>a>>b;
    cin>>c>>d;

    cout<<b-c<<endl;

    return 0;
}

#include <iostream>
using namespace std;

int main(){
    int N, S, D;
    cin >> N >> S >> D;

    int *spell_time = new int[N];
    int *spell_damage = new int[N];
    for(int i = 0; i < N; i++){
        cin >> spell_time[i] >> spell_damage[i];
    }

    bool result = false;

    for(int i = 0; i < N; i++){
        if((spell_time[i] < S) && (spell_damage[i] > D)){
            result = true;
            break;
        }
    }

    cout << (result ? "Yes" : "No") << endl;

}


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

#define   pf printf
#define   sc scanf
#define   nline cout << '\n'
#define   dbg(x) \
          cout << "debug " << #x << ": " << x << '\n';
#define   inp(t) cin >> (t)
#define   out(t) cout << (t)
#define   FOR(n,x) for(ll i = n; i < x; i++)
#define   FOR2(N,X) for(ll j = N; j < X; j++)
#define   wh(n) while(n--)
ll        fib[52] = {};
bool      isPrime[50];

void OJ(){
    // file I/O
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
}

ll LCM(ll x, ll y){ return x * (y/__gcd(x,y)); }

// string addition
string sum(string num1,string num2){
    int len1 = num1.length();
    int len2 = num2.length();
    string ans;
    bool end1 = false, end2 = false;
    int i = 0, carry = 0;

    // will break if num1 and num2 ends
    for( ;!end1 || !end2; ){
        // iterating in reverse order
        if(len1) len1--;
        if(len2) len2--;

        // adding num1 and num2
        int temp = (num1[len1]-48) + (num2[len2] - 48);
        temp += carry;

        carry = temp / 10;
        ans.push_back((temp % 10)+48);
        i++;

        // initializing to 0 if num1 or num2 ends
        if(!len1){ end1 = true; num1[len1] = '0'; }
        if(!len2){ end2 = true; num2[len2] = '0'; }
    }

    if(carry) ans.push_back(carry+48);
    reverse(ans.begin(), ans.end());
    return ans;
}

//str multiplication of a*b
string strMultiplication(string a, string b){
    string ans = "0";
    string postZeros = "";

    for(int i = b.length()-1; i >= 0; i--){
        int carry = 0;
        string current;

        if(b[i] == '0')
            current.push_back('0');
        else
            for(int j = a.length()-1; j >= 0; j--){
                int S1_j = a[j] - 48;
                int S2_i = b[i] - 48;
                int temp = (S1_j * S2_i) + carry;
                carry = temp/10;
                // if(current != "0")
                    current.push_back((char)((temp%10) + 48));
            }
        if(carry) current.push_back(carry+48);
        reverse(current.begin(), current.end());
        current += postZeros;
        ans = sum(ans,current);

        postZeros += "0";
    }

    return ans;
}

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

    // Start
    ll n,m,t; inp(n) >> m >> t;
    bool flag = true;
    ll charge = n, last = 0;

    FOR(0,m){
        ll a,b; inp(a) >> b;
        charge = charge - (a - last);
        if(charge <= 0)
            flag = false;
        charge = charge + (b - a);
        last = b;
        if(charge > n)
            charge = n;
    }
    charge = charge - (t - last);
    if(flag && charge > 0)
        out("Yes");
    else
        out("No");
    nline;
}

// g++ F.cpp -std=c++14 -I . && ./a.out
#include <bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;
// using mint = modint1000000007;
// using mint = modint998244353;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, a, b) for (int i = a; i < (int)(b); i++)
#define rrep(i, a, b) for (int i = a; i > (int)(b); i--)
#define all(v) v.begin(), v.end()
using ll = long long;
const ll INF = 1e18;
// 変数定義
int N, M, Q, a, b, c, d, x, y, t, T, total, ans;

int main()
{
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> N >> M;
  vector<vector<int>> adj(N, vector<int>(N, 0));
  rep(i, M)
  {
    cin >> a >> b;
    adj[a - 1][b - 1] = 1;
    adj[b - 1][a - 1] = 1;
  }
  vector<int> dp(1 << N, N);
  dp[0] = 0;
  rep(S, 1 << N)
  {
    bool flag = true;
    vector<int> lis;
    rep(i, N)
    {
      if ((S >> i) % 2)
        lis.push_back(i);
    }
    for (auto p : lis)
    {
      for (auto q : lis)
      {
        if (p == q)
          break;
        if (adj[p][q] == 0)
        {
          flag = false;
          goto last;
        }
      }
    }
  last:
    if (flag == true)
      dp[S] = 1;
    else
    {
      T = (S - 1) & S;
      while (T > 0)
      {
        dp[S] = min(dp[S], dp[T] + dp[S - T]);
        T = (T - 1) & S;
      }
    }
  }
  cout << dp[(1 << N) - 1] << '\n';
  return 0;
}


#include <bits/stdc++.h>
#if __has_include(<atcoder/all>)
    #include <atcoder/all>
    using namespace atcoder;
#endif

using namespace std;
using Graph = vector<vector<int>>;
using Grid= vector<string>;
using vin= vector<int>;
using ll=long long;
using ull=unsigned long long;
using vll= vector<ll>;
using vbl=vector<bool>;
using vch=vector<char>;
using P=pair<int ,int>;
//const long double pi=acos(-1);
#define ft first
#define sd second
#define fn front
#define pb push_back
#define eb emplace_back
#define it insert
#define si(v) int((v).size())
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rell(i,n) for (ll i=0; i< (ll)(n); i++)
#define sot(x) sort(x.begin(), x.end())
#define rese(x) reverse(x.begin(), x.end())
#define vnn(x,y,s,name) vector<vector<int>> name(x, vector<int>(y,s))
#define mse(x) memset(x, 0, sizeof(x))
#define mii(x,y,z) min(x,min(y,z))
#define maa(x,y,z) max(x,max(y,z))
#define cps CLOCKS_PER_SEC
#define yes cout<<"Yes"<<"\n"
#define no cout<<"No"<<"\n"
#define cset(x) cout<<fixed<<setprecision(x)
//const int INF=1001001001;
//998244353  1000000007

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n;
    cin>>n;
    vector<long long> ax(n);
    long long as=0;
    rep(i,n){
        ll a,b;
        cin>>a>>b;
        as-=a;
        ax[i]=2*a+b;
    }
    sot(ax);
    rese(ax);
    int cnt=0;
    while(as<=0){
        as+=ax[cnt];
        cnt++;
    }
    cout<<cnt<<endl;
}

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define pi pair<int, int>
#define endl '\n'
#define all(v) (v).begin(), (v).end()
#define FIO ios_base::sync_with_stdio(0), cin.tie(0)
#define fi first
#define se second

signed main() {
    FIO;
    int n;
    cin >> n;
    set<pair<int, string>> v;
    for (int i = 0; i < n; i++) {
        string s;
        int t;
        cin >> s >> t;
        v.insert({-t, s});
    }
    auto it = v.begin();
    it++;
    cout << ((*it).se);
}


#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<map>
#include<cmath>
#include<string>

#define rep(i,p) for(long long int i=0;i<p;i++)
#define reep(i,p) for(long long int i=1;i<=p;i++)
#define ll long long

using namespace std;

int main(){

ll int N;
cin >> N;

vector<ll int> a;
ll int keta;
ll int wa;
while(N!=0){
	a.push_back(N%10);
	wa+=N%10;
	N/=10;
}
keta = a.size();

ll int ans=100;
ll int kari=0;
ll int kariwa;

	for(ll int j=0;j<(1<<keta);j++){
		kariwa=wa;
		kari=0;
		for(ll int i=0;i<keta;i++){
			if(j>>i&1){
				kariwa-=a[i];
				kari+=1;
			}
			else{}
		}
		if(kariwa%3==0 && kariwa!=0){
			if(kari<ans){
				ans=kari;
			}
			else{}
		}
		else{}
	}
if(ans==100){
	cout << "-1";
}
else{
	cout << ans;
}

	return 0;

}

#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
using namespace std;

#define REP(i, n) for(int i = 0; i < (int)(n); ++i)
#define FOR(i, m, n) for(int i = (int)(m); i < (int)(n); ++i)

void recursive_comb(int *indexes, int s, int rest, std::function<void(int *)> f) {
  if (rest == 0) {
    f(indexes);
  } else {
    if (s < 0) return;
    recursive_comb(indexes, s - 1, rest, f);
    indexes[rest - 1] = s;
    recursive_comb(indexes, s - 1, rest - 1, f);
  }
}

// nCkの組み合わせに対して処理を実行する
void foreach_comb(int n, int k, std::function<void(int *)> f) {
  int indexes[k];
  recursive_comb(indexes, n - 1, k, f);
}

int main()
{
    string S;
    cin >> S;

    vector<int> digit;
    for(auto s: S){
        int n = s-'0';
        digit.push_back(n);
    }

    int ans = -1;
    FOR(k, 1, digit.size()+1){
        foreach_comb(digit.size(), k, [&](int *indexes){
            int s = 0;
            REP(i, k) s += digit[indexes[i]];
            if(s%3 == 0) ans = int(S.size())-k;
        });
    }
    cout << ans << endl;

    return 0;
}


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

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vll = vector<ll>;
using vc = vector<char>;
using vcc = vector<vc>;
using Pii = pair<int,int>;
using ld = long double;

int main(){
    int h,w,x,y;
    cin >> h >> w >> x >> y;
    vvi V(200, vi(200,0));
    for(int i = 0; i<h; i++){
        for(int j = 0; j<w; j++){
            char c;
            cin >> c;
            if(c=='#'){
                V[i][j] = 1;
            }
        }
    }
    int ans = 0;
    for(int i = x-2; i>=0; i--){
        if(V[i][y-1]){
            break;
        }
        else{
            ans++;
        }
    }
    for(int i = x; i<h; i++){
        if(V[i][y-1]){
            break;
        }
        else{
            ans++;
        }
    }
    for(int j = y-2; j>=0; j--){
        if(V[x-1][j]){
            break;
        }
        else{
            ans++;
        }
    }
    for(int j = y; j<w; j++){
        if(V[x-1][j]){
            break;
        }
        else{
            ans++;
        }
    }
    cout << ans+1 << endl;
}

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < n; i++)
#define Rep(i,n) for(int i = 1; i <= n; i++)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define debug(a) { cerr << #a << ':' << a << endl; }
#define endl '\n'
#define fi first
#define se second
using lint = long long;
using P = pair<int,int>;
template<class T> using V = vector<T>;
template<class T> using priq     = priority_queue<T>;
template<class T> using priq_inv = priority_queue<T, vector<T>, greater<T>>;
const int dx[] = {0,1,0,-1,1,1,-1,-1};
const int dy[] = {1,0,-1,0,1,-1,-1,1};
template<class T> T ceil(const T &a, const T &b) { return (a+b-1)/b; }
template<class T> bool chmin(T &a, T b) { if(a > b) { a = b; return 1; } return 0; }
template<class T> bool chmax(T &a, T b) { if(a < b) { a = b; return 1; } return 0; }
struct INF { template<class T> operator T() { return numeric_limits<T>::max() / 2; } } INF;

template<class T, class U> istream& operator>>(istream &in, pair<T,U> &p) {
    return in >> p.first >> p.second;
}
template<class T, class U> ostream& operator<<(ostream &out, const pair<T,U> &p) {
    return out << "{ " << p.first << ',' << p.second << " } ";
}
template<class T> istream& operator>>(istream &in, vector<T> &v) {
    for(auto &&e: v) in >> e;
    return in;
}
template<class T> ostream& operator<<(ostream &out, const vector<T> &v) {
    for(const auto &e: v) out << e << ' ';
    return out;
}

/*----------------------------------------------------------------------------------------------------*/

int n, m;
V<string> s;

// fstream out("AHC004_out.txt");

int main() {

    clock_t start = clock();

    cin >> n >> m;
    s.resize(m);
    cin >> s;

    V<P> cnts(m); // pair = (count, index);
    V<V<int>> ins(m);

    rep(i,m) {
        int cnt = 0;
        rep(j,m) {
            bool isin = ( s[i].find(s[j]) != string::npos );
            if(isin) {
                cnt++;
                ins[i].push_back(j);
            }
        }
        cnts[i] = make_pair(cnt,i);
    }
    sort(rall(cnts));

    V<bool> used(m);
    V<string> ans(n);

    rep(i,n) { // 初期解生成
        string str = "";
        rep(j,m) {
            int x = cnts[j].se;
            if(used[x]) continue;
            string plus = s[x];
            for(int k = min(sz(str), sz(s[x])); k >= 0; k--) {
                if( str.substr(sz(str)-k,k) == s[x].substr(0,k) ) {
                    plus = s[x].substr(k,sz(s[x])-k);
                    break;
                }
            }
            if(sz(str) + sz(plus) > n) continue;
            str += plus;
            for(int y: ins[x]) used[y] = true;
        }
        str.resize(n,'.');
        ans[i] = str;
    }

    V<int> v(n); iota(all(v), 0);
    pair<int,V<int>> mx = make_pair(0,v);

    random_device seed_gen;
    mt19937 engine(seed_gen());

    while(clock() - start < 2980) {
        V<bool> nused = used;
        int cnt = 0;
        rep(i,n) {
            string ns = "";
            rep(j,n) ns += string(1,ans[v[i]][j]);
            ns += ns;
            rep(j,m) {
                if(nused[j]) continue;
                if( ns.find(s[j]) != string::npos ) {
                    cnt++;
                    nused[j] = true;
                }
            }
        }
        chmax(mx, make_pair(cnt, v));
        shuffle(all(v), engine);
    }

    rep(i,n) {
        cout << ans[mx.se[i]] << endl;
    }
}


#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <functional>
#include <cassert>
#include <stack>
#include <numeric>
#include <iomanip>
#include <limits>
#include <random>
#include <unordered_set>
#include <chrono>

typedef int64_t ll;
typedef std::pair<int, int> Pii;
typedef std::pair<ll, ll> Pll;
typedef std::pair<double, double> Pdd;

#define rip(_i, _n, _s) for (int _i = (_s); _i < (int)(_n); _i++)
#define all(_l) _l.begin(), _l.end()
#define rall(_l) _l.rbegin(), _l.rend()
#define MM << " " <<

template<typename _T>
using MaxHeap = std::priority_queue<_T>;
template<typename _T>
using MinHeap = std::priority_queue<_T, std::vector<_T>, std::greater<_T>>;

template<typename _T>
inline bool chmax(_T &_l, const _T _b) {
    if (_l < _b) {
        _l = _b;
        return true;
    }
    return false;
}
template<typename _T>
inline bool chmin(_T &_l, const _T _b) {
    if (_l > _b) {
        _l = _b;
        return true;
    }
    return false;
}

template<typename _T>
void vdeb(const std::vector<_T> &bb) {
    for (unsigned int i = 0;i < bb.size();i++) {
        if (i == bb.size() - 1) std::cout << bb[i];
        else std::cout << bb[i] << ' ';
    }
    std::cout << '\n';
}
template<typename _T>
void vdeb(const std::vector<std::vector<_T>> &bb) {
    for (unsigned int i = 0;i < bb.size();i++) {
        // std::cout << i << ' ';
        vdeb(bb[i]);
    }
    std::cout << '\n';
}

using namespace std;

int main() {
    int a, b, c; cin >> a >> b >> c;
    set<int> st = {a, b, c};
    if(st.size() < 3) {
        cout << (a^b^c) << endl;
    }
    else {
        cout << 0 << endl;
    }
}

#include<iostream>
using namespace std;

int main() {
	int a, b, c;
	cin >> a >> b >> c;
	if (a != b && a != c && b != c) cout << 0 << endl;
	else {
		if (a == b) {
			cout << c << endl;
			return 0;
		}
		if (a == c) {
			cout << b << endl;
			return 0;
		}
		if (c == b) {
			cout << a << endl;
			return 0;
		}
	}
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) (v).begin(), (v).end()
using ll = long long;
constexpr int INF = 1e9;
constexpr long long LINF = 1e18;
constexpr long long MOD = 1e9 + 7;

signed main() {
    int n, m;
    cin >> n >> m;
    int a[m], b[m];
    rep(i, m) {
        cin >> a[i] >> b[i];
        a[i]--;
        b[i]--;
    }
    int dp[1 << n] = {};
    fill(dp, dp + (1 << n), INF);
    for (int bit = 1; bit < 1 << n; bit++) {
        int bp = __builtin_popcount(bit);
        int cnt = 0;
        rep(i, m) {
            if ((bit & 1 << a[i]) and (bit & 1 << b[i])) cnt++;
        }
        if (cnt == bp * (bp - 1) / 2) dp[bit] = 1;
    }
    for (int S = 0; S < 1 << n; S++) {
        for (int T = S;; T = (T - 1) & S) {
            if (T == 0) break;
            dp[S] = min(dp[S], dp[T] + dp[S ^ T]);
        }
    }
    cout << dp[(1 << n) - 1] << endl;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
const int MAXN=1<<18,INF=-0x3f3f3f3f3f;
int N,F[MAXN],G[MAXN],U,Con[MAXN],IsCon[MAXN];
inline int lowbit(int n) { return n&(-n); }
inline bool iscon(int b,int s){
    if(G[s]!=1) return 0;
    while(s){
        if(!Con[b|lowbit(s)]) return 0;
        s^=lowbit(s);
    }
    return 1;
}
vector<int> Alw;
inline void dp(int S){
    if( S==lowbit(S)||iscon(lowbit(S),S^lowbit(S)) ){
        F[S]=G[S]=1;
        Alw.push_back(S);
        return ;
    }
    for(auto s : Alw)
        if((S&s)==s){
            F[S]=min(F[S],F[S^s]+1);
            if(F[S]==2) return ;
        }
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    for(int i=0;i<MAXN;i++) F[i]=G[i]=-INF;
    int M;
    cin>>N>>M;
    U=(1<<N)-1;
    for(int i=1;i<=M;i++){
        int a,b;
        cin>>a>>b;
        Con[(1<<a-1)|(1<<b-1)]=1;
    }
    for(int i=1;i<=U;i++) dp(i);
    cout<<F[U]<<endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <queue>
#include <bitset>
#include <stack>
#include <functional>

// AtCoder
// #include <atcoder/all>
// using namespace atcoder;

#ifdef LOCAL
    #define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
    #define eprintf(...)
#endif

#define rep_(i, a_, b_, a, b, ...) for (int i = (a), i##_len = (b); i < i##_len; ++i)
#define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define reprev_(i, a_, b_, a, b, ...) for (int i = (b)-1, i##_min = (a); i >= i##_min; --i)
#define reprev(i, ...) reprev_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define all(x) (x).begin(), (x).end()
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; }
#define fls(x) (64 - __builtin_clzll(x))
#define pcnt(x) __builtin_popcountll(x)
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair <int,int> P;
typedef long double ld;

int main (void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int t; cin >> t;
    rep (_, t) {
        int n; cin >> n;
        vector<int> p(n), prev(n);
        rep (i, n) {
            cin >> p[i]; --p[i];
            prev[p[i]] = i;
        }
        vector<int> res;
        int piv = 0, cnt = 0;
        while (piv < n) {
        // rep (x, 10) {
            eprintf("piv=%d cnt=%d : ", piv, cnt);
            rep (i, n) eprintf("%d ", p[i]);
            if (prev[piv] == piv) {
                eprintf("A\n");
                ++piv;
                continue;
            }
            int pivplace = prev[piv];
            if ((pivplace + 1) % 2 == cnt % 2) {
                eprintf("B\n");
                swap(p[pivplace - 1], p[pivplace]);
                swap(prev[p[pivplace - 1]], prev[p[pivplace]]);
                ++cnt;
                res.push_back(pivplace);
            } else {
                if (p[pivplace - 2] > piv) {
                    eprintf("C\n");
                    swap(p[pivplace - 2], p[pivplace - 1]);
                    swap(prev[p[pivplace - 2]], prev[p[pivplace - 1]]);
                    ++cnt;
                    res.push_back(pivplace - 1);
                } else {
                    eprintf("D\n");
                    swap(p[pivplace - 1], p[pivplace]);
                    swap(prev[p[pivplace - 1]], prev[p[pivplace]]);
                    cnt += 5;
                    res.push_back(pivplace - 1);
                    res.push_back(pivplace);
                    res.push_back(pivplace - 1);
                    res.push_back(pivplace);
                    res.push_back(pivplace - 1);
                }
            }
        }
        cout << res.size() << '\n';
        rep (i, res.size()) cout << res[i] << " \n"[i + 1 == i_len];
    }
    return 0;
}

//AnkitCode99 here....
//every ups and downs matter!

#include<bits/stdc++.h>
#define endl "\n"
#define IOS ios_base::sync_with_stdio(0);cin.tie(nullptr);cout.tie(nullptr)
typedef long long int ll;
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define pb push_back

using namespace std;

const ll sz=(1e5+5)*2;
const ll szz=1e6+6;
const ll mod=1e9+7;

ll n,m,price[sz],maxi[sz];
vector<ll> ar[sz];
bool vis[sz];
ll ans = -1e16;

void dfs(ll node)
{
    vis[node]=1;

    if(maxi[node]!=-1)
    {
        return;
    }

    maxi[node]=0;
    for(auto x:ar[node])
    {
        dfs(x);
        maxi[node] = max(maxi[node],maxi[x]);
    }

    if(ar[node].size())
    {
        ans = max(ans,maxi[node]-price[node]);
    }
    maxi[node] = max(maxi[node],price[node]);
}

int main()
{
    IOS;

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

    clock_t startTime=clock();

    cin>>n>>m;
    rep(i,1,n+1)
    {
        cin>>price[i];
    }

    rep(i,0,m)
    {
        ll a,b;
        cin>>a>>b;
        ar[a].pb(b);
    }

    memset(maxi,-1,sizeof maxi);

    rep(i,1,n+1)
    {
        if(!vis[i])
        {
            dfs(i);
        }
    }

    cout<<ans<<endl;

    cerr << endl <<setprecision(20)<< double( clock() - startTime ) / (double)CLOCKS_PER_SEC<< " seconds." << endl;

}//Goodbye...


// Author: wlzhouzhuan
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pb push_back
#define fir first
#define sec second
#define rep(i, l, r) for (int i = l; i <= r; i++)
#define per(i, l, r) for (int i = l; i >= r; i--)
#define mset(s, t) memset(s, t, sizeof(s))
#define mcpy(s, t) memcpy(s, t, sizeof(t))
#define poly vector<int>
#define SZ(x) (int(x.size()))
template<typename T1, typename T2> void ckmin(T1 &a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> void ckmax(T1 &a, T2 b) { if (a < b) a = b; }
int read() {
  int x = 0, f = 0; char ch = getchar();
  while (!isdigit(ch)) f |= ch == '-', ch = getchar();
  while (isdigit(ch)) x = 10 * x + ch - '0', ch = getchar();
  return f ? -x : x;
}
template<typename T> void print(T x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10) print(x / 10);
  putchar(x % 10 + '0');
}
template<typename T> void print(T x, char let) {
  print(x), putchar(let);
}

bitset<100005> dp;
int n, m;

int main() {
  n = read(), m = 0;
  dp.set(0);
  for (int i = 1; i <= n; i++) {
    int x = read();
    dp |= dp << x;
    m += x;
  }
  for (int i = m / 2; i >= 0; i--) {
    if (dp.test(i)) {
      print(max(i, m - i), '\n');
      return 0;
    }
  }
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define P pair<ll,ll>
#define FOR(I,A,B) for(ll I = ll(A); I < ll(B); ++I)
#define FORR(I,A,B) for(ll I = ll((B)-1); I >= ll(A); --I)
#define TO(x,t,f) ((x)?(t):(f))
#define SORT(x) (sort(x.begin(),x.end())) // 0 2 2 3 4 5 8 9
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin()) //xi>=v  x is sorted
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin()) //xi>v  x is sorted
#define NUM(x,v) (POSU(x,v)-POSL(x,v))  //x is sorted
#define REV(x) (reverse(x.begin(),x.end())) //reverse
ll gcd_(ll a,ll b){if(a%b==0)return b;return gcd_(b,a%b);}
ll lcm_(ll a,ll b){ll c=gcd_(a,b);return ((a/c)*b);}
#define NEXTP(x) next_permutation(x.begin(),x.end())
const ll INF=ll(1e16)+ll(7);
const ll MOD=1000000007LL;
#define out(a) cout<<fixed<<setprecision((a))
//tie(a,b,c) = make_tuple(10,9,87);
#define pop_(a) __builtin_popcount((a))
ll keta(ll a){ll r=0;while(a){a/=10;r++;}return r;}
#define num_l(a,v) POSL(a,v) //v未満の要素数
#define num_eql(a,v) POSU(a,v) //v以下の要素数
#define num_h(a,v) (a.size() - POSU(a,v)) //vより大きい要素数
#define num_eqh(a,v) (a.size() - POSL(a,v)) //v以上の要素数
// static_cast< long long ll >

int N = 30*30;
int L = 30;
// (i,j) i個下 , j個右
int ind(int a,int b){
	return a*L + b;
}

//vector< vector<string> > S(N,vector<string>(N));
vector<vector<long double>> H(L,vector<long double>(L,5000.0));
vector<vector<long double>> V(L,vector<long double>(L,5000.0));

// 上は下より(i,j)のiが小さい　右は左より(i,j)のjが大きい

// 順路正の向きで
const int From_U = 0;//上から来た
const int From_D = 1;//下から来た
const int From_L = 2;//左から来た
const int From_R = 3;//右から来た

// 復元
string make_char = "DURL";
const int dy[] = {-1,1,0,0};
const int dx[] = {0,0,-1,1};

int main(){
	int sy,sx,ty,tx;
	int s,t;
	FOR(k,0,1000){
		cin >> sy >> sx >> ty >> tx;

		int dir_y = 1;
		int dir_x = 1;
		if(sy > ty) dir_y = -1;
		if(sx > tx) dir_x = -1;

		// (i,j) には (i - dir_y , j) or (i , j - dir_x) から行く

		vector< vector<long double> > score(L,vector<long double>(L,INF));
		vector< vector<int> > from(L,vector<int>(L,-1));

		score[sy][sx] = 0;
		for(int y=sy;(y-dir_y)!=ty;y+=dir_y){
			for(int x=sx;(x-dir_x)!=tx;x+=dir_x){
				//cout << y << " " << x << endl;

				if(y==sy && x==sx) continue;
				long double a = INF,b = INF;
				if(y != sy) a = score[y-dir_y][x] + H[min(y-dir_y,y)][x];
				if(x != sx) b = score[y][x-dir_x] + V[y][min(x,x-dir_x)];

				if(a < b){
					from[y][x] = (dir_y == 1 ? From_U : From_D );
					score[y][x] = a;
				}else{
					from[y][x] = (dir_x == 1 ? From_L : From_R );
					score[y][x] = b;
				}

			}
		}
		//return 0;
		//cout << "95" << endl;

		string T;
		int y=ty,x=tx;
		while(y!=sy || x!=sx){
			int a = from[y][x];
			T += make_char[a];
			assert(0 <= a && a <= 3);
			y += dy[a];
			x += dx[a];
			//cout << "from[" << y <<  "][" << x << "] = "  << a << endl;
		}

		//cout << "106" << endl;

		//cout << T << endl;
		REV(T);
		/*{ // チェック
			assert(T.size() == (abs(sy-ty)+abs(sx-tx)));
			int ud=0,lr=0;
			for(auto c:T){
				if(c == 'U' || c == 'D') ud++;
				if(c == 'L' || c == 'R') lr++;
			}
			assert(abs(sy-ty) == ud);
			assert(abs(sx-tx) == lr);
		}*/
		cout << T << endl;
		cout << flush;

		long double score_now,r;
		cin >> score_now;

		r = pow( score_now / score[ty][tx] , 1.0 - 1.0*(abs(sy-ty) + abs(sx-tx)) / 112.0 );

		y=ty,x=tx;
		while(y!=sy || x!=sx){
			int a = from[y][x];
			int by = y + dy[a],bx = x + dx[a];
			if(by != y){
				H[min(by,y)][x] *= r;
			}else{
				V[y][min(bx,x)] *= r;
			}
			y += dy[a];
			x += dx[a];
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<ll>;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rrep(i,n) for(int i=(n)-1;i>=0;i--)
#define rep1(i,n) for(int i=1;i<=(n);i++)
#define rrep1(i,n) for(int i=(n);i>0;i--)
#define fore(i_in,a) for (auto& i_in: a)
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define cnts(x,c) count(all(x),c)
#define fio() cin.tie(nullptr);ios::sync_with_stdio(false);
#define DEBUG_CTN(v) cerr<<#v<<":";for(auto itr=v.begin();itr!=v.end();itr++) cerr<<" "<<*itr; cerr<<endl
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 (a>b) {a = b; return 1;} return 0;}
template<class T> void print(const T &t) {cout<<t<<"\n";}
template<class T> void PRINT(const T &t) {rep(i,sz(t)) cout<<t[i]<<" \n"[i==sz(t)-1];}
const ll INF = 1LL << 62;
const int iINF = 1 << 30;

int n;
string s;

int main() {
    fio(); cin>>n>>s;
    if(*s.begin()!=*s.rbegin()) print(1);
    else{
        char c=*s.begin();
        bool flg=false;
        rep(i,n-1) if(s[i]!=c and s[i+1]!=c) flg=true;
        print(flg?"2":"-1");
    }
}

#ifdef __LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int,int>;
using PIL = pair<int,ll>;
using PLI = pair<ll,int>;
using PLL = pair<ll,ll>;
using Graph = vector<vector<int>>;
using Cost_Graph = vector<vector<PIL>>;

template<class T> bool chmin(T &a, T b) {if(a>b){a=b;return 1;}return 0;}
template<class T> bool chmax(T &a, T b) {if(a<b){a=b;return 1;}return 0;}
#define REP(i,n) for(int i=0;i<int(n);i++)
#define ROUNDUP(a,b) ((a+b-1)/b)
#define YESNO(T) cout<<(T?"YES":"NO")<<endl
#define yesno(T) cout<<(T?"yes":"no")<<endl
#define YesNo(T) cout<<(T?"Yes":"No")<<endl

const int INFint = 1 << 30;
const ll INFLL = 1LL << 60;
const ll MOD = 1000000007LL;
const double pi = 3.14159265358979;

int si,sj;
vector<vector<int>> tile(50, vector<int>(50));
vector<vector<int>> profit(50, vector<int>(50));
vector<bool> seen(2500,false);
string ans;

void all_resize(){
    return;
}

void all_input(){
    cin >> si >> sj;

    all_resize();

    for (int i = 0; i < 50; i++){
        for (int j = 0; j < 50; j++){
            cin >> tile[i][j];
        }
    }

    for (int i = 0; i < 50; i++){
        for (int j = 0; j < 50; j++){
            cin >> profit[i][j];
        }
    }

    return;
}

void all_output(){

    cout << ans << endl;

    return;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(15);

    all_input();

    all_output();
}

//


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define ln cout<<'\n'
void pr(){ln;}
template<class A,class...B>void pr(const A &a,const B&...b){cout<<a<<' ';pr(b...);}
void chmin(int& x, int y) { x = min(x, y);}

void solve() {
	int n, k; cin >> n >> k;
	int a[810][810];
	rep(i, n) rep(j, n) cin >> a[i][j];
	int l = -1, u = 1001001001;
	int L = k*k/2+1;

	while(l + 1 < u) {
		bool ok = false;
		int mid = (l + u) / 2;
		{
			int s[810][810];
			rep(i, n) rep(j, n) s[i+1][j+1] = a[i][j] > mid ? 1:0;
			rep(i, n+1) rep(j, n) s[i][j+1] += s[i][j];
			rep(i, n) rep(j, n+1) s[i+1][j] += s[i][j];
			rep(i, n-k+1) rep(j, n-k+1) {
				int now = s[i+k][j+k];
				now -= s[i][j+k];
				now -= s[i+k][j];
				now += s[i][j];
				if (now < L) ok = true;
			}
		}
		if (ok) u = mid; else l = mid;
	}
	cout << u << endl;
}

int main(){
	ios_base::sync_with_stdio(false), cin.tie(nullptr);
	solve();
	return 0;
}

#include <algorithm>
#include <iostream>

using namespace std;

const int N = 800, A = 1000000000;

int aa[N + 1][N + 1], pp[N + 1][N + 1];

int main() {
	int n, k; cin >> n >> k;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			cin >> aa[i][j];
	int lower = -1, upper = A;
	while (upper - lower > 1) {
		int a = (lower + upper) / 2;
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				pp[i][j] = (aa[i][j] <= a) + pp[i - 1][j] + pp[i][j - 1] - pp[i - 1][j - 1];
		bool yes = false;
		for (int i = k; i <= n; i++)
			for (int j = k; j <= n; j++)
				if (pp[i][j] - pp[i - k][j] - pp[i][j - k] + pp[i - k][j - k] >= (k * k + 1) / 2)
					yes = true;
		if (yes)
			upper = a;
		else
			lower = a;
	}
	cout << upper << '\n';
	return 0;
}


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

int main() {
    int n;
    cin >> n;
    vector<double> x(n),y(n);
    for(int i=0; i<n; i++) {
        cin >> x[i] >> y[i];
    }
    bool res = false;
    for(int i=0; i<n-2; i++) {
        for(int j=i+1; j<n-1; j++) {
            for(int k=j+1; k<n; k++){
                if((x[j]-x[i])*(y[k]-y[i])==(x[k]-x[i])*(y[j]-y[i])){
                    res = true;
                    break;
                }
            }
        }
    }

    if(res) cout << "Yes" << endl;
    else cout << "No" << endl;

    return 0;
}

#include <cassert>
#include <cctype>
#include <cerrno>
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cwchar>
#include <cwctype>
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <array>
#include <atomic>
#include <chrono>
#include <codecvt>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
using namespace std;
int main()
{
double n,D,H,R;
double ans;
cin>>n>>D>>H;
R=100000000;
D=D;
H=H;
vector<double> d(n),h(n);

for(double i=0; i<n; i++){
    cin>>d.at(i)>>h.at(i);
    d.at(i)=D-d.at(i);
    h.at(i)=H-h.at(i);
    R=min(R,h.at(i)/d.at(i));
}
ans=H-D*R;
if(H>=D*R)cout<<ans<<endl;
  else cout<<0<<endl;

}


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

#define ll long long
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pb push_back
#define ff first
#define ss second
#define YES printf("YES\n")
#define NO printf("NO\n")
#define nn "\n"
#define sci(x) scanf("%d", &x)
#define LL_INF (1LL << 62)
#define INF (1 << 30)
#define SetBit(x, k) (x |= (1LL << k))
#define ClearBit(x, k) (x &= ~(1LL << k))
#define CheckBit(x, k) (x & (1LL << k))
#define mod 1000000007
#define N 100005

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

    int n, m;
    cin >> n >> m;
    int x[m+2];
    x[0] = 0;
    x[1] = n+1;
    for(int i = 2; i < m+2; i++){
        cin >> x[i];
    }

    sort(x, x+(m+2));

    int z = INF;
    for(int i = 1; i < m+2; i++){
        int diff = x[i]-x[i-1]-1;
        if(diff) z = min(z, diff);
    }

    ll ans = 0;
    for(int i = 1; i < m+2; i++){
        int diff = x[i]-x[i-1]-1;
        ans += (diff + z - 1)/z;
    }

    cout << ans << nn;

    return 0;
}

// Pratiyush Mishra

#include <bits/stdc++.h>
#define ull unsigned long long int
#define ll long long int
#define LL_MAX 9223372036854775807
#define pb push_back
#define pf push_front
#define mp make_pair
#define popb pop_back
#define vl vector<ll>
#define pl pair<ll,ll>
#define bs(v, x) binary_search(v.begin(), v.end(), x)
#define mem1(a) memset(a, -1, sizeof(a))
#define popf pop_front
#define p0(x) cout << x << " "
#define p1(x) cout << x << '\n'
#define p2(x, y) cout << x << " " << y << '\n'
#define p3(x, y, z) cout << x << " " << y << " " << z << '\n'
#define printv(v)                   \
  for (ll i = 0; i < v.size(); ++i) \
    cout << v[i] << " ";            \
  cout << '\n'
#define pr1(x) cout << fixed << setprecision(15) << x << '\n'
#define ordered_set tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag, tree_order_statistics_node_update>
// ordered_set s ; s.order_of_key(val)  no. of elements strictly less than val
// s.find_by_order(i)  itertor to ith element (0 indexed)
#define mod 1000000007
#define mod1 998244353
#define fio                         \
  ios_base::sync_with_stdio(false); \
  cin.tie(NULL)
#define get(n) \
  ll n;        \
  cin >> n
#define getvec(v, n)         \
  vector<ll> v(n);           \
  for (ll i = 0; i < n; i++) \
    cin >> v[i];
#define getstr(s) \
  string s;       \
  cin >> s
#define all(x) x.begin(), x.end()
#define countBits(x) __builtin_popcount(x)
using namespace std;

ll n, m;
vl a, b;
vector<vl> dp;

ll solve(ll i, ll j)
{
  if (i >= n)
    return m - j;
  if (j >= m)
    return n - i;
  if (dp[i][j] != -1)
    return dp[i][j];
  ll ans1 = (a[i] != b[j]) + solve(i + 1, j + 1);
  ll ans2 = solve(i + 1, j) + 1;
  ll ans3 = solve(i, j + 1) + 1;
  ll ans = min(ans1, min(ans2, ans3));
  dp[i][j] = ans;
  return ans;
}

void mainSolve()
{
  cin >> n >> m;
  dp.resize(n, vl(m, -1));
  for (int i = 0; i < n; i++)
  {
    get(x);
    a.pb(x);
  }
  for (int i = 0; i < m; i++)
  {
    get(x);
    b.pb(x);
  }
  ll ans = solve(0, 0);
  p1(ans);
}

int main()
{
  fio;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  //get(t);
  ll t = 1;
  while (t--)
  {
    mainSolve();
  }
  return 0;
}


#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>

#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define printVec(v) printf("{"); for (const auto& i : v) { std::cout << i << ", "; } printf("}\n");
#define all(v) (v).begin(),(v).end()
#define debug(x) cout << #x << ": " << x << '\n';
#define degreeToRadian(deg) (((deg)/360)*2*M_PI)
#define radianTodegree(rad) (((rad)/2/M_PI)*360)

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
using ll = long long;
using P = pair<int,int>;
using PL = pair<ll, ll>;
const ll INF = 1LL<<60;
const int MOD = 1e9 + 7;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

struct UnionFind {
  int n;
  vector<int> a;
  vector<int> sz;

  UnionFind(int _n) : n(_n), a(_n), sz(_n, 1) {
    for(int i = 0; i < _n; i++) a[i] = i;
  }

  int root(int x) {
    if (a[x] == x) return x;
    return a[x] = root(a[x]);
  }

  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry) return;
    if (sz[rx] < sz[ry]) swap(rx, ry);
    sz[rx] += sz[ry];
    a[ry] = rx;
  }

  bool same(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }

  int size(int x) {
    return sz[root(x)];
  }

  map<int, vector<int>> groups() {
    map<int, vector<int>> group;
    rep (i, n) root(i);
    rep (i, n) group[a[i]].push_back(i);
    return group;
  }
};

int main() {
  //cin.tie(0);ios::sync_with_stdio(false);
  //cout << fixed << setprecision(15);
  int N, M;
  cin >> N >> M;
  UnionFind uf(N);
  vector<int> a(N), b(N);
  rep (i, N) cin >> a[i];
  rep (i, N) cin >> b[i];

  rep (i, M) {
    int c, d;
    cin >> c >> d;
    --c; --d;
    uf.unite(c, d);
  }

  for (auto v : uf.groups()) {
    ll x = 0, y = 0;
    for (auto vv : v.second) {
      x += a[vv];
      y += b[vv];
    }
    if (x == y) continue;

    cout << "No" << endl;
    return 0;
  }

  cout << "Yes" << endl;
  return 0;
}

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define pi 3.141592653589793238
#define int long long
#define ll long long
#define ld long double
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());

long long powm(long long a, long long b,long long mod) {
long long res = 1;
while (b > 0) {
    if (b & 1)
        res = res * a %mod;
    a = a * a %mod;
    b >>= 1;
}

return res;
}

ll gcd(ll a, ll b)
{
if (b == 0)
    return a;
return gcd(b, a % b);
}

signed main()
{

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

#ifndef ONLINE_JUDGE
if(fopen("INPUT.txt","r"))
{
freopen("INPUT.txt","r",stdin);
freopen("OUTPUT.txt","w",stdout);
}
#endif

    int k,n,m;
    cin>>k>>n>>m;
    int a[k+1];
    for(int i=1;i<=k;i++)
        cin>>a[i];
    int l=0,r=1e18;

    int mnf=r+1;

    while(l<=r)
    {
        int mid=l+(r-l)/2;
        int mn[k+1]={0};
        int mx[k+1]={0};
        int tot1=0;
        int tot2=0;

        for(int i=1;i<=k;i++)
        {
            int z1=mid+a[i]*m;
            z1/=n;
            int z2=-mid+a[i]*m;
            z2=(z2+n-1)/n;
            mn[i]=max(0LL,z2);
            mx[i]=min(z1,m);
            tot1+=mx[i];
            tot2+=mn[i];
        }

        if(tot1>=m && m>=tot2)
        {
            mnf=min(mnf,mid);
            r=mid-1;
        }
        else
        {
            l=mid+1;
        }
    }

    int mn[k+1];
    int mx[k+1];
    int sum1[k+1]={0};
    int sum2[k+1]={0};
    vector<int>ans;

    for(int i=1;i<=k;i++)
    {
        int z1=mnf+a[i]*m;
        z1/=n;
        int z2=-mnf+a[i]*m;
        z2=(z2+n-1)/n;
        mn[i]=max(0LL,z2);
        mx[i]=min(z1,m);
        sum1[i]=sum1[i-1]+mx[i];
        sum2[i]=sum2[i-1]+mn[i];
    }

    for(int i=k;i>=1;i--)
    {
        int z1=m-sum1[i-1];
        int z2=m-sum2[i-1];
        z1=max(mn[i],z1);
        z2=min(mx[i],z2);
        ans.push_back(z1);
        m-=z1;
    }

    reverse(ans.begin(),ans.end());
    for(auto i:ans)
        cout<<i<<" ";

}


#include <bits/stdc++.h>

using namespace std;

typedef long double LD;

typedef pair<LD, LD> PDD;

const LD eps = 1e-6;

int main()
{
	int n, m, k; scanf("%d %d %d", &n, &m, &k);
	vector<int> a(k + 1), vis(n + 1);
	for (int i = 1; i <= k; i++)
		scanf("%d", &a[i]), vis[a[i]] = 1;

	vector<PDD> suf(n + m + 1, {0.0, 0.0}), f(n + 1);
	for (int i = n - 1; i >= 0; i--)
	{
		if (vis[i]) f[i] = (PDD){1.0, 0.0};
		else f[i] = (PDD){(suf[i + 1].first - suf[i + m + 1].first) / m,
						 (suf[i + 1].second - suf[i + m + 1].second) / m + 1.0};
		suf[i] = (PDD){suf[i + 1].first + f[i].first, suf[i + 1].second + f[i].second};
	}

	if (abs(f[0].first - 1.0) < eps) printf("-1\n");
	else printf("%.10Lf\n", f[0].second / (1.0 - f[0].first));

	return 0;
}

//include <atcoder>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <tuple>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#define flush fflush(stdout)
#define endl '\n'
#define all(v) v.begin(), v.end()
#define pf(dans) printf("%.12lf", dans)
#define pfn(dans) pf(dans); cout << endl;
using namespace std;
//using namespace atcoder;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pl;
const int mod1 = (int)1e9 + 7, mod2 = (int)998244353;
const int INF = (int)1e9 + 10;
const ll LINF = (ll)1e18 + 10;
const int di[8] = {1, 0, -1, 0, 1, 1, -1, -1}, dj[8] = {0, 1, 0, -1, -1, 1, -1, 1};

#define rep0(i, n) for (i = 0; i < n; i++)
#define rep1(i, a, b) for (i = a; i < b; i++)

template <typename T>
T my_abs(T x){
    return (x >= 0)? x : -x;
}

template <typename T>
void chmax(T &a, T b){
    a = max(a, b);
}

template <typename T>
void chmin(T &a, T b){
    a = min(a, b);
}

ll gcd(ll a, ll b){
    if (a > b) return gcd(b, a);
    if (a == 0) return b;
    return gcd(b % a, a);
}

bool incld_bit(ll bit, int i){
    return ((bit>>i) & 1) == 1;
}

// --------------------------------------------------------------------------------

int n;
int x[20], y[20], z[20];
ll dp[(1<<17) + 3][20];
bool u[(1<<17) + 3][20] = {false};

int dist(int u, int v){
    return my_abs(x[v] - x[u]) + my_abs(y[v] - y[u]) + max(0, z[v] - z[u]);
}

void dfs(int bit, int v){
    int i;
    if (u[bit][v]){
        return;
    }
    rep0(i, n){
        if (i != v && incld_bit(bit, i)){
            dfs(bit - (1<<v), i);
            chmin(dp[bit][v], dp[bit - (1<<v)][i] + dist(i, v));
        }
    }
    u[bit][v] = true;
}

int main(void){
    int i, j;

    cin >> n;
    rep0(i, n){
        cin >> x[i] >> y[i] >> z[i];
    }

    int bit;
    rep0(bit, 1<<n){
        rep0(i, n){
            dp[bit][i] = LINF;
        }
    }
    dp[1][0] = 0;

    ll ans;
    ans = LINF;
    rep0(i, n){
        dfs((1<<n) - 1, i);
        chmin(ans, dp[(1<<n) - 1][i] + dist(i, 0));
    }

    cout << ans << endl;

    return 0;
}


#include<bits/stdc++.h>
#define qc ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define rt return
#define fi first
#define se second
#define yes cout<<"YES\n";
#define no cout<<"NO\n";
typedef long long ll;
using namespace std;
ll dig(ll n)
{
    ll ans=0;
    while(n>0){
        n/=10;ans++;
    }rt ans;
}
ll ten(ll n)
{
    ll ans=1;
    while(n--){
        ans*=10;
    }
    rt ans;
}
int main(){
    qc
    ll n;cin>>n;
    ll d=dig(n);
    if(d==1)cout<<0;
    else if(d%2==1){
        d/=2;
        for(ll i=0;i<d;i++){
            cout<<9;
        }
    }
    else{
        ll a=n/ten(d/2);
        n%=ten(d/2);
        if(a>n)cout<<a-1;
        else cout<<a;
    }
    rt 0;
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
	ll n;
	cin >> n;
	ll limit = (log10(n)+1)/2;
	ll ans = n/pow(10,limit);
	//cout << ans << " " << limit <<" " << endl;
	ll temp = ans;
	while(temp*pow(10,limit)+temp>n){
		temp--;
		ans--;
		limit = log10(temp)+1;
	}
	cout << ans << endl;

	return 0;
}


#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, n, s) for (int i = (s); i < (n); i++)
#define rrep(i, n) for (int i = (n - 1); i >= 0; i--)
#define rreps(i, n, s) for (int i = s; i >= n; i--)

using ll = long long;
using namespace std;
constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int MOD = 1000000007;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int a, b, x, y;
    cin >> a >> b >> x >> y;
    int res = 0;
    int u = 2 * x < y ? 2 * x : y;
    if (a > b) {
        res += u * (a - b - 1) + x;
    } else {
        res += u * (b - a) + x;
    }
    cout << res << endl;
    return 0;
}

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

#define fr first
#define sc second
#define pb push_back

int a, b, x, y, ans;

int main(){

    cin>>a>>b>>x>>y;

    if(a < b){
        ans = min((b-a)*y,(b-a)*2*x) + x;
    }
    else if(a > b){
        ans = min((a-b-1)*y,(a-b-1)*2*x) + x;
    }
    else{
        ans = x;
    }
    cout<<ans<<endl;

    return 0;
}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <assert.h>
#include <complex>
#include <utility>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <tuple>
#include <cmath>
#include <bitset>
#include <cctype>
#include <set>
#include <map>
#include <unordered_map>
#include <numeric>
#include <functional>
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(ll i=ll(a);i<ll(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define rrep(i,a) for(ll i=ll(a-1);i>=0;--i)
#define all(x) (x).begin(),(x).end()
#define PRINT(V) cout << V << "\n"
#define SORT(V) sort((V).begin(),(V).end())
#define RSORT(V) sort((V).rbegin(), (V).rend())
using namespace std;
using ll = long long;
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; }
inline void Yes(bool condition){ if(condition) PRINT("Yes"); else PRINT("No"); }
template<class itr> void cins(itr first,itr last){
    for (auto i = first;i != last;i++){
        cin >> (*i);
    }
}
template<class itr> void array_output(itr start,itr goal){
    string ans = "",k = " ";
    for (auto i = start;i != goal;i++) ans += to_string(*i)+k;
    if (!ans.empty()) ans.pop_back();
    PRINT(ans);
}
ll gcd(ll a, ll b) {
    return a ? gcd(b%a,a) : b;
}

const ll INF = 1e18;
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const ll MOD3 = 1e6;
const ll EPS = 1e-10;
int sgn(const double a){
    return (a < -EPS ? -1 : (a > EPS ? +1 : 0));
}
typedef pair<int,int> pi;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> tri;
typedef pair<double,double> point;
typedef complex<double> Point;
const ll MAX = 200005;
constexpr ll nx[4] = {-1,0,1,0};
constexpr ll ny[4] = {0,1,0,-1};
class BIT{
    private:
        vector<ll> bit;
        ll size;
    public:
        BIT(ll n){
            bit.resize(n+1);
            size = n;
        }
        ll sum(ll i){
            ll res = 0;
            while(i > 0){
                res += bit[i];
                i -= i&-i;
            }
            return res;
        }
        void add(ll i,ll x){
            while(i <= size){
                bit[i] += x;
                i += i&-i;
            }
        }
};

int main(){
    ll h,w,m;
    cin >> h >> w >> m;
    vector<vector<ll>> x(h);
    vector<ll> y(w,h);
    ll ox = h,oy = w;
    rep(i,m){
        ll s,t;
        cin >> s >> t;
        s--,t--;
        x[s].push_back(t);
        chmin(y[t],s);
        if (s == 0) chmin(oy,t);
        if (t == 0) chmin(ox,s);
    }
    rep(i,h){
        x[i].push_back(w);
        SORT(x[i]);
    }
    ll ans = 0;
    BIT a(w+1);
    rep(i,ox){
        if (i == 0){
            rep(j,x[i][0]+1,w+1){
                a.add(j,1);
            }
        }
        else{
            for (ll ind:x[i]){
                if (a.sum(ind+1)-a.sum(ind) == 0){
                    a.add(ind+1,1);
                }
            }
        }
        ans += x[i][0]-(x[i][0]-a.sum(x[i][0]));
        //PRINT(x[i][0] << " " << a.sum(x[i][0]));
    }
    rep(i,oy){
        ans += y[i];
    }
    PRINT(ans);
}

#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, n, s) for (int i = (s); i < (n); i++)
#define rrep(i, n) for (int i = (n - 1); i >= 0; i--)
#define rreps(i, n, s) for (int i = s; i >= n; i--)

using ll = long long;
using namespace std;
constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int MOD = 1000000007;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n, m;
    cin >> n >> m;
    vector<vector<ll>> G(n);
    rep(i, m) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        G[a].pb(b);
        G[b].pb(a);
    }
    int k;
    cin >> k;
    vector<ll> c(k);
    set<int> p;
    rep(i, k) {
        cin >> c[i];
        c[i]--;
        p.insert(c[i]);
    }

    vector<vector<ll>> dist(k, vector<ll>(k));
    queue<ll> q;

    rep(i, k) {
        q.push(c[i]);
        vector<ll> tmp_dist(n, INF);
        tmp_dist[c[i]] = 0;
        while (!q.empty()) {
            ll crr = q.front();
            q.pop();
            for (auto next : G[crr]) {
                if (tmp_dist[crr] + 1 < tmp_dist[next]) {
                    tmp_dist[next] = tmp_dist[crr] + 1;
                    q.push(next);
                }
            }
        }
        rep(j, k) dist[i][j] = tmp_dist[c[j]];
    }

    // bitdp
    ll max_s = 1 << k;
    vector<vector<ll>> dp(max_s, vector<ll>(k, INF));
    rep(i, k) dp[1 << i][i] = 1;
    reps(bit, max_s, 1) {
        rep(i, k) {
            if ((bit >> i) & 1) {
                ll target_bit = 1 << i;
                ll bit2 = bit ^ target_bit;
                rep(j, k) {
                    if ((bit2 >> j) & 1) { dp[bit][i] = min(dp[bit][i], dp[bit2][j] + dist[i][j]); }
                }
            }
        }
    }
    ll res = INF;
    for (ll val : dp[max_s - 1]) res = min(res, val);
    if (res == INF) res = -1;
    cout << res << endl;
    return 0;
}


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

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

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; }

typedef long long ll;
typedef pair<int, int> P;

const int MAX = 200005;
const int INF = 1001001001;
const int MOD = 1000000007;

int main(){
    int N, K;
    cin >> N >> K;

    ll ans = 0;
    for (int x = 2; x <= 2*N; ++x) {
        int y = x - K;
        if (y < 2 || 2*N < y) continue;

        ans += (ll)(x-1 - max(0, (x-N-1) * 2)) * (y-1 - max(0, (y-N-1) * 2));
    }

    cout << ans << endl;
}

#include <bits/stdc++.h>
using namespace std;
#pragma optimize("-O3")
#define int long long int
#define f first
#define s second
#define pb push_back
#define endl '\n'
const int MOD=998244353;
int n, m;
int po[5005][5005];

main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin>>n>>m;
	int tot=n;
	for(int i=0; i<5005; i++){
		po[i][0]=1;
	}
	po[0][0]=1;
	for(int i=1; i<5005; i++){
		for(int j=1; j<5005; j++){
			po[i][j]=(i*po[i][j-1])%MOD;
		}
	}
	for(int i=0; i<n; i++){
		tot=(tot*m)%MOD;
	}

	for(int i=0; i<=n-2; i++){
		int val=n-i-1;
		for(int j=1; j<=m; j++){
			int val=((((n-i-1)*po[m-j][i])%MOD)*po[m][n-i-2])%MOD;
			tot=(tot-val+MOD)%MOD;
		}
	}
	cout<<tot;
	return 0;
}


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

int main(){
    string s; cin >> s;
    string tmp1="",tmp2;
    int c = s.size();
    bool flag = false;
    for(int ix=0;ix<c;ix++){
        if(s.at(c-ix-1) != '0' || flag == true){
            tmp1 = s.at(c-ix-1) + tmp1;
            flag = true;
        }
    }
    //cout << tmp1 << endl;
    tmp2 = tmp1;
    reverse(tmp2.begin(),tmp2.end());
    if(tmp1 == tmp2){
        cout << "Yes" << endl;
    }
    else{
        cout << "No" << endl;
    }
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
ll gcd(ll a, ll b){return b? gcd(b, a % b): a;}
ll quickpow(ll a, ll b){ll res = 1; while(b){if (b & 1) res = res * a; a = a * a; b >>= 1;} return res;}
// head

string s;

int main(void){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> s;
	for (auto it: s) {
		if (it == '.') break;
		cout << it;
	}
	cout << endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
  ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
  char s,t;
  cin>>s>>t;
  if(s=='Y')cout<<char(toupper(t))<<endl;
  else cout<<t<<endl;
  return 0;
}

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
//#include<atcoder/all>
#include<iostream>
#include<cstdint>
#include<cstddef>
using namespace std;
//using namespace atcoder;
using i32 = int_fast32_t;
using i64 = int_fast64_t;
using usize = uint_fast64_t;
#define rep(i, n) for (usize i = 0; i < (usize)(n); i++)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
using P = pair<i64,i64>;

int main(){
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    char a;
    char b;
    cin >> a >> b;
    if(a == 'Y'){
        if(b == 'a')cout << 'A' << endl;
        else if(b == 'b')cout << 'B' << endl;
        else cout << 'C' << endl;
    }else{
        cout << b << endl;
    }
}