//Ishaan Singh 2019CS10359
//tut
#include <iostream>
#include<sstream>
#include<vector>
#include<set>
#include<math.h>
#include<bitset>
#include<iomanip>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
typedef long long int ll;
typedef long double ld;
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//#define oset tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define fast                          \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL)
#define fo(i,n) for(int i=0;i<n;i++)
#define PI 3.141592654
#define all(v) (v).begin(), (v).end()
#define allr(v) (v).rbegin(), (v).rend()
#define pb push_back
#define mp make_pair
#define vb vector<bool>
#define vvb vector<vb>
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<ll>
#define vvl vector<vl>
#define pii pair<int,int>
#define vpi vector<pii>
#define unordered_set(T) unordered_set<T, custom_hash>
#define unordered_map(T1, T2) unordered_map<T1, T2, custom_hash>
#define f first
#define se second    // large prime for modulo
using namespace std;
const ll M=998244353;
ll fpow(ll a,ll b) {// function for binary exponentiation
    b%=(M-1);
    ll res= 1;
    while(b>0) {
        if (b&1){
            res = res * a%M;
        }
        a = a * a%M;
        b >>= 1 ;
    }
    return res;
}
ll inv(ll n){return n==1 ? n : M-(M/n)*inv(M%n)%M;}
/*
 ll spf[10000000+1];
bool primes[2000+5]={false};
 void sieve()  // sieve of eratosthenes
 {
     spf[1] = 1;
     primes[2]=true;
     for (ll i=2; i<2005; i++)
         spf[i] = i;
     for(ll i=4;i<2005;i+=2)
         spf[i]=2;
     for (ll i=3; i*i<2005; i+=2)
     {
         if (spf[i] == i)
         {
             primes[i]=true;
             for (ll j=i*i; j<2005; j+=i)
                 if (spf[j]==j)
                     spf[j] = i;
         }
     }
 }
map<ll,ll> gpf(ll n) // generates prime factors of a number in log(n) time with o(nloglogn) preprocessing time
 {
     map<ll,ll>v;
     ll curr = spf[n];
     ll cnt = 1;
     while (n > 1)
     {
         n/= spf[n];
         if (curr == spf[n])
         {
             cnt++;
             continue;
         }
         v.insert({curr,cnt});
         curr = spf[n];
         cnt = 1;
     }
     return v;
 }

    bool operat(pair<pii,ll>a,pair<pii,ll>b)
    {
        if (a.first.first == b.first.first){
            return a.first.second < b.first.second;
        }
        return a.first.first < b.first.first;
    }
ll gcd(int a,int b){return b ? gcd(b,a%b) :a;}
// returns gcd of two numbers in logarithmic time
map<ll,ll> prsqrt(ll n){                            // returns prime factors of a number in root n time complexity
    ll cnt=0;map<ll,ll>m;
    while (n % 2 == 0){cnt++;n = n/2;}
    if (cnt!=0){m.insert({2,cnt});}
    for (ll i=3;i<=sqrt(n);i+=2){cnt=0;while (n % i == 0){cnt++;n = n/i;}if(cnt!=0){m.insert({i,cnt});}}
    if (n > 2){m.insert({n,1});}
    return m;
}
ll lpfact(ll n,ll p){ll ans=0;while(n){ans+=n/p;n/=p;}return ans;}
//returns largest power of a prime p in n factorial
vector<int>tin(200000+5);vector<int>low(200000+5);
vector<int>adj[2*100000+5];
vpi bridges;
int timer=0;
bool visited[3*100000+5];
void bridge(int v,int p){                   //finding bridges in O(n+m) time
    visited[v]=true;
    tin[v]=low[v]=timer++;
    for(auto u:adj[v]){if (u==p){continue;}else if (visited[u]){low[v]=min(low[v],tin[u]);return;}
        else{bridge(u,v);low[v]=min(low[v],low[u]);if (low[u]>tin[v]){bridges.pb({u,v});}}}}
ll dyno1[1000001]={0};
ll dyno2[1000001]={0};
ll dynosum(int l,int r,ll dyno[]){
    if(l>r){return 0;}                  // fenwick tree sum query
    ll res=0,resl=0;
    for(int i=r;i>=0;i=(i & (i+1))-1){res+=dyno[i];}
     for(int i=l-1;i>=0;i=(i & (i+1))-1){resl+=dyno[i];}
           return res-resl;
}
void dynoupdate(int ind,ll val,ll dyno[]){                               //fenwick tree point update query
       for(int i=ind;i<1000001;i=(i|(i+1))){
           dyno[i]+=val;
       }
}
ll t[5*1000000]={0};
void build(int a[], int v, int tl, int tr) {
    if (tl == tr) {
        t[v] = a[tl];
    } else {
        int tm = (tl + tr) / 2;
        build(a, v*2, tl, tm);
        build(a, v*2+1, tm+1, tr);
        t[v] = 0;
    }
}

void update(int v, int tl, int tr, int l, int r, int add) {
    if (l > r)
        return;
    if (l == tl && r == tr) {
        t[v] += add;
    } else {
        int tm = (tl + tr) / 2;
        update(v*2, tl, tm, l, min(r, tm), add);
        update(v*2+1, tm+1, tr, max(l, tm+1), r, add);
    }
}

ll get(int v, int tl, int tr, int pos) {
    if (tl == tr)
        return t[v];
    int tm = (tl + tr) / 2;
    if (pos <= tm)
        return t[v] + get(v*2, tl, tm, pos);
    else
        return t[v] + get(v*2+1, tm+1, tr, pos);
}
ll sum(int v, int tl, int tr, int l, int r) {
    if (l > r)
        return 0;
    if (l == tl && r == tr) {
        return t[v];
    }
    int tm = (tl + tr) / 2;
    return sum(v*2, tl, tm, l, min(r, tm))
           + sum(v*2+1, tm+1, tr, max(l, tm+1), r);
}
 */
//ll g(int k1,int k2){ll ans=0;for(int oh=k1;oh<=k2;oh++){ans+=9*(81*(oh-1)*(oh-2)/2+9*(oh-1)+1);}return ans;}
int n;
ll a[3*100000+5];
bool lm(ll x){if(x==0){if(a[0]>a[1]){return 1;}}else if(x==n-1){if(a[n-1]>a[n-2]){return 1;}}else{if(a[x]>a[x-1] && a[x]>a[x+1]){return 1;}}return 0;}
bool lmi(ll x){if(x>0 && x<n-1){if(a[x]<a[x+1] && a[x]<a[x-1]){return 1;}}return 0;}
int main()
{
    int t;cin>>t;while(t--){
        int q;cin>>n>>q;
        fo(i,n){cin>>a[i];}
         ll ls=0,ms=0;
        for(int i=1;i<n-1;i++){
            if(a[i]>a[i-1] && a[i]>a[i+1]){ls+=a[i];}
            else if (a[i]<a[i-1] && a[i]<a[i+1]){ms+=a[i];}
        }
        if(n>1){if(a[0]>a[1]){ls+=a[0];}if(a[n-1]>a[n-2]){ls+=a[n-1];}}
        if(n==1){ls=a[0];}
        cout<<ls-ms<<endl;
        fo(i,q){
            ll l,r;cin>>l>>r;
            l--;r--;
            if(l==r){cout<<ls-ms<<endl;}
            else{
                if(lm(l)){ls-=a[l];}if(lmi(l)){ms-=a[l];}
            if(lm(l+1)){ls-=a[l+1];}if(lmi(l+1)){ms-=a[l+1];}
            if(l!=0){if(lm(l-1)){ls-=a[l-1];}if(lmi(l-1)){ms-=a[l-1];}}
            if(r!=n-1){if(lm(r+1)){ls-=a[r+1];}if(lmi(r+1)){ms-=a[r+1];}}
            if(r>l+1){if(lm(r)){ls-=a[r];}if(lmi(r)){ms-=a[r];}}
            if(r>l+2){if(lm(r-1)){ls-=a[r-1];}if(lmi(r-1)){ms-=a[r-1];}}
            ll temp=a[l];a[l]=a[r];a[r]=temp;
            if(lm(l)){ls+=a[l];}if(lmi(l)){ms+=a[l];}
            if(lm(l+1)){ls+=a[l+1];}if(lmi(l+1)){ms+=a[l+1];}
            if(l!=0){if(lm(l-1)){ls+=a[l-1];}if(lmi(l-1)){ms+=a[l-1];}}
            if(r!=n-1){if(lm(r+1)){ls+=a[r+1];}if(lmi(r+1)){ms+=a[r+1];}}
            if(r>l+1){if(lm(r)){ls+=a[r];}if(lmi(r)){ms+=a[r];}}
            if(r>l+2){if(lm(r-1)){ls+=a[r-1];}if(lmi(r-1)){ms+=a[r-1];}}
                cout<<ls-ms<<endl;
                }
                }
    }
    return 0;
}


//Ishaan Singh 2019CS10359
//tut
#include <iostream>
#include<sstream>
#include<vector>
#include<set>
#include<math.h>
#include<bitset>
#include<iomanip>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
typedef long long int ll;
typedef long double ld;
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//#define oset tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define fast                          \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL)
#define fo(i,n) for(int i=0;i<n;i++)
#define PI 3.141592654
#define all(v) (v).begin(), (v).end()
#define allr(v) (v).rbegin(), (v).rend()
#define pb push_back
#define mp make_pair
#define vb vector<bool>
#define vvb vector<vb>
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<ll>
#define vvl vector<vl>
#define pii pair<int,int>
#define vpi vector<pii>
#define unordered_set(T) unordered_set<T, custom_hash>
#define unordered_map(T1, T2) unordered_map<T1, T2, custom_hash>
#define f first
#define se second    // large prime for modulo
using namespace std;
const ll M=998244353;
ll fpow(ll a,ll b) {// function for binary exponentiation
    b%=(M-1);
    ll res= 1;
    while(b>0) {
        if (b&1){
            res = res * a%M;
        }
        a = a * a%M;
        b >>= 1 ;
    }
    return res;
}
ll inv(ll n){return n==1 ? n : M-(M/n)*inv(M%n)%M;}
/*
 ll spf[10000000+1];
bool primes[2000+5]={false};
 void sieve()  // sieve of eratosthenes
 {
     spf[1] = 1;
     primes[2]=true;
     for (ll i=2; i<2005; i++)
         spf[i] = i;
     for(ll i=4;i<2005;i+=2)
         spf[i]=2;
     for (ll i=3; i*i<2005; i+=2)
     {
         if (spf[i] == i)
         {
             primes[i]=true;
             for (ll j=i*i; j<2005; j+=i)
                 if (spf[j]==j)
                     spf[j] = i;
         }
     }
 }
map<ll,ll> gpf(ll n) // generates prime factors of a number in log(n) time with o(nloglogn) preprocessing time
 {
     map<ll,ll>v;
     ll curr = spf[n];
     ll cnt = 1;
     while (n > 1)
     {
         n/= spf[n];
         if (curr == spf[n])
         {
             cnt++;
             continue;
         }
         v.insert({curr,cnt});
         curr = spf[n];
         cnt = 1;
     }
     return v;
 }

    bool operat(pair<pii,ll>a,pair<pii,ll>b)
    {
        if (a.first.first == b.first.first){
            return a.first.second < b.first.second;
        }
        return a.first.first < b.first.first;
    }
ll gcd(int a,int b){return b ? gcd(b,a%b) :a;}
// returns gcd of two numbers in logarithmic time
map<ll,ll> prsqrt(ll n){                            // returns prime factors of a number in root n time complexity
    ll cnt=0;map<ll,ll>m;
    while (n % 2 == 0){cnt++;n = n/2;}
    if (cnt!=0){m.insert({2,cnt});}
    for (ll i=3;i<=sqrt(n);i+=2){cnt=0;while (n % i == 0){cnt++;n = n/i;}if(cnt!=0){m.insert({i,cnt});}}
    if (n > 2){m.insert({n,1});}
    return m;
}
ll lpfact(ll n,ll p){ll ans=0;while(n){ans+=n/p;n/=p;}return ans;}
//returns largest power of a prime p in n factorial
vector<int>tin(200000+5);vector<int>low(200000+5);
vector<int>adj[2*100000+5];
vpi bridges;
int timer=0;
bool visited[3*100000+5];
void bridge(int v,int p){                   //finding bridges in O(n+m) time
    visited[v]=true;
    tin[v]=low[v]=timer++;
    for(auto u:adj[v]){if (u==p){continue;}else if (visited[u]){low[v]=min(low[v],tin[u]);return;}
        else{bridge(u,v);low[v]=min(low[v],low[u]);if (low[u]>tin[v]){bridges.pb({u,v});}}}}
ll dyno1[1000001]={0};
ll dyno2[1000001]={0};
ll dynosum(int l,int r,ll dyno[]){
    if(l>r){return 0;}                  // fenwick tree sum query
    ll res=0,resl=0;
    for(int i=r;i>=0;i=(i & (i+1))-1){res+=dyno[i];}
     for(int i=l-1;i>=0;i=(i & (i+1))-1){resl+=dyno[i];}
           return res-resl;
}
void dynoupdate(int ind,ll val,ll dyno[]){                               //fenwick tree point update query
       for(int i=ind;i<1000001;i=(i|(i+1))){
           dyno[i]+=val;
       }
}
ll t[5*1000000]={0};
void build(int a[], int v, int tl, int tr) {
    if (tl == tr) {
        t[v] = a[tl];
    } else {
        int tm = (tl + tr) / 2;
        build(a, v*2, tl, tm);
        build(a, v*2+1, tm+1, tr);
        t[v] = 0;
    }
}

void update(int v, int tl, int tr, int l, int r, int add) {
    if (l > r)
        return;
    if (l == tl && r == tr) {
        t[v] += add;
    } else {
        int tm = (tl + tr) / 2;
        update(v*2, tl, tm, l, min(r, tm), add);
        update(v*2+1, tm+1, tr, max(l, tm+1), r, add);
    }
}

ll get(int v, int tl, int tr, int pos) {
    if (tl == tr)
        return t[v];
    int tm = (tl + tr) / 2;
    if (pos <= tm)
        return t[v] + get(v*2, tl, tm, pos);
    else
        return t[v] + get(v*2+1, tm+1, tr, pos);
}
ll sum(int v, int tl, int tr, int l, int r) {
    if (l > r)
        return 0;
    if (l == tl && r == tr) {
        return t[v];
    }
    int tm = (tl + tr) / 2;
    return sum(v*2, tl, tm, l, min(r, tm))
           + sum(v*2+1, tm+1, tr, max(l, tm+1), r);
}
 */
//ll g(int k1,int k2){ll ans=0;for(int oh=k1;oh<=k2;oh++){ans+=9*(81*(oh-1)*(oh-2)/2+9*(oh-1)+1);}return ans;}
int main()
{
    int t;cin>>t;while(t--){
        int n,q;cin>>n>>q;
        ll a[n];fo(i,n){cin>>a[i];}
        set<ll>locma;
        set<ll>locmi;
        for(int i=1;i<n-1;i++){
            if(a[i]>a[i-1] && a[i]>a[i+1]){locma.insert(i);}
            else if (a[i]<a[i-1] && a[i]<a[i+1]){locmi.insert(i);}
        }
        if(n>1){if(a[0]>a[1]){locma.insert(0);}if(a[n-1]>a[n-2]){locma.insert(n-1);}}
        ll ls=0,ms=0;
        for(auto u:locma){ls+=a[u];}for(auto u:locmi){ms+=a[u];}
        cout<<ls-ms<<endl;
        fo(i,q){
            ll l,r;cin>>l>>r;
            l--;r--;
            if(l==r){cout<<ls-ms<<endl;}
            else{
                if(locma.find(l)!=locma.end()){locma.erase(l);ls-=a[l];}
                if(locmi.find(l)!=locmi.end()){locmi.erase(l);ms-=a[l];}
                 if(locma.find(l+1)!=locma.end()){locma.erase(l+1);ls-=a[l+1];}
                if(locmi.find(l+1)!=locmi.end()){locmi.erase(l+1);ms-=a[l+1];}
            if(l!=0){
            if(locmi.find(l-1)!=locmi.end()){locmi.erase(l-1);ms-=a[l-1];}
                if(locma.find(l-1)!=locma.end()){locma.erase(l-1);ls-=a[l-1];}
            }
            if(r!=n-1){
                if(locma.find(r+1)!=locma.end()){locma.erase(r+1);ls-=a[r+1];}
                if(locmi.find(r+1)!=locmi.end()){locmi.erase(r+1);ms-=a[r+1];}
            }
            if(r>l+1){
                    if(locma.find(r)!=locma.end()){locma.erase(r);ls-=a[r];}
                    if(locmi.find(r)!=locmi.end()){locmi.erase(r);ms-=a[r];}
            }
            if(r>l+2){
                if(locma.find(r-1)!=locma.end()){locma.erase(r-1);ls-=a[r-1];}
                if(locmi.find(r-1)!=locmi.end()){locmi.erase(r-1);ms-=a[r-1];}
            }
            ll temp=a[l];
            a[l]=a[r];
            a[r]=temp;
                if(l+1==n-1){
                if(a[l+1]>a[l]){locma.insert(l+1);ls+=a[l+1];}
                }
            else{
            if(a[l+1]>a[l] && a[l+1]>a[l+2]){locma.insert(l+1);ls+=a[l+1];}else if (a[l+1]<a[l] && a[l+1]<a[l+2]){locmi.insert(l+1);ms+=a[l+1];}
            }
                if(l==0){if(a[l]>a[l+1]){locma.insert(l);ls+=a[l];}
                }
                else{if(a[l]>a[l-1] && a[l]>a[l+1]){locma.insert(l);ls+=a[l];}else if (a[l]<a[l-1] && a[l]<a[l+1]){locmi.insert(l);ms+=a[l];}}
                if(l==1){if(a[0]>a[1]){locma.insert(0);ls+=a[0];}}
                else{if(a[l-1]>a[l-2] && a[l-1]>a[l]){locma.insert(l-1);ls+=a[l-1];}else if (a[l-1]<a[l-2] && a[l-1]<a[l]){locmi.insert(l-1);ms+=a[l-1];}}
                if(r!=n-1){
                    if(r+1==n-1){
                        if(a[n-1]>a[n-2]){locma.insert(n-1);ls+=a[n-1];}
                    }
                    else{
                        if(a[r+1]>a[r] && a[r+1]>a[r+2]){locma.insert(r+1);ls+=a[r+1];}else if (a[r+1]<a[r] && a[r+1]<a[r+2]){locmi.insert(r+1);ms+=a[r+1];}
                    }
                }
                if(r>l+1){
                    if(r==n-1){
                        if(a[n-1]>a[n-2]){locma.insert(n-1);ls+=a[n-1];}
                    }
                    else{
                        if(a[r]>a[r-1] && a[r]>a[r+1]){locma.insert(r);ls+=a[r];}else if (a[r]<a[r-1] && a[r]<a[r+1]){locmi.insert(r);ms+=a[r];}
                    }
                }
                if(r>l+2){
                      if(a[r-1]>a[r-2] && a[r-1]>a[r]){locma.insert(r-1);ls+=a[r-1];}else if (a[r-1]<a[r-2] && a[r-1]<a[r]){locmi.insert(r-1);ms+=a[r-1];}
                }
                cout<<ls-ms<<endl;
                }
                }
    }
    return 0;
}


#include <bits/stdc++.h>

#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()

#define int long long

using namespace std;

const int INF = 1e16 + 109;

struct NODE {
    int dp00, dp01, dp10, dp11;
};

vector<NODE> tree;
vector<int> a;
int n;

inline void upd(NODE& c, NODE& a, NODE& b) {
    c = {-INF, -INF, -INF, -INF};

    c.dp00 = max(c.dp00, a.dp00 + b.dp10);
    c.dp00 = max(c.dp00, a.dp01 + b.dp00);

    c.dp01 = max(c.dp01, a.dp00 + b.dp11);
    c.dp01 = max(c.dp01, a.dp01 + b.dp01);

    c.dp10 = max(c.dp10, a.dp10 + b.dp10);
    c.dp10 = max(c.dp10, a.dp11 + b.dp00);

    c.dp11 = max(c.dp11, a.dp10 + b.dp11);
    c.dp11 = max(c.dp11, a.dp11 + b.dp01);
}

void inc(int v, int l, int r, int ind, int x) {
    if (l + 1 == r) {
        tree[v].dp00 = -x;
        tree[v].dp01 = 0;
        tree[v].dp10 = 0;
        tree[v].dp11 = x;

        return;
    }

    int m = (l + r) / 2;
    if (ind < m) {
        inc(2 * v + 1, l, m, ind, x);
    } else {
        inc(2 * v + 2, m, r, ind, x);
    }

    upd(tree[v], tree[2 * v + 1], tree[2 * v + 2]);
}

void build(int v, int l, int r) {
    if (l + 1 == r) {
        tree[v].dp00 = -a[l];
        tree[v].dp01 = 0;
        tree[v].dp10 = 0;
        tree[v].dp11 = a[l];

        //cout << "!!! " << v << " " << l << " " << r << "\n";
        //coutcout << tree[v].dp00 << " " << tree[v].dp01 << " " << tree[v].dp10 << " " << tree[v].dp11 << "\n\n";

        return;
    }

    int m = (l + r) / 2;
    build(2 * v + 1, l, m);
    build(2 * v + 2, m, r);

    upd(tree[v], tree[2 * v + 1], tree[2 * v + 2]);

    //cout << "!!! " << v << " " << l << " " << r << "\n";
    //cout << tree[v].dp00 << " " << tree[v].dp01 << " " << tree[v].dp10 << " " << tree[v].dp11 << "\n\n";
}

signed main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

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

    int t, q, l, r, ans, x, y;
    cin >> t;

    NODE aaa;
    while (t--) {
        cin >> n >> q;
        a.resize(n);

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

        tree.resize(4 * n);
        build(0, 0, n);

        ans = tree[0].dp11;
        cout << ans << "\n";

        while (q--) {
            cin >> l >> r;
            l--, r--;

            x = a[l], y = a[r];
            swap(a[l], a[r]);

            inc(0, 0, n, l, y);
            inc(0, 0, n, r, x);

            ans = tree[0].dp11;
            cout << ans << "\n";
        }
    }

    return 0;
}


#include <bits/stdc++.h>

#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()

#define int long long

using namespace std;

struct NODE {
    int dp0max, dp1max, dp0min, dp1min;
};

vector<NODE> tree;
vector<int> a;
int n;

inline void upd(NODE& c, NODE& a, NODE& b) {
    c.dp0max = max(c.dp0max, a.dp0max + b.dp0max);
    if (b.dp1min != 0) {
        c.dp0max = max(c.dp0max, a.dp1max - b.dp1min);
    }

    if (b.dp1,ax != 0) {
        c.dp1max = max(c.dp1max, a.dp0max + b.dp1max);
    }
    c.dp1max = max(c.dp1max, a.dp1max - b.dp0min);

    c.dp0min = min(c.dp0min, a.dp0min + b.dp0min);
    if (b.dp1max != 0) {
        c.dp0min = min(c.dp0min, a.dp1min - b.dp1max);
    }

    if (b.dp1min != 0) {
        c.dp1min = min(c.dp1min, a.dp0min + b.dp1min);
    }
    c.dp1min = min(c.dp1min, a.dp1min - b.dp0max);
}

void inc(int v, int l, int r, int ind, int x) {
    if (l + 1 == r) {
        tree[v].dp0max = 0;
        tree[v].dp1max = x;
        tree[v].dp0min = 0;
        tree[v].dp1min = x;

        return;
    }

    int m = (l + r) / 2;
    if (ind < m) {
        inc(2 * v + 1, l, m, ind, x);
    } else {
        inc(2 * v + 2, m, r, ind, x);
    }

    upd(tree[v], tree[2 * v + 1], tree[2 * v + 2]);
}

void build(int v, int l, int r) {
    if (l + 1 == r) {
        tree[v].dp0max = 0;
        tree[v].dp1max = a[l];
        tree[v].dp0min = 0;
        tree[v].dp1min = a[l];

        return;
    }

    int m = (l + r) / 2;
    build(2 * v + 1, l, m);
    build(2 * v + 2, m, r);

    upd(tree[v], tree[2 * v + 1], tree[2 * v + 2]);

    cout << "!!! " << v << " " << l << " " << r << "\n";
    cout << tree[v].dp0max << " " << tree[v].dp1max << " " << tree[v].dp0min << " " << tree[v].dp1min << "\n\n";
}

signed main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

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

    int t, q, l, r, ans, x, y;
    cin >> t;

    NODE aaa;
    while (t--) {
        cin >> n >> q;
        a.resize(n);

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

        tree.assign(4 * n, {0, 0, 0, 0});
        build(0, 0, n);

        aaa = tree[0];
        ans = max(max(aaa.dp0max, aaa.dp1max), max(aaa.dp0min, aaa.dp1min));
        cout << ans << "\n";

        while (q--) {
            cin >> l >> r;
            l--, r--;

            x = a[l], y = a[r];
            swap(a[l], a[r]);

            inc(0, 0, n, l, y);
            inc(0, 0, n, r, x);

            aaa = tree[0];
            ans = max(max(aaa.dp0max, aaa.dp1max), max(aaa.dp0min, aaa.dp1min));
            cout << ans << "\n";
        }
    }

    return 0;
}


#include <bits/stdc++.h>

#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()

#define int long long

using namespace std;

struct NODE {
    int dp0max, dp1max, dp0min, dp1min;
};

vector<NODE> tree;
vector<int> a;
int n;

inline void upd(NODE& c, NODE& a, NODE& b) {
    c.dp0max = max(c.dp0max, a.dp0max + b.dp0max);
    if (b.dp1min != 0) {
        c.dp0max = max(c.dp0max, a.dp1max - b.dp1min);
    }

    if (b.dp1max != 0) {
        c.dp1max = max(c.dp1max, a.dp0max + b.dp1max);
    }
    c.dp1max = max(c.dp1max, a.dp1max - b.dp0min);

    c.dp0min = min(c.dp0min, a.dp0min + b.dp0min);
    if (b.dp1max != 0) {
        c.dp0min = min(c.dp0min, a.dp1min - b.dp1max);
    }

    if (b.dp1min != 0) {
        c.dp1min = min(c.dp1min, a.dp0min + b.dp1min);
    }
    c.dp1min = min(c.dp1min, a.dp1min - b.dp0max);
}

void inc(int v, int l, int r, int ind, int x) {
    if (l + 1 == r) {
        tree[v].dp0max = 0;
        tree[v].dp1max = x;
        tree[v].dp0min = 0;
        tree[v].dp1min = x;

        return;
    }

    int m = (l + r) / 2;
    if (ind < m) {
        inc(2 * v + 1, l, m, ind, x);
    } else {
        inc(2 * v + 2, m, r, ind, x);
    }

    upd(tree[v], tree[2 * v + 1], tree[2 * v + 2]);
}

void build(int v, int l, int r) {
    if (l + 1 == r) {
        tree[v].dp0max = 0;
        tree[v].dp1max = a[l];
        tree[v].dp0min = 0;
        tree[v].dp1min = a[l];

        return;
    }

    int m = (l + r) / 2;
    build(2 * v + 1, l, m);
    build(2 * v + 2, m, r);

    upd(tree[v], tree[2 * v + 1], tree[2 * v + 2]);

    //cout << "!!! " << v << " " << l << " " << r << "\n";
    cout << tree[v].dp0max << " " << tree[v].dp1max << " " << tree[v].dp0min << " " << tree[v].dp1min << "\n\n";
}

signed main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

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

    int t, q, l, r, ans, x, y;
    cin >> t;

    NODE aaa;
    while (t--) {
        cin >> n >> q;
        a.resize(n);

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

        tree.assign(4 * n, {0, 0, 0, 0});
        build(0, 0, n);

        aaa = tree[0];
        ans = max(max(aaa.dp0max, aaa.dp1max), max(aaa.dp0min, aaa.dp1min));
        cout << ans << "\n";

        while (q--) {
            cin >> l >> r;
            l--, r--;

            x = a[l], y = a[r];
            swap(a[l], a[r]);

            inc(0, 0, n, l, y);
            inc(0, 0, n, r, x);

            aaa = tree[0];
            ans = max(max(aaa.dp0max, aaa.dp1max), max(aaa.dp0min, aaa.dp1min));
            cout << ans << "\n";
        }
    }

    return 0;
}


#pragma GCC optimize("-O3")
#pragma GCC target("avx2")

#include <bits/stdc++.h>

#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()

using namespace std;

const long long INF = 1e16 + 109;

struct NODE {
    long long dp00, dp01, dp10, dp11;
};

vector<NODE> tree;
vector<int> a;
int n;

void upd(NODE& c, NODE& a, NODE& b) {
    c = {-INF, -INF, -INF, -INF};

    c.dp00 = max(c.dp00, a.dp00 + b.dp10);
    c.dp00 = max(c.dp00, a.dp01 + b.dp00);

    c.dp01 = max(c.dp01, a.dp00 + b.dp11);
    c.dp01 = max(c.dp01, a.dp01 + b.dp01);

    c.dp10 = max(c.dp10, a.dp10 + b.dp10);
    c.dp10 = max(c.dp10, a.dp11 + b.dp00);

    c.dp11 = max(c.dp11, a.dp10 + b.dp11);
    c.dp11 = max(c.dp11, a.dp11 + b.dp01);
}

void inc(int v, int l, int r, int ind, int x) {
    if (l + 1 == r) {
        tree[v].dp00 = -x;
        tree[v].dp01 = 0;
        tree[v].dp10 = 0;
        tree[v].dp11 = x;

        return;
    }

    int m = (l + r) / 2;
    if (ind < m) {
        inc(2 * v + 1, l, m, ind, x);
    } else {
        inc(2 * v + 2, m, r, ind, x);
    }

    upd(tree[v], tree[2 * v + 1], tree[2 * v + 2]);
}

void build(int v, int l, int r) {
    if (l + 1 == r) {
        tree[v].dp00 = -a[l];
        tree[v].dp01 = 0;
        tree[v].dp10 = 0;
        tree[v].dp11 = a[l];

        return;
    }

    int m = (l + r) / 2;
    build(2 * v + 1, l, m);
    build(2 * v + 2, m, r);

    upd(tree[v], tree[2 * v + 1], tree[2 * v + 2]);
}

signed main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

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

    int t, q, l, r, x, y;
    long long ans;
    cin >> t;

    NODE aaa;
    while (t--) {
        cin >> n >> q;
        a.resize(n);

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

        tree.resize(4 * n);
        build(0, 0, n);

        ans = tree[0].dp11;
        cout << ans << "\n";

        while (q--) {
            cin >> l >> r;
            l--, r--;

            swap(a[l], a[r]);

            inc(0, 0, n, l, a[l]);
            inc(0, 0, n, r, a[r]);

            ans = tree[0].dp11;
            cout << ans << "\n";
        }
    }

    return 0;
}


#include <bits/stdc++.h>

#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()

#define int long long

using namespace std;

struct NODE {
    int dp0max, dp1max, dp0min, dp1min;
};

vector<NODE> tree;
vector<int> a;
int n;

inline void upd(NODE& c, NODE& a, NODE& b) {
    c.dp0max = max(c.dp0max, a.dp0max + b.dp0max);
    if (b.dp1min != 0) {
        c.dp0max = max(c.dp0max, a.dp1max - b.dp1min);
    }

    if (b.dp1max != 0) {
        c.dp1max = max(c.dp1max, a.dp0max + b.dp1max);
    }
    c.dp1max = max(c.dp1max, a.dp1max - b.dp0min);

    c.dp0min = min(c.dp0min, a.dp0min + b.dp0min);
    if (b.dp1max != 0) {
        c.dp0min = min(c.dp0min, a.dp1min - b.dp1max);
    }

    if (b.dp1min != 0) {
        c.dp1min = min(c.dp1min, a.dp0min + b.dp1min);
    }
    c.dp1min = min(c.dp1min, a.dp1min - b.dp0max);
}

void inc(int v, int l, int r, int ind, int x) {
    if (l + 1 == r) {
        tree[v].dp0max = 0;
        tree[v].dp1max = x;
        tree[v].dp0min = 0;
        tree[v].dp1min = x;

        return;
    }

    int m = (l + r) / 2;
    if (ind < m) {
        inc(2 * v + 1, l, m, ind, x);
    } else {
        inc(2 * v + 2, m, r, ind, x);
    }

    upd(tree[v], tree[2 * v + 1], tree[2 * v + 2]);
}

void build(int v, int l, int r) {
    if (l + 1 == r) {
        tree[v].dp0max = 0;
        tree[v].dp1max = a[l];
        tree[v].dp0min = 0;
        tree[v].dp1min = a[l];

        return;
    }

    int m = (l + r) / 2;
    build(2 * v + 1, l, m);
    build(2 * v + 2, m, r);

    upd(tree[v], tree[2 * v + 1], tree[2 * v + 2]);

    //cout << "!!! " << v << " " << l << " " << r << "\n";
    //cout << tree[v].dp0max << " " << tree[v].dp1max << " " << tree[v].dp0min << " " << tree[v].dp1min << "\n\n";
}

signed main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

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

    int t, q, l, r, ans, x, y;
    cin >> t;

    NODE aaa;
    while (t--) {
        cin >> n >> q;
        a.resize(n);

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

        tree.assign(4 * n, {0, 0, 0, 0});
        build(0, 0, n);

        aaa = tree[0];
        ans = max(max(aaa.dp0max, aaa.dp1max), max(aaa.dp0min, aaa.dp1min));
        cout << ans << "\n";

        while (q--) {
            cin >> l >> r;
            l--, r--;

            x = a[l], y = a[r];
            swap(a[l], a[r]);

            inc(0, 0, n, l, y);
            inc(0, 0, n, r, x);

            aaa = tree[0];
            ans = max(max(aaa.dp0max, aaa.dp1max), max(aaa.dp0min, aaa.dp1min));
            cout << ans << "\n";
        }
    }

    return 0;
}


#include <bits/stdc++.h>

#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()

#define int long long

using namespace std;

struct NODE {
    int dp0max, dp1max, dp0min, dp1min;
};

vector<NODE> tree;
vector<int> a;
int n;

inline void upd(NODE& c, NODE& a, NODE& b) {
    c.dp0max = max(c.dp0max, a.dp0max + b.dp0max);
    if (b.dp1min != 0) {
        c.dp0max = max(c.dp0max, a.dp1max - b.dp1min);
    }

    if (b.dp1max != 0) {
        c.dp1max = max(c.dp1max, a.dp0max + b.dp1max);
    }
    c.dp1max = max(c.dp1max, a.dp1max - b.dp0min);

    c.dp0min = min(c.dp0min, a.dp0min + b.dp0min);
    if (b.dp1max != 0) {
        c.dp0min = min(c.dp0min, a.dp1min - b.dp1max);
    }

    if (b.dp1min != 0) {
        c.dp1min = min(c.dp1min, a.dp0min + b.dp1min);
    }
    c.dp1min = min(c.dp1min, a.dp1min - b.dp0max);
}

void inc(int v, int l, int r, int ind, int x) {
    if (l + 1 == r) {
        tree[v].dp0max = 0;
        tree[v].dp1max = x;
        tree[v].dp0min = 0;
        tree[v].dp1min = x;

        return;
    }

    int m = (l + r) / 2;
    if (ind < m) {
        inc(2 * v + 1, l, m, ind, x);
    } else {
        inc(2 * v + 2, m, r, ind, x);
    }

    upd(tree[v], tree[2 * v + 1], tree[2 * v + 2]);
}

void build(int v, int l, int r) {
    if (l + 1 == r) {
        tree[v].dp0max = 0;
        tree[v].dp1max = a[l];
        tree[v].dp0min = 0;
        tree[v].dp1min = a[l];

        return;
    }

    int m = (l + r) / 2;
    build(2 * v + 1, l, m);
    build(2 * v + 2, m, r);

    upd(tree[v], tree[2 * v + 1], tree[2 * v + 2]);

    cout << "!!! " << v << " " << l << " " << r << "\n";
    cout << tree[v].dp0max << " " << tree[v].dp1max << " " << tree[v].dp0min << " " << tree[v].dp1min << "\n\n";
}

signed main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

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

    int t, q, l, r, ans, x, y;
    cin >> t;

    NODE aaa;
    while (t--) {
        cin >> n >> q;
        a.resize(n);

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

        tree.assign(4 * n, {0, 0, 0, 0});
        build(0, 0, n);

        aaa = tree[0];
        ans = max(max(aaa.dp0max, aaa.dp1max), max(aaa.dp0min, aaa.dp1min));
        cout << ans << "\n";

        while (q--) {
            cin >> l >> r;
            l--, r--;

            x = a[l], y = a[r];
            swap(a[l], a[r]);

            inc(0, 0, n, l, y);
            inc(0, 0, n, r, x);

            aaa = tree[0];
            ans = max(max(aaa.dp0max, aaa.dp1max), max(aaa.dp0min, aaa.dp1min));
            cout << ans << "\n";
        }
    }

    return 0;
}


#pragma GCC optimize("-O3")
#pragma GCC target("avx2")

#include <bits/stdc++.h>

#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()

using namespace std;

const long long INF = 1e16 + 109;

struct NODE {
    long long dp00, dp01, dp10, dp11;
};

vector<NODE> tree;
vector<int> a;
int n;

inline void upd(NODE& c, NODE& a, NODE& b) {
    c = {-INF, -INF, -INF, -INF};

    c.dp00 = max(c.dp00, a.dp00 + b.dp10);
    c.dp00 = max(c.dp00, a.dp01 + b.dp00);

    c.dp01 = max(c.dp01, a.dp00 + b.dp11);
    c.dp01 = max(c.dp01, a.dp01 + b.dp01);

    c.dp10 = max(c.dp10, a.dp10 + b.dp10);
    c.dp10 = max(c.dp10, a.dp11 + b.dp00);

    c.dp11 = max(c.dp11, a.dp10 + b.dp11);
    c.dp11 = max(c.dp11, a.dp11 + b.dp01);
}

inline void inc(int v, int l, int r, int ind, int x) {
    if (l + 1 == r) {
        tree[v].dp00 = -x;
        tree[v].dp01 = 0;
        tree[v].dp10 = 0;
        tree[v].dp11 = x;

        return;
    }

    int m = (l + r) / 2;
    if (ind < m) {
        inc(2 * v + 1, l, m, ind, x);
    } else {
        inc(2 * v + 2, m, r, ind, x);
    }

    upd(tree[v], tree[2 * v + 1], tree[2 * v + 2]);
}

inline void build(int v, int l, int r) {
    if (l + 1 == r) {
        tree[v].dp00 = -a[l];
        tree[v].dp01 = 0;
        tree[v].dp10 = 0;
        tree[v].dp11 = a[l];

        return;
    }

    int m = (l + r) / 2;
    build(2 * v + 1, l, m);
    build(2 * v + 2, m, r);

    upd(tree[v], tree[2 * v + 1], tree[2 * v + 2]);
}

signed main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

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

    int t, q, l, r, x, y;
    long long ans;
    cin >> t;

    NODE aaa;
    while (t--) {
        cin >> n >> q;
        a.resize(n);

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

        tree.resize(4 * n);
        build(0, 0, n);

        ans = tree[0].dp11;
        cout << ans << "\n";

        while (q--) {
            cin >> l >> r;
            l--, r--;

            swap(a[l], a[r]);

            inc(0, 0, n, l, a[l]);
            inc(0, 0, n, r, a[r]);

            ans = tree[0].dp11;
            cout << ans << "\n";
        }
    }

    return 0;
}


#pragma GCC optimize("-O3")
#pragma GCC target("avx2")

#include <bits/stdc++.h>

#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()

using namespace std;

const long long INF = 1e16 + 109;

struct NODE {
    long long dp00, dp01, dp10, dp11;
};

vector<NODE> tree;
vector<int> a;
int n;

inline void upd(NODE& c, NODE& a, NODE& b) {
    c = {-INF, -INF, -INF, -INF};

    c.dp00 = max(c.dp00, a.dp00 + b.dp10);
    c.dp00 = max(c.dp00, a.dp01 + b.dp00);

    c.dp01 = max(c.dp01, a.dp00 + b.dp11);
    c.dp01 = max(c.dp01, a.dp01 + b.dp01);

    c.dp10 = max(c.dp10, a.dp10 + b.dp10);
    c.dp10 = max(c.dp10, a.dp11 + b.dp00);

    c.dp11 = max(c.dp11, a.dp10 + b.dp11);
    c.dp11 = max(c.dp11, a.dp11 + b.dp01);
}

void inc(int v, int l, int r, int ind, int x) {
    if (l + 1 == r) {
        tree[v].dp00 = -x;
        tree[v].dp01 = 0;
        tree[v].dp10 = 0;
        tree[v].dp11 = x;

        return;
    }

    int m = (l + r) / 2;
    if (ind < m) {
        inc(2 * v + 1, l, m, ind, x);
    } else {
        inc(2 * v + 2, m, r, ind, x);
    }

    upd(tree[v], tree[2 * v + 1], tree[2 * v + 2]);
}

void build(int v, int l, int r) {
    if (l + 1 == r) {
        tree[v].dp00 = -a[l];
        tree[v].dp01 = 0;
        tree[v].dp10 = 0;
        tree[v].dp11 = a[l];

        return;
    }

    int m = (l + r) / 2;
    build(2 * v + 1, l, m);
    build(2 * v + 2, m, r);

    upd(tree[v], tree[2 * v + 1], tree[2 * v + 2]);
}

signed main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

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

    int t, q, l, r, x, y;
    long long ans;
    cin >> t;

    NODE aaa;
    while (t--) {
        cin >> n >> q;
        a.resize(n);

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

        tree.resize(4 * n);
        build(0, 0, n);

        ans = tree[0].dp11;
        cout << ans << "\n";

        while (q--) {
            cin >> l >> r;
            l--, r--;

            x = a[l], y = a[r];
            swap(a[l], a[r]);

            inc(0, 0, n, l, y);
            inc(0, 0, n, r, x);

            ans = tree[0].dp11;
            cout << ans << "\n";
        }
    }

    return 0;
}


/********************************
* AUTHOR:  SUBHADIP MAJI         *
* NICK  :  atheistsoldier        *
* INSTITUTION: IIEST, SHIBPUR    *
********************************/

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

typedef long long ll;

typedef unsigned long long ull;
typedef string str;
typedef long double ld;

typedef vector < long long >vll;

typedef vector < string > vs;
typedef map<int,int>                   MII;
typedef map<ll,ll>                     MLL;
typedef map<str,int>                   MSI;
typedef map<char,int>                  MCI;
typedef map<int,str>                   MIS;
typedef set<str>                       SS;
typedef set<int>                       SI;
typedef set<ll>                        SLL;

#define f(i, a, n) for(ll i=a; i<(ll)n; i++)
#define f1(i, a, b) for(ll i=a; i<=(ll)b; i++)
#define f2(i, a, b) for(ll i=a;i>=(ll)b;i--)
#define lcm(a,b)		(a*b)/__gcd(a,b)
#define bs(v,x)			binary_search(all(v),x)
#define ub(v,x)			upper_bound(all(v),x)
#define lb(v,x)			lower_bound(all(v),x)
#define take(A,n)		{f(i,0,n)cin>>A[i];}
#define pob				pop_back
#define eb				emplace_back
#define pf				push_front
#define pof				pop_front
#define mkp				make_pair
#define all(a)			a.begin(),a.end()
#define PB push_back
#define FF first
#define SS second
const ll MOD =998244353 ;

const ll INF = LLONG_MAX;

const ld PI = 3.141592653589793238463;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	cout.tie(NULL);
    ll T;
	cin>>T;
	while(T--)
	{
		ll n,q;
		cin>>n>>q;
		ll a[n];
		take(a,n);
		set<ll> peaks,depression;
		ll ans=0;
		if(n==1)
		ans=1;
		else
		{
			f(i,0,n)
			{
			if(i==0)
				{
					if(a[i]>a[i+1])
					{
						peaks.insert(i);//storing the peak indexes
						ans+=a[i];
					}

				}
				else if(i==n-1)
				{
					if(a[i]>a[i-1])
					{
						peaks.insert(i);//storing the peak indexes
						ans+=a[i];
					}

				}
				else
				{
					if((a[i]>a[i+1])&&(a[i]>a[i-1]))
					{
						peaks.insert(i);//storing the peak indexes
						ans+=a[i];
					}

					else if((a[i]<a[i+1])&&(a[i]<a[i-1]))
					{
						depression.insert(i);//storing the depression indexes
						ans-=a[i];
					}

				}

			}
		}

		set<ll> s;
		cout<<ans<<"\n";
		while(q--)
		{
			ll l,r;
			cin>>l>>r;
			if(l==r)
			{
				cout<<ans<<"\n";
				continue;
			}

			s.clear();
			s.insert(l-1);
			if(l>1)
			s.insert(l-2);
			if(l<n)
			s.insert(l);
			s.insert(r-1);
			if(r>1)
			s.insert(r-2);
			if(r<n)
			s.insert(r);
			for(auto i:s)
			{
				auto it1=peaks.find(i);
				auto it2=depression.find(i);
				if(it1!=peaks.end())
				{
					ans-=a[i];
					peaks.erase(it1);
				}
				if(it2!=depression.end())
				{
					ans+=a[i];
					depression.erase(it2);
				}
			}

			a[l-1]=a[l-1]^a[r-1];
			a[r-1]=a[l-1]^a[r-1];
			a[l-1]=a[l-1]^a[r-1];
			if(n==1)
			ans=1;
			else
			{
			for(auto i:s)
			{
				if(i==0)
				{
					if(a[i]>a[i+1])
					{
						peaks.insert(i);
						ans+=a[i];
					}

				}
				else if(i==n-1)
				{
					if(a[i]>a[i-1])
					{
						peaks.insert(i);
						ans+=a[i];
					}

				}
				else
				{
					if((a[i]>a[i+1])&&(a[i]>a[i-1]))
					{
						peaks.insert(i);
						ans+=a[i];
					}

					else if((a[i]<a[i+1])&&(a[i]<a[i-1]))
					{
						depression.insert(i);
						ans-=a[i];
					}

				}
			}
			}

			cout<<ans<<"\n";
		}
	}
	return 0;
}


/********************************
* AUTHOR:  SUBHADIP MAJI         *
* NICK  :  atheistsoldier        *
* INSTITUTION: IIEST, SHIBPUR    *
********************************/

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

typedef long long ll;

typedef unsigned long long ull;
typedef string str;
typedef long double ld;

typedef vector < long long >vll;

typedef vector < string > vs;
typedef map<int,int>                   MII;
typedef map<ll,ll>                     MLL;
typedef map<str,int>                   MSI;
typedef map<char,int>                  MCI;
typedef map<int,str>                   MIS;
typedef set<str>                       SS;
typedef set<int>                       SI;
typedef set<ll>                        SLL;

#define f(i, a, n) for(ll i=a; i<(ll)n; i++)
#define f1(i, a, b) for(ll i=a; i<=(ll)b; i++)
#define f2(i, a, b) for(ll i=a;i>=(ll)b;i--)
#define lcm(a,b)		(a*b)/__gcd(a,b)
#define bs(v,x)			binary_search(all(v),x)
#define ub(v,x)			upper_bound(all(v),x)
#define lb(v,x)			lower_bound(all(v),x)
#define take(A,n)		{f(i,0,n)cin>>A[i];}
#define pob				pop_back
#define eb				emplace_back
#define pf				push_front
#define pof				pop_front
#define mkp				make_pair
#define all(a)			a.begin(),a.end()
#define PB push_back
#define FF first
#define SS second
const ll MOD =998244353 ;

const ll INF = LLONG_MAX;

const ld PI = 3.141592653589793238463;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll T;
	cin>>T;
	while(T--)
	{
		ll n,q;
		cin>>n>>q;
		ll a[n];
		take(a,n);
		set<ll> peaks,depression;
		ll ans=0;
		if(n==1)
		ans=1;
		else
		{
			f(i,0,n)
			{
			if(i==0)
				{
					if(a[i]>a[i+1])
					{
						peaks.insert(i);
						ans+=a[i];
					}

				}
				else if(i==n-1)
				{
					if(a[i]>a[i-1])
					{
						peaks.insert(i);
						ans+=a[i];
					}

				}
				else
				{
					if((a[i]>a[i+1])&&(a[i]>a[i-1]))
					{
						peaks.insert(i);
						ans+=a[i];
					}

					else if((a[i]<a[i+1])&&(a[i]<a[i-1]))
					{
						depression.insert(i);
						ans-=a[i];
					}

				}

			}
		}

		cout<<ans<<endl;
		while(q--)
		{
			ll l,r;
			cin>>l>>r;

			set<ll> s;

			s.insert(l-1);
			if(l>1)
			s.insert(l-2);
			if(l<n)
			s.insert(l);
			s.insert(r-1);
			if(r>1)
			s.insert(r-2);
			if(r<n)
			s.insert(r);
			for(auto it:s)
			{

				if(peaks.count(it))
				{
					ans-=a[it];
					peaks.erase(it);
				}
				if(depression.count(it))
				{
					ans+=a[it];
					depression.erase(it);
				}
			}

			swap(a[l-1],a[r-1]);
			if(n==1)
			ans=1;
			else
			{
			for(auto i:s)
			{
				if(i==0)
				{
					if(a[i]>a[i+1])
					{
						peaks.insert(i);
						ans+=a[i];
					}

				}
				else if(i==n-1)
				{
					if(a[i]>a[i-1])
					{
						peaks.insert(i);
						ans+=a[i];
					}

				}
				else
				{
					if((a[i]>a[i+1])&&(a[i]>a[i-1]))
					{
						peaks.insert(i);
						ans+=a[i];
					}

					else if((a[i]<a[i+1])&&(a[i]<a[i-1]))
					{
						depression.insert(i);
						ans-=a[i];
					}

				}
			}
			}

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


/********************************
* AUTHOR:  SUBHADIP MAJI         *
* NICK  :  atheistsoldier        *
* INSTITUTION: IIEST, SHIBPUR    *
********************************/

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

typedef long long ll;

typedef unsigned long long ull;
typedef string str;
typedef long double ld;

typedef vector < long long >vll;

typedef vector < string > vs;
typedef map<int,int>                   MII;
typedef map<ll,ll>                     MLL;
typedef map<str,int>                   MSI;
typedef map<char,int>                  MCI;
typedef map<int,str>                   MIS;
typedef set<str>                       SS;
typedef set<int>                       SI;
typedef set<ll>                        SLL;

#define f(i, a, n) for(ll i=a; i<(ll)n; i++)
#define f1(i, a, b) for(ll i=a; i<=(ll)b; i++)
#define f2(i, a, b) for(ll i=a;i>=(ll)b;i--)
#define lcm(a,b)		(a*b)/__gcd(a,b)
#define bs(v,x)			binary_search(all(v),x)
#define ub(v,x)			upper_bound(all(v),x)
#define lb(v,x)			lower_bound(all(v),x)
#define take(A,n)		{f(i,0,n)cin>>A[i];}
#define pob				pop_back
#define eb				emplace_back
#define pf				push_front
#define pof				pop_front
#define mkp				make_pair
#define all(a)			a.begin(),a.end()
#define PB push_back
#define FF first
#define SS second
const ll MOD =998244353 ;

const ll INF = LLONG_MAX;

const ld PI = 3.141592653589793238463;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll T;
	cin>>T;
	while(T--)
	{
		ll n,q;
		cin>>n>>q;
		ll a[n];
		take(a,n);
		set<ll> peaks,depression;
		ll ans=0;
		f(i,0,n)
		{
			if(i==0)
				{
					if(a[i]>a[i+1])
					{
						peaks.insert(i);
						ans+=a[i];
					}

				}
				else if(i==n-1)
				{
					if(a[i]>a[i-1])
					{
						peaks.insert(i);
						ans+=a[i];
					}

				}
				else
				{
					if((a[i]>a[i+1])&&(a[i]>a[i-1]))
					{
						peaks.insert(i);
						ans+=a[i];
					}

					else if((a[i]<a[i+1])&&(a[i]<a[i-1]))
					{
						depression.insert(i);
						ans-=a[i];
					}

				}

		}
		cout<<ans<<endl;
		while(q--)
		{
			ll l,r;
			cin>>l>>r;

			set<ll> s;

			s.insert(l-1);
			if(l>1)
			s.insert(l-2);
			if(l<n)
			s.insert(l);
			s.insert(r-1);
			if(r>1)
			s.insert(r-2);
			if(r<n)
			s.insert(r);
			for(auto it:s)
			{

				if(peaks.count(it))
				{
					ans-=a[it];
					peaks.erase(it);
				}
				if(depression.count(it))
				{
					ans+=a[it];
					depression.erase(it);
				}
			}

			swap(a[l-1],a[r-1]);
			for(auto i:s)
			{
				if(i==0)
				{
					if(a[i]>a[i+1])
					{
						peaks.insert(i);
						ans+=a[i];
					}

				}
				else if(i==n-1)
				{
					if(a[i]>a[i-1])
					{
						peaks.insert(i);
						ans+=a[i];
					}

				}
				else
				{
					if((a[i]>a[i+1])&&(a[i]>a[i-1]))
					{
						peaks.insert(i);
						ans+=a[i];
					}

					else if((a[i]<a[i+1])&&(a[i]<a[i-1]))
					{
						depression.insert(i);
						ans-=a[i];
					}

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


/********************************
* AUTHOR:  SUBHADIP MAJI         *
* NICK  :  atheistsoldier        *
* INSTITUTION: IIEST, SHIBPUR    *
********************************/

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

typedef long long ll;

typedef unsigned long long ull;
typedef string str;
typedef long double ld;

typedef vector < long long >vll;

typedef vector < string > vs;
typedef map<int,int>                   MII;
typedef map<ll,ll>                     MLL;
typedef map<str,int>                   MSI;
typedef map<char,int>                  MCI;
typedef map<int,str>                   MIS;
typedef set<str>                       SS;
typedef set<int>                       SI;
typedef set<ll>                        SLL;

#define f(i, a, n) for(ll i=a; i<(ll)n; i++)
#define f1(i, a, b) for(ll i=a; i<=(ll)b; i++)
#define f2(i, a, b) for(ll i=a;i>=(ll)b;i--)
#define lcm(a,b)		(a*b)/__gcd(a,b)
#define bs(v,x)			binary_search(all(v),x)
#define ub(v,x)			upper_bound(all(v),x)
#define lb(v,x)			lower_bound(all(v),x)
#define take(A,n)		{f(i,0,n)cin>>A[i];}
#define pob				pop_back
#define eb				emplace_back
#define pf				push_front
#define pof				pop_front
#define mkp				make_pair
#define all(a)			a.begin(),a.end()
#define PB push_back
#define FF first
#define SS second
const ll MOD =998244353 ;

const ll INF = LLONG_MAX;

const ld PI = 3.141592653589793238463;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	cout.tie(NULL);
    ll T;
	cin>>T;
	while(T--)
	{
		ll n,q;
		cin>>n>>q;
		ll a[n];
		take(a,n);
		set<ll> peaks,depression;
		ll ans=0;
		if(n==1)
		ans=1;
		else
		{
			f(i,0,n)
			{
			if(i==0)
				{
					if(a[i]>a[i+1])
					{
						peaks.insert(i);//storing the peak indexes
						ans+=a[i];
					}

				}
				else if(i==n-1)
				{
					if(a[i]>a[i-1])
					{
						peaks.insert(i);//storing the peak indexes
						ans+=a[i];
					}

				}
				else
				{
					if((a[i]>a[i+1])&&(a[i]>a[i-1]))
					{
						peaks.insert(i);//storing the peak indexes
						ans+=a[i];
					}

					else if((a[i]<a[i+1])&&(a[i]<a[i-1]))
					{
						depression.insert(i);//storing the depression indexes
						ans-=a[i];
					}

				}

			}
		}

		cout<<ans<<"\n";
		while(q--)
		{
			ll l,r;
			cin>>l>>r;

			set<ll> s;

			s.insert(l-1);
			if(l>1)
			s.insert(l-2);
			if(l<n)
			s.insert(l);
			s.insert(r-1);
			if(r>1)
			s.insert(r-2);
			if(r<n)
			s.insert(r);
			for(auto i:s)
			{
				auto it1=peaks.find(i);
				auto it2=depression.find(i);
				if(it1!=peaks.end())
				{
					ans-=a[i];
					peaks.erase(it1);
				}
				if(it2!=depression.end())
				{
					ans+=a[i];
					depression.erase(it2);
				}
			}

			a[l-1]=a[l-1]^a[r-1];
			a[r-1]=a[l-1]^a[r-1];
			a[l-1]=a[l-1]^a[r-1];
			if(n==1)
			ans=1;
			else
			{
			for(auto i:s)
			{
				if(i==0)
				{
					if(a[i]>a[i+1])
					{
						peaks.insert(i);
						ans+=a[i];
					}

				}
				else if(i==n-1)
				{
					if(a[i]>a[i-1])
					{
						peaks.insert(i);
						ans+=a[i];
					}

				}
				else
				{
					if((a[i]>a[i+1])&&(a[i]>a[i-1]))
					{
						peaks.insert(i);
						ans+=a[i];
					}

					else if((a[i]<a[i+1])&&(a[i]<a[i-1]))
					{
						depression.insert(i);
						ans-=a[i];
					}

				}
			}
			}

			cout<<ans<<"\n";
		}
	}
	return 0;
}


/********************************
* AUTHOR:  SUBHADIP MAJI         *
* NICK  :  atheistsoldier        *
* INSTITUTION: IIEST, SHIBPUR    *
********************************/

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

typedef long long ll;

typedef unsigned long long ull;
typedef string str;
typedef long double ld;

typedef vector < long long >vll;

typedef vector < string > vs;
typedef map<int,int>                   MII;
typedef map<ll,ll>                     MLL;
typedef map<str,int>                   MSI;
typedef map<char,int>                  MCI;
typedef map<int,str>                   MIS;
typedef set<str>                       SS;
typedef set<int>                       SI;
typedef set<ll>                        SLL;

#define f(i, a, n) for(ll i=a; i<(ll)n; i++)
#define f1(i, a, b) for(ll i=a; i<=(ll)b; i++)
#define f2(i, a, b) for(ll i=a;i>=(ll)b;i--)
#define lcm(a,b)		(a*b)/__gcd(a,b)
#define bs(v,x)			binary_search(all(v),x)
#define ub(v,x)			upper_bound(all(v),x)
#define lb(v,x)			lower_bound(all(v),x)
#define take(A,n)		{f(i,0,n)cin>>A[i];}
#define pob				pop_back
#define eb				emplace_back
#define pf				push_front
#define pof				pop_front
#define mkp				make_pair
#define all(a)			a.begin(),a.end()
#define PB push_back
#define FF first
#define SS second
const ll MOD =998244353 ;

const ll INF = LLONG_MAX;

const ld PI = 3.141592653589793238463;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	cout.tie(NULL);
    ll T;
	cin>>T;
	while(T--)
	{
		ll n,q;
		cin>>n>>q;
		ll a[n];
		take(a,n);
		set<ll> peaks,depression;
		ll ans=0;
		if(n==1)
		ans=1;
		else
		{
			f(i,0,n)
			{
			if(i==0)
				{
					if(a[i]>a[i+1])
					{
						peaks.insert(i);//storing the peak indexes
						ans+=a[i];
					}

				}
				else if(i==n-1)
				{
					if(a[i]>a[i-1])
					{
						peaks.insert(i);//storing the peak indexes
						ans+=a[i];
					}

				}
				else
				{
					if((a[i]>a[i+1])&&(a[i]>a[i-1]))
					{
						peaks.insert(i);//storing the peak indexes
						ans+=a[i];
					}

					else if((a[i]<a[i+1])&&(a[i]<a[i-1]))
					{
						depression.insert(i);//storing the depression indexes
						ans-=a[i];
					}

				}

			}
		}

		cout<<ans<<"\n";
		while(q--)
		{
			ll l,r;
			cin>>l>>r;

			set<ll> s;

			s.insert(l-1);
			if(l>1)
			s.insert(l-2);
			if(l<n)
			s.insert(l);
			s.insert(r-1);
			if(r>1)
			s.insert(r-2);
			if(r<n)
			s.insert(r);
			for(auto i:s)
			{
				auto it1=peaks.find(i);
				auto it2=depression.find(i);
				if(it1!=peaks.end())
				{
					ans-=a[i];
					peaks.erase(it1);
				}
				if(it2!=depression.end())
				{
					ans+=a[i];
					depression.erase(it2);
				}
			}

			swap(a[l-1],a[r-1]);
			if(n==1)
			ans=1;
			else
			{
			for(auto i:s)
			{
				if(i==0)
				{
					if(a[i]>a[i+1])
					{
						peaks.insert(i);
						ans+=a[i];
					}

				}
				else if(i==n-1)
				{
					if(a[i]>a[i-1])
					{
						peaks.insert(i);
						ans+=a[i];
					}

				}
				else
				{
					if((a[i]>a[i+1])&&(a[i]>a[i-1]))
					{
						peaks.insert(i);
						ans+=a[i];
					}

					else if((a[i]<a[i+1])&&(a[i]<a[i-1]))
					{
						depression.insert(i);
						ans-=a[i];
					}

				}
			}
			}

			cout<<ans<<"\n";
		}
	}
	return 0;
}


/********************************
* AUTHOR:  SUBHADIP MAJI         *
* NICK  :  atheistsoldier        *
* INSTITUTION: IIEST, SHIBPUR    *
********************************/

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

typedef long long ll;

typedef unsigned long long ull;
typedef string str;
typedef long double ld;

typedef vector < long long >vll;

typedef vector < string > vs;
typedef map<int,int>                   MII;
typedef map<ll,ll>                     MLL;
typedef map<str,int>                   MSI;
typedef map<char,int>                  MCI;
typedef map<int,str>                   MIS;
typedef set<str>                       SS;
typedef set<int>                       SI;
typedef set<ll>                        SLL;

#define f(i, a, n) for(ll i=a; i<(ll)n; i++)
#define f1(i, a, b) for(ll i=a; i<=(ll)b; i++)
#define f2(i, a, b) for(ll i=a;i>=(ll)b;i--)
#define lcm(a,b)		(a*b)/__gcd(a,b)
#define bs(v,x)			binary_search(all(v),x)
#define ub(v,x)			upper_bound(all(v),x)
#define lb(v,x)			lower_bound(all(v),x)
#define take(A,n)		{f(i,0,n)cin>>A[i];}
#define pob				pop_back
#define eb				emplace_back
#define pf				push_front
#define pof				pop_front
#define mkp				make_pair
#define all(a)			a.begin(),a.end()
#define PB push_back
#define FF first
#define SS second
const ll MOD =998244353 ;

const ll INF = LLONG_MAX;

const ld PI = 3.141592653589793238463;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll T;
	cin>>T;
	while(T--)
	{
		ll n,q;
		cin>>n>>q;
		ll a[n];
		take(a,n);
		set<ll> peaks,depression;
		ll ans=0;
		if(n==1)
		ans=1;
		else
		{
			f(i,0,n)
			{
			if(i==0)
				{
					if(a[i]>a[i+1])
					{
						peaks.insert(i);
						ans+=a[i];
					}

				}
				else if(i==n-1)
				{
					if(a[i]>a[i-1])
					{
						peaks.insert(i);
						ans+=a[i];
					}

				}
				else
				{
					if((a[i]>a[i+1])&&(a[i]>a[i-1]))
					{
						peaks.insert(i);
						ans+=a[i];
					}

					else if((a[i]<a[i+1])&&(a[i]<a[i-1]))
					{
						depression.insert(i);
						ans-=a[i];
					}

				}

			}
		}

		cout<<ans<<endl;
		while(q--)
		{
			ll l,r;
			cin>>l>>r;

			set<ll> s;

			s.insert(l-1);
			if(l>1)
			s.insert(l-2);
			if(l<n)
			s.insert(l);
			s.insert(r-1);
			if(r>1)
			s.insert(r-2);
			if(r<n)
			s.insert(r);
			for(auto i:s)
			{
				auto it1=peaks.find(i);
				auto it2=depression.find(i);
				if(it1!=peaks.end())
				{
					ans-=a[i];
					peaks.erase(it1);
				}
				if(it2!=depression.end())
				{
					ans+=a[i];
					depression.erase(it2);
				}
			}

			swap(a[l-1],a[r-1]);
			if(n==1)
			ans=1;
			else
			{
			for(auto i:s)
			{
				if(i==0)
				{
					if(a[i]>a[i+1])
					{
						peaks.insert(i);
						ans+=a[i];
					}

				}
				else if(i==n-1)
				{
					if(a[i]>a[i-1])
					{
						peaks.insert(i);
						ans+=a[i];
					}

				}
				else
				{
					if((a[i]>a[i+1])&&(a[i]>a[i-1]))
					{
						peaks.insert(i);
						ans+=a[i];
					}

					else if((a[i]<a[i+1])&&(a[i]<a[i-1]))
					{
						depression.insert(i);
						ans-=a[i];
					}

				}
			}
			}

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


/********************************
* AUTHOR:  SUBHADIP MAJI         *
* NICK  :  atheistsoldier        *
* INSTITUTION: IIEST, SHIBPUR    *
********************************/

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

typedef long long ll;

typedef unsigned long long ull;
typedef string str;
typedef long double ld;

typedef vector < long long >vll;

typedef vector < string > vs;
typedef map<int,int>                   MII;
typedef map<ll,ll>                     MLL;
typedef map<str,int>                   MSI;
typedef map<char,int>                  MCI;
typedef map<int,str>                   MIS;
typedef set<str>                       SS;
typedef set<int>                       SI;
typedef set<ll>                        SLL;

#define f(i, a, n) for(ll i=a; i<(ll)n; i++)
#define f1(i, a, b) for(ll i=a; i<=(ll)b; i++)
#define f2(i, a, b) for(ll i=a;i>=(ll)b;i--)
#define lcm(a,b)		(a*b)/__gcd(a,b)
#define bs(v,x)			binary_search(all(v),x)
#define ub(v,x)			upper_bound(all(v),x)
#define lb(v,x)			lower_bound(all(v),x)
#define take(A,n)		{f(i,0,n)cin>>A[i];}
#define pob				pop_back
#define eb				emplace_back
#define pf				push_front
#define pof				pop_front
#define mkp				make_pair
#define all(a)			a.begin(),a.end()
#define PB push_back
#define FF first
#define SS second
const ll MOD =998244353 ;

const ll INF = LLONG_MAX;

const ld PI = 3.141592653589793238463;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	cout.tie(NULL);
    ll T;
	cin>>T;
	while(T--)
	{
		ll n,q;
		cin>>n>>q;
		ll a[n];
		take(a,n);
		set<ll> peaks,depression;
		ll ans=0;
		if(n==1)
		ans=1;
		else
		{
			f(i,0,n)
			{
			if(i==0)
				{
					if(a[i]>a[i+1])
					{
						peaks.insert(i);//storing the peak indexes
						ans+=a[i];
					}

				}
				else if(i==n-1)
				{
					if(a[i]>a[i-1])
					{
						peaks.insert(i);//storing the peak indexes
						ans+=a[i];
					}

				}
				else
				{
					if((a[i]>a[i+1])&&(a[i]>a[i-1]))
					{
						peaks.insert(i);//storing the peak indexes
						ans+=a[i];
					}

					else if((a[i]<a[i+1])&&(a[i]<a[i-1]))
					{
						depression.insert(i);//storing the depression indexes
						ans-=a[i];
					}

				}

			}
		}

		cout<<ans<<"\n";
		while(q--)
		{
			ll l,r;
			cin>>l>>r;
			if(l==r)
			{
				cout<<ans<<"\n";
				continue;
			}
			set<ll> s;

			s.insert(l-1);
			if(l>1)
			s.insert(l-2);
			if(l<n)
			s.insert(l);
			s.insert(r-1);
			if(r>1)
			s.insert(r-2);
			if(r<n)
			s.insert(r);
			for(auto i:s)
			{
				auto it1=peaks.find(i);
				auto it2=depression.find(i);
				if(it1!=peaks.end())
				{
					ans-=a[i];
					peaks.erase(it1);
				}
				if(it2!=depression.end())
				{
					ans+=a[i];
					depression.erase(it2);
				}
			}

			a[l-1]=a[l-1]^a[r-1];
			a[r-1]=a[l-1]^a[r-1];
			a[l-1]=a[l-1]^a[r-1];
			if(n==1)
			ans=1;
			else
			{
			for(auto i:s)
			{
				if(i==0)
				{
					if(a[i]>a[i+1])
					{
						peaks.insert(i);
						ans+=a[i];
					}

				}
				else if(i==n-1)
				{
					if(a[i]>a[i-1])
					{
						peaks.insert(i);
						ans+=a[i];
					}

				}
				else
				{
					if((a[i]>a[i+1])&&(a[i]>a[i-1]))
					{
						peaks.insert(i);
						ans+=a[i];
					}

					else if((a[i]<a[i+1])&&(a[i]<a[i-1]))
					{
						depression.insert(i);
						ans-=a[i];
					}

				}
			}
			}

			cout<<ans<<"\n";
		}
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<double,double> pdd;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<pll> vll;
typedef vector<pdd> vdd;
typedef vector<vl> vvl;
typedef vector<vd> vvd;
const int mod = 1000000007;
const vi mods = {998244353,1000000007,1000000009};
const ll inf = 3e18;
const double pi = acos(-1.0);
const double eps = 0.0000001;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define rep(i,a,b) for(ll i=(a);i<(b);++i)
#define trav(a,x) for(auto&a:x)
#define all(x) begin(x),end(x)
#define sz(x) ((int)(x).size())
#define fll(xs,x,a) fill(&x,&x+sizeof(xs)/sizeof(x)-1,a)//fll(dp,dp[0][0],inf)
#define has(x,a) (x.find(a)!=end(x))
#define pc(x) __builtin_popcountll(x)
#define pb push_back
#define fi first
#define se second
template<class...K> using umap = unordered_map<K...>;
template<class...K> using uset = unordered_set<K...>;

struct Tree {
	inline vl f(const vl& a, const vl& b) const {
    return {
      max(max(a[0],b[0]),max(a[0]+b[3],a[1]+b[0])),
      max(max(a[1],b[1]),max(a[0]+b[2],a[1]+b[1])),
      max(max(a[2],b[2]),max(a[2]+b[1],a[3]+b[2])),
      max(max(a[3],b[3]),max(a[2]+b[0],a[3]+b[3])),
    };
  }
  void fa(vl& a, const vl& b) const {
    a = {
      max(max(a[0],b[0]),max(a[0]+b[3],a[1]+b[0])),
      max(max(a[1],b[1]),max(a[0]+b[2],a[1]+b[1])),
      max(max(a[2],b[2]),max(a[2]+b[1],a[3]+b[2])),
      max(max(a[3],b[3]),max(a[2]+b[0],a[3]+b[3])),
    };
  }
  void fb(const vl& a, vl& b) const {
    b = {
      max(max(a[0],b[0]),max(a[0]+b[3],a[1]+b[0])),
      max(max(a[1],b[1]),max(a[0]+b[2],a[1]+b[1])),
      max(max(a[2],b[2]),max(a[2]+b[1],a[3]+b[2])),
      max(max(a[3],b[3]),max(a[2]+b[0],a[3]+b[3])),
    };
  }
	vector<vl> s; int n;
	Tree(int n = 0) : s(2*n, vl{0,0,0,0}), n(n) {}
	void update(int pos, int val) {
    pos+=n;
    s[pos]={val,-inf,-val,-inf};
		for (; pos /= 2;)
			s[pos] = f(s[pos * 2], s[pos * 2 + 1]);
	}
	vl query(int b, int e) { // query [b, e)
		vl ra = {-inf,-inf,-inf,-inf}, rb = {-inf,-inf,-inf,-inf};
		for (b += n, e += n; b < e; b /= 2, e /= 2) {
			if (b % 2) fa(ra, s[b++]);
			if (e % 2) fb(s[--e], rb);
		}
		return f(ra, rb);
	}
};

ll n,q;
vl a;
ll l,r;

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cout.precision(13);

  ll t;
  cin>>t;
  rep(_t,0,t){
    cin>>n>>q;
    a.assign(n,0);
    rep(i,0,n)cin>>a[i];
    Tree seg(n);
    rep(i,0,n)seg.update(i,a[i]);
    vl qq=seg.query(0,n);
    cout<<max(qq[0],qq[1])<<"\n";
    rep(_q,0,q){
      cin>>l>>r;
      swap(a[--l],a[--r]);
      seg.update(l,a[l]);
      seg.update(r,a[r]);
      qq=seg.query(0,n);
      cout<<max(qq[0],qq[1])<<"\n";
    }
  }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<double,double> pdd;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<pll> vll;
typedef vector<pdd> vdd;
typedef vector<vl> vvl;
typedef vector<vd> vvd;
const int mod = 1000000007;
const vi mods = {998244353,1000000007,1000000009};
const ll inf = 3e18;
const double pi = acos(-1.0);
const double eps = 0.0000001;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define rep(i,a,b) for(ll i=(a);i<(b);++i)
#define trav(a,x) for(auto&a:x)
#define all(x) begin(x),end(x)
#define sz(x) ((int)(x).size())
#define fll(xs,x,a) fill(&x,&x+sizeof(xs)/sizeof(x)-1,a)//fll(dp,dp[0][0],inf)
#define has(x,a) (x.find(a)!=end(x))
#define pc(x) __builtin_popcountll(x)
#define pb push_back
#define fi first
#define se second
template<class...K> using umap = unordered_map<K...>;
template<class...K> using uset = unordered_set<K...>;

const int N=300100;
ll s[N][4];
ll ra[4],rb[4],rc[4];
ll n,q;
vl a;
ll l,r;
bool pr;

void f(ll* a, ll* b, ll* c) {
  ll c0,c1,c2,c3;
  c0=max(max(a[0],b[0]),max(a[0]+b[3],a[1]+b[0]));
  c1=max(max(a[1],b[1]),max(a[0]+b[2],a[1]+b[1]));
  c2=max(max(a[2],b[2]),max(a[2]+b[1],a[3]+b[2]));
  c3=max(max(a[3],b[3]),max(a[2]+b[0],a[3]+b[3]));
  // if(pr)cout<<c0<<" "<<c1<<" "<<c2<<" "<<c3<<"\n";
  c[0]=c0;c[1]=c1;c[2]=c2;c[3]=c3;
}
void update(int pos,ll val) {
  pos+=n;
  s[pos][0]=val;
  s[pos][1]=-inf;
  s[pos][2]=-val;
  s[pos][3]=-inf;
  for (; pos /= 2;)
    f(s[pos * 2], s[pos * 2 + 1], s[pos]);
}
void query(int b, int e) { // query [b, e)
  rep(i,0,4)ra[i]=rb[i]=-inf;
  for (b += n, e += n; b < e; b /= 2, e /= 2) {
    if (b % 2) f(ra,s[b++],ra);
    if (e % 2) f(s[--e],rb,rb);
  }
  return f(ra,rb,rc);
}

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cout.precision(13);

  ll t;
  cin>>t;
  rep(_t,0,t){
    cin>>n>>q;
    a.assign(n,0);
    rep(i,0,n)cin>>a[i];
    rep(i,0,n)update(i,a[i]);
    pr=1;
    query(0,n); //1 2 5 4 3 6 7
    // cout<<"Q:\n";
    // cout<<rc[0]<<" "<<rc[1]<<" "<<rc[2]<<" "<<rc[3]<<"\n";
    cout<<max(rc[0],rc[1])<<"\n";
    rep(_q,0,q){
      cin>>l>>r;
      swap(a[--l],a[--r]);
      update(l,a[l]);
      update(r,a[r]);
      query(0,n);
      cout<<max(rc[0],rc[1])<<"\n";
    }
  }

  return 0;
}


#include <iostream>
using namespace std;
typedef long ll;
ll n, q;
ll f(ll x) {
        if(x & 1) return (x + 1) >> 1;
        ll step = n - (x >> 1);
        if(step & 1) return (x + step + 1) >> 1;
        else return f(x+step);
}
int main() {
        cin >> n >> q;
        while(q--) {ll x; cin >> x; cout << f(x) << endl;}
        return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <cmath>
#include <queue>
#include <map>
using namespace std;
typedef long long ll;
#define rep(i,j,k) for(ll i = j; i < k; ++i)
#define per(i,j,k) for(ll i = j; i > k; --i)
ll n, q;
ll f(ll x) {
        if(x & 1) return (x + 1) >> 1;
        ll step = n - (x >> 1);
        if(step & 1) return (x + step + 1) >> 1;
        else return f(x+step);
}
int main() {
        cin >> n >> q;
        for(; q--;){ll x; cin >> x; cout << f(x) << endl;}
        return 0;
}


#include <iostream>
using namespace std;
typedef long long ll;
ll n, q;
ll f(ll x) {
        if(x & 1) return (x + 1) >> 1;
        ll step = n - (x >> 1);
        if(step & 1) return (x + step + 1) >> 1;
        else return f(x+step);
}
int main() {
        cin >> n >> q;
        while(q--) {ll x; cin >> x; cout << f(x) << endl;}
        return 0;
}


#pragma comment(linker, "/STACK:1073741824")

#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<deque>
#include<unordered_set>
#include<unordered_map>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<fstream>
#include<iterator>
#include<functional>
#include<bitset>
#include<memory>
#include<sstream>
#include<new>
#include<cassert>
#include<iomanip>

using namespace std;

typedef vector<int> vi;
typedef vector<bool> vb;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<bool, bool> pbb;

const int inf = (int)2 * 1e9;
const ll linf = (ll) 1e18 * 8;
const ld eps = 1e-5;
const int mod = (int) 1e9 + 7;

#define it(v) begin(v), end(v)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fst first
#define scn second
#define sgn(x) (x > 0 ? 1 : (x == 0 ? 0 : -1))

ll n;

ll solve(ll x)
{
	if (x % 2)
	{
		return (x + 1) / 2;
	}
	return solve(x + n - x / 2);
}

int main()
{
	ios_base::sync_with_stdio(false);
	cout.tie(0);
	cin.tie(0);
	srand(time(0));
	cin >> n;
	int q;
	cin >> q;
	for (int i = 0; i < q; i++)
	{
		ll k;
		cin >> k;
		cout << solve(k) << "\n";
	}
}


#pragma comment(linker, "/STACK:1073741824")

#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<deque>
#include<unordered_set>
#include<unordered_map>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<fstream>
#include<iterator>
#include<functional>
#include<bitset>
#include<memory>
#include<sstream>
#include<new>
#include<cassert>
#include<iomanip>

using namespace std;

typedef vector<int> vi;
typedef vector<bool> vb;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<bool, bool> pbb;

const int inf = (int)2 * 1e9;
const ll linf = (ll) 1e18 * 8;
const ld eps = 1e-5;
const int mod = (int) 1e9 + 7;

#define it(v) begin(v), end(v)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fst first
#define scn second
#define sgn(x) (x > 0 ? 1 : (x == 0 ? 0 : -1))

ll n;

ll solve(int x)
{
	if (x % 2)
	{
		return (x + 1) / 2;
	}
	return solve(x + n - x / 2);
}

int main()
{
	ios_base::sync_with_stdio(false);
	cout.tie(0);
	cin.tie(0);
	srand(time(0));
	cin >> n;
	int q;
	cin >> q;
	for (int i = 0; i < q; i++)
	{
		ll k;
		cin >> k;
		cout << solve(k) << "\n";
	}
}


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

int main()
{
	//freopen("inputf.txt","r",stdin);
	ll n,q;
	scanf("%lld %lld",&n,&q);
	while(q--)
	{
		ll x;
		scanf("%lld",&x);
		ll prev;
		while(1)
		{
			if(x&1)
			{
				prev = x/2 + 1;
				break;
			}
			else
			{
				ll xx = x + n - x/2;
				x = xx;
			}
		}
		printf("%lld\n",prev);
	}
	return 0;
}


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

int main()
{
	//freopen("inputf.txt","r",stdin);
	ll n,q;
	scanf("%lld %lld",&n,&q);
	while(q--)
	{
		ll x;
		scanf("%lld",&x);
		ll ans;
		while(1)
		{
			if(x&1)
			{
				ans = x/2 + 1;
				break;
			}
			else
				x = x + n - x/2;
		}
		printf("%lld\n",ans);
	}
	return 0;
}


#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<cstdio>
#include<cmath>
using namespace std;
typedef long long int lli;
lli bs(lli n,lli ind,int flg){
  if(flg==0 and ind%2==1){
    return (ind+1)/2;
  }
  if(flg==1 and ind%2==0){
    return ind/2;
  }
  lli x,y;
  if(flg==0){
    x = ceil(n/2.0);
  }
  else{
    x = floor(n/2.0);
  }
  y = n-x;
  ind = ceil(ind/2.0);
  lli r = n+1;
  if(r%2==0){
    flg^=1;
  }
  return x+bs(y,ind,flg);
}
int main(){
  ios::sync_with_stdio(false);
  lli n,q;
  cin >> n >> q;
  while(q--){
    lli ind;
    cin >> ind;
    cout << bs(n,ind,0) << "\n";
  }
  return 0;
}


#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<cstdio>
#include<cmath>
using namespace std;
typedef long long int lli;
int main(){
  ios::sync_with_stdio(false);
  lli n,q;
  cin >> n >> q;
  while(q--){
    lli ind;
    cin >> ind;
    while(ind%2==0){
        ind += n-ind/2;
    }
    cout << (ind+1)/2 << "\n";
  }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long n,x;
long long dfs(long long n,long long maxid,long long step,long long x,int type,long long start,long long position)
{
   // printf("n:%lld  maxid:%lld   step:%lld   x:%lld    type:%d  start:%lld  position:%lld\n",n,maxid,step,x,type,start,position);
   // if(n==1) return position+1;
    if(type==0)
    {
        if((x-start)%(2*step)==0) return position+(x-start)/(2*step)+1;
        if((maxid-start)%(2*step)==0) type=1,maxid=maxid-step;
        start=start+step;
        position+=(n+1)/2;
        n-=(n+1)/2;
        return dfs(n,maxid,step*2,x,type,start,position);
    }
    start+=step;
    if((x-start)%(2*step)==0) return position+(x-start)/(2*step)+1;
    if((maxid-start)%(2*step)==0) type=1,maxid=maxid-step;
    else type=0;
    position+=n/2;
    n-=n/2;
    start-=step;
    return dfs(n,maxid,step*2,x,type,start,position);

}
int main()
{

    int T;
    scanf("%lld%d",&n,&T);
    while(T--)
    {
        scanf("%lld",&x);
        printf("%lld\n",dfs(n,n,1,x,0,1,0));
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long n,x;
long long dfs(long long n,long long maxid,long long step,long long x,int type,long long start,long long position)
{
   // printf("n:%lld  maxid:%lld   step:%lld   x:%lld    type:%d  start:%lld  position:%lld\n",n,maxid,step,x,type,start,position);
    if(type==0)
    {
        if((x-start)%(2*step)==0) return position+(x-start)/(2*step)+1;
        if((maxid-start)%(2*step)==0) type=1,maxid=maxid-step;
        start=start+step;
        position+=(n+1)/2;
        n-=(n+1)/2;
        return dfs(n,maxid,step*2,x,type,start,position);
    }
    start+=step;
    if((x-start)%(2*step)==0) return position+(x-start)/(2*step)+1;
    if((maxid-start)%(2*step)==0) type=0,maxid=maxid-step;
    position+=n/2;
    n-=n/2;
    start-=step;
    return dfs(n,maxid,step,x,type,start,position);

}
int main()
{

    int T;
    scanf("%lld%d",&n,&T);
    while(T--)
    {
        scanf("%lld",&x);
        printf("%lld\n",dfs(n,n,1,x,0,1,0));
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long n,x;
long long dfs(long long n,long long maxid,long long step,long long x,int type,long long start,long long position)
{
    //printf("n:%lld  maxid:%lld   step:%lld   x:%lld    type:%d  start:%lld  position:%lld\n",n,maxid,step,x,type,start,position);
    if(n==1) return position+1;
    if(type==0)
    {
        if((x-start)%(2*step)==0) return position+(x-start)/(2*step)+1;
        if((maxid-start)%(2*step)==0) type=1,maxid=maxid-step;
        start=start+step;
        position+=(n+1)/2;
        n-=(n+1)/2;
        return dfs(n,maxid,step*2,x,type,start,position);
    }
    start+=step;
    if((x-start)%(2*step)==0) return position+(x-start)/(2*step)+1;
    if((maxid-start)%(2*step)==0) type=0,maxid=maxid-step;
    position+=n/2;
    n-=n/2;
    start-=step;
    return dfs(n,maxid,step*2,x,type,start,position);

}
int main()
{

    int T;
    scanf("%lld%d",&n,&T);
    while(T--)
    {
        scanf("%lld",&x);
        printf("%lld\n",dfs(n,n,1,x,0,1,0));
    }
    return 0;
}


#include <vector>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <memory.h>
#include <cmath>
#include <array>

void re(int& x);
void re(long long& x);

template<class T, class... Ts> void re(T& t, Ts&... ts);

void pr(long long x);

void pr(const char *x);

void ps();
template<class T, class... Ts> void ps(const T& t, const Ts&... ts);

#ifdef FFDBG

#else
#define dbg(x...) dsfdsfsdfasd
#endif

long long n;
int t;

long long f(long long n, long long x) {
    if (x % 2 == 1) return (x+1)/2;
    return f(n - x/2, n - x/2) + x/2;
}

void solve()
{
    for (re(n,t); t; t--) {
        long long x; re(x);
        ps(f(n,x));
    }
}

int main() {

    solve();
}

void re(int& x) { scanf("%d", &x); }
void re(long long& x) { scanf("%lld", &x); }

template<class T, class... Ts> void re(T& t, Ts&... ts) {
    re(t); re(ts...);
}

void pr(long long x) { printf("%lld", x); }

void pr(const char *x) { printf("%s", x); }

void ps() { pr("\n"); }
template<class T, class... Ts> void ps(const T& t, const Ts&... ts) {
    pr(t); if (sizeof...(ts)) pr(" "); ps(ts...);
}


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

#define rep(i,a,b) for(int i=(a),i##end=(b);i<=i##end;++i)
#define per(i,a,b) for(int i=(a),i##end=(b);i>=i##end;--i)
mt19937 Rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
template<typename T>void chkmax(T&x,T y){if(x<y)x=y;}
template<typename T>void chkmin(T&x,T y){if(x>y)x=y;}
inline int read() {
  #define nc getchar()
  int x=0;char c=nc;
  while(c<48)c=nc;
  while(c>47)x=x*10+(c^48),c=nc;
  return x;
  #undef nc
}

typedef double db;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
ll N;int q;

ll find(ll n,ll x){
//  printf("(%lld %lld)\n",n,x);
  if(x&1)return (x+1)/2;
  int p=x/2;
  if(n&1){
    return n/2+1+find(n/2,p==1?n/2:p-1);
  }else{
    return n/2+find(n/2,p);
  }
}

void solve() {
  cin.tie(0),cout.tie(0);
  ios::sync_with_stdio(0);
  cin>>N>>q;
  while(q--){
    ll x;cin>>x;
    cout<<find(N,x)<<endl;
  }
}

signed main() {
//  int T=read();
//  while(T--)solve();
  solve();
  return 0;
}


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

#define rep(i,a,b) for(int i=(a),i##end=(b);i<=i##end;++i)
#define per(i,a,b) for(int i=(a),i##end=(b);i>=i##end;--i)
mt19937 Rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
template<typename T>void chkmax(T&x,T y){if(x<y)x=y;}
template<typename T>void chkmin(T&x,T y){if(x>y)x=y;}
inline int read() {
  #define nc getchar()
  int x=0;char c=nc;
  while(c<48)c=nc;
  while(c>47)x=x*10+(c^48),c=nc;
  return x;
  #undef nc
}

typedef double db;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
ll N;int q;

ll find(ll n,ll x){
//  printf("(%lld %lld)\n",n,x);
  if(x&1)return (x+1)/2;
  ll p=x/2;
  if(n&1){
    return n/2+1+find(n/2,p==1?n/2:p-1);
  }else{
    return n/2+find(n/2,p);
  }
}

void solve() {
  cin.tie(0),cout.tie(0);
  ios::sync_with_stdio(0);
  cin>>N>>q;
  while(q--){
    ll x;cin>>x;
    cout<<find(N,x)<<endl;
  }
}

signed main() {
//  int T=read();
//  while(T--)solve();
  solve();
  return 0;
}


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

#define rep(i,a,b) for(int i=(a),i##end=(b);i<=i##end;++i)
#define per(i,a,b) for(int i=(a),i##end=(b);i>=i##end;--i)
mt19937 Rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
template<typename T>void chkmax(T&x,T y){if(x<y)x=y;}
template<typename T>void chkmin(T&x,T y){if(x>y)x=y;}
inline int read() {
  #define nc getchar()
  int x=0;char c=nc;
  while(c<48)c=nc;
  while(c>47)x=x*10+(c^48),c=nc;
  return x;
  #undef nc
}

typedef double db;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
ll N;int q;

ll find(ll n,ll x){
//  printf("(%lld %lld)\n",n,x);
  if(x&1)return (x+1)/2;
  ll p=x/2;
  if(n&1){
    return n/2+1+find(n/2,p==1?n/2:p-1);
  }else{
    return n/2+find(n/2,p);
  }
}

void solve() {
  cin.tie(0),cout.tie(0);
  ios::sync_with_stdio(0);
  cin>>N>>q;
  while(q--){
    ll x;cin>>x;
    cout<<'#'<<find(N,x)<<endl;
  }
}

signed main() {
//  int T=read();
//  while(T--)solve();
  solve();
  return 0;
}


#include <iostream>

using namespace std;

typedef unsigned long long ll;

int main() {
	ll q, n;
	cin >> n >> q;
	while (q--) {
		ll x;
		cin >> x;
		while (x % 2 == 0) {
			x += (n - x / 2);
		}
		cout << (x + 1) / 2 << endl;
	}
}


#include<iostream>
#include<stack>
using namespace std;
long long a[400005];
int main()
{
    int n,q,i,j,x,sz;
    cin>>n>>q;
    for(i=1;i<=n;i++)
    {
        a[i*2-1]=i;
        a[i*2]=0;
    }
    if(n>1)
    {
        i=n*2-1;
        j=i+1;
        while(j>2)
        {
            j-=2;
            a[j]=a[i];
            i--;
        }
    }
    for(i=0;i<q;i++)
    {
        cin>>x;
        cout<<a[x]<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
long long n,q;
int main()
{
    cin>>n>>q;
    long long x;
    while(q--)
    {
        cin>>x;
        while(x%2==0)
        {
            x+=n-(x/2);
        }
        cout<<x/2+1<<endl;
    }
    return 0;
}


#include<iostream>
#include<stack>
using namespace std;
long long a[400005];
int main()
{
    int n,q,i,j,x,sz;
    cin>>n>>q;
    for(i=1;i<=n;i++)
    {
        a[i*2-1]=i;
        a[i*2]=0;
    }
    if(n>1)
    {
        i=n*2-1;
        j=i-3;
        a[j]=a[i];
        i=j+1;
        while(j>2)
        {
            j-=2;
            a[j]=a[i];
            i--;
        }
    }
    for(i=0;i<q;i++)
    {
        cin>>x;
        cout<<a[x]<<endl;
    }
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<bitset>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<iterator>
#include<climits>
#include<functional>
#include<iomanip>
using namespace std;
#define   MP             make_pair
#define   PB             push_back
#define   endl           '\n'
#define   IOS            ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define   int            long long

typedef long long LL ;

const int MOD=1e9+7,Base=998244353 ;
const int N=6e5+7 ;
const int INF=1LL*1000*1000*1000*1000*1000*1000+7LL ;
const double pie=acos(-1.0) ;
const double EPS=1e-9 ;

int n , q , x ;

int Calco(int pos)
{
   while(!(pos&1))
   {
       pos+=n-(pos>>1) ;
   }

   return (pos+1)>>1 ;
}

int32_t main()
{
IOS

cin>>n>>q ;

while(q--)
{
    cin>>x ;
    cout<<Calco(x)<<endl ;
}

	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<bitset>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<iterator>
#include<climits>
#include<functional>
#include<iomanip>
using namespace std;
#define   MP             make_pair
#define   PB             push_back
#define   endl           '\n'
#define   IOS            ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define   int            long long

typedef long long LL ;

const int MOD=1e9+7,Base=998244353 ;
const int N=6e5+7 ;
const int INF=1LL*1000*1000*1000*1000*1000*1000+7LL ;
const double pie=acos(-1.0) ;
const double EPS=1e-9 ;

int n , q , x ;

int Calco(int pos)
{
   if(pos&1)return (pos+1)>>1 ;

   return Calco(pos+n-(pos>>1)) ;
}

int32_t main()
{
IOS

cin>>n>>q ;

while(q--)
{
    cin>>x ;
    cout<<Calco(x)<<endl ;
}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll ;
ll n;
ll dfs(int i)
{
	if(i%2)return 0*printf("%I64d\n",(i+1)/2);
	dfs(n-i/2+i);
}
main()
{
	ll q,x ;
	scanf("%I64d %I64d",&n,&q);
	while(q--)
	{
		scanf("%I64d",&x);
		dfs(x);
	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll ;
ll n;
ll dfs(ll i)
{
	if(i%2)return 0*printf("%I64d\n",(i+1)/2);
	dfs(n-i/2+i);
}
main()
{
	ll q,x ;
	scanf("%I64d %I64d",&n,&q);
	while(q--)
	{
		scanf("%I64d",&x);
		dfs(x);
	}
}


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

int main(){
	LL n,q,xi;
	scanf("%lld%lld",&n,&q);
	for(int i = 0;i < q;++i){
		scanf("%lld",&xi);
		if(xi & 1){
			printf("%lld\n",(xi + 1)>>1);
			continue;
		}
		LL len = 2 * n - xi;
		while(!(len & 1)) len = len>>1;
		printf("%lld\n",(2 * n - len + 1)>>1);
	}
	return 0;
}


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

int main(){
	LL n,q,xi;
	scanf("%lld%lld",&n,&q);
	for(int i = 0;i < q;++i){
		scanf("%lld",&xi);
		if(xi & 1){
			printf("%lld\n",(xi + 1)>>1);
			continue;
		}
		LL pre = xi,plen = (xi - 1) / 2 + (n - xi);
		LL cur = xi + plen + 1;
		if(plen & 1){
			printf("%lld\n",n);
			continue;
		}
		while(!(cur & 1)){
			plen = (cur - pre)>>1;
			pre = cur;
			cur = pre + plen + 1;
		}
		//printf("cur == %lld\n",cur);
		printf("%lld\n",n - ((n * 2 - cur)>>1));
	}
	return 0;
}


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

int main(){
	LL n,q,xi;
	scanf("%lld%lld",&n,&q);
	for(int i = 0;i < q;++i){
		scanf("%lld",&xi);
		if(xi & 1){
			printf("%lld\n",(xi + 1)>>1);
			continue;
		}
		LL pre = xi,plen = (xi - 1) / 2 + (n - xi);
		LL cur = xi + plen + 1;
//		printf("plen == %lld\n",plen);
//		if(plen & 1){
//			printf("%lld\n",n);
//			continue;
//		}
		while(!(cur & 1)){
			plen = (cur - pre - 1)>>1;
			pre = cur;
			cur = pre + plen + 1;
			//printf("cur == %lld\n",cur);
		}
		//printf("cur == %lld\n",cur);
		printf("%lld\n",n - ((n * 2 - cur)>>1));
	}
	return 0;
}


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

typedef long long ll;
ll n,q,x;

void work(){

	for(;d0.size();){
		if((d1.size())&&(*d0.begin())>(*d1.begin()))
			{puts("-1");return 0;}

		it=d0.begin();d0.erase(it);
		++cnt;ans[cnt]=1;
		nxt1=d1.upper_bound(*it);
		while(nxt1!=d1.end()){
			nxt0=d0.upper_bound(*nxt1);
			if(nxt0!=d0.end()){
				d1.erase(nxt1);
				d0.erase(nxt0);
				ans[cnt]+=2;
				nxt1=d1.upper_bound(*nxt0);
			}
			else break;
		}
	}
}

int main(){
	cin>>n>>q;
	for(ll x;q--;){
		cin>>x;
		ll ans=0,t=n;
		while(1){
			if(x&1){ans+=((x+1)>>1);break;}
			if(t&1){ans+=t>>1;x>>=1;x++;t=(t+1)>>1;}
			else{ans+=t>>1;x>>=1;t>>=1;}
		}
		cout<<ans<<endl;
	}
	return 0;
}


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

typedef long long ll;
ll n,q,x;

set<int> d0,d1;
set<int> ::iterator nxt0,nxt1;

void work(){

	for(;d0.size();){
		if((d1.size())&&(*d0.begin())>(*d1.begin()))
			puts("-1");

		while(nxt1!=d1.end()){
			nxt0=d0.upper_bound(*nxt1);
			if(nxt0!=d0.end()){
				d1.erase(nxt1);
				d0.erase(nxt0);

				nxt1=d1.upper_bound(*nxt0);
			}
			else break;
		}
	}
}

int main(){
	cin>>n>>q;
	for(ll x;q--;){
		cin>>x;
		ll ans=0,t=n;
		while(1){
			if(x&1){ans+=((x+1)>>1);break;}
			if(t&1){ans+=t>>1;x>>=1;x++;t=(t+1)>>1;}
			else{ans+=t>>1;x>>=1;t>>=1;}
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
typedef long long ll;
ll f(ll n,ll x){
	if(n==1) return 1;
	if(x&1) return x/2+1;
	if(n%2==0) return n/2+f(n/2,x/2);
	else{
		ll y=x/2-1;
		if(y==0) y=x/2;
		return (n+1)/2+f(n/2,y);
	}
}
int main(int argc, char const *argv[])
{
	ll n;
	int q;
	scanf("%I64d%d", &n,&q);
	while(q--){
		ll x;
		scanf("%I64d", &x);
		// printf("%lld\n", f(n,x));
		cout << f(n,x) << endl;
	}

	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
typedef long long ll;
ll f(ll n,ll x){
	if(n==1) return 1;
	if(x&1) return (x+1)/2;
	if(n%2==0) return n/2+f(n/2,x/2);
	else{
		ll y=x/2-1;
		if(y==0) y=n/2;
		return (n+1)/2+f(n/2,y);
	}
}
int main(int argc, char const *argv[])
{
	ll n;
	int q;
	cin>>n>>q;
	while(q--){
		ll x;
		cin>>x;
		cout << f(n,x) << endl;
	}
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
typedef long long ll;
ll f(ll n,ll x){
	if(n==1) return 1;
	if(x&1) return x/2+1;
	if(n%2==0) return n/2+f(n/2,x/2);
	else{
		ll y=x/2-1;
		if(y==0) y=x/2;
		return (n+1)/2+f(n/2,y);
	}
}
int main(int argc, char const *argv[])
{
	ll n;
	int q;
	scanf("%lld%d", &n,&q);
	while(q--){
		ll x;
		scanf("%lld", &x);
		printf("%I64dd\n", f(n,x));
	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main(){
long long n,q;
cin>>n>>q;
long long ans[q];
for(int i=0;i<q;i++){
long long x;
cin>>x;
if(x%2==1){
ans[i]=(x+1)/2;
}
else{long long p=-1;
long long j=n-x/2;
for(int i=0;i<=62;i++){
long long v=(long long)(pow(2,i));
if(j%v==0){
p=(j/v+1)/2;
}
}

ans[i]=n-p+1;

}
}

for(int i=0;i<q;i++){cout<<ans[i]<<endl;}

return 0;

}


#include<bits/stdc++.h>

using namespace std;

int main(){

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

long long n,q;
cin>>n>>q;
long long ans[q];
for(int i=0;i<q;i++){
long long x;
cin>>x;
if(x%2==1){
ans[i]=(x+1)/2;
}
else{long long p=-1;
long long j=n-x/2;
for(int i=0;i<=62;i++){
long long v=(long long)(pow(2,i));
if(j%v==0){
p=(j/v+1)/2;
}
}

ans[i]=n-p+1;

}
}

for(int i=0;i<q;i++){cout<<ans[i]<<endl;}

return 0;

}


#include <bits/stdc++.h>
#define f0(i, n) for(int (i)=0; (i)<(n); ++(i))
#define f1(i, n) for(int (i)=1; (i)<=(n); ++(i))
#define stdf(it, a) for(auto (it): (a))
#define pb push_back
#define F first
#define S second

using namespace std;

typedef pair < int, int > ii;
typedef long long ll;
typedef long double ld;

const int MAXN = 0;
const int LOGN = 0;
const int MOD = 1e9 + 7;
const int INF = 1e9 + 10;

ll N, Q;
ll x;

ll f(ll x){
	if(x&1) return (x+1)>>1;
	ll id = N - (x>>1);
	id -= id & -id;
	id>>=1;
	return N - id;
}

void read(){
	cin >> N >> Q;
	while(Q--){
		cin >> x;
		cout << f(x) << "\n";
	}
	return;
}

int main(){
	ios_base::sync_with_stdio(false);
	read();
	return 0;
}


#include <bits/stdc++.h>
#define f0(i, n) for(int (i)=0; (i)<(n); ++(i))
#define f1(i, n) for(int (i)=1; (i)<=(n); ++(i))
#define stdf(it, a) for(auto (it): (a))
#define pb push_back
#define F first
#define S second

using namespace std;

typedef pair < int, int > ii;
typedef long long ll;
typedef long double ld;

const int MAXN = 0;
const int LOGN = 0;
const int MOD = 1e9 + 7;
const int INF = 1e9 + 10;

ll N, Q;
ll x;

ll f(ll x){
	if(x&1) return (x+1)>>1;
	ll id = N - (x>>1);
	while(!(id&1)) id /= 2;
	--id; id /= 2;
	return N - id;
}

void read(){
	cin >> N >> Q;
	while(Q--){
		cin >> x;
		cout << f(x) << "\n";
	}
	return;
}

int main(){
	read();

}


#include <bits/stdc++.h>
#define f0(i, n) for(int (i)=0; (i)<(n); ++(i))
#define f1(i, n) for(int (i)=1; (i)<=(n); ++(i))
#define stdf(it, a) for(auto (it): (a))
#define pb push_back
#define F first
#define S second

using namespace std;

typedef pair < int, int > ii;
typedef long long ll;
typedef long double ld;

const int MAXN = 0;
const int LOGN = 0;
const int MOD = 1e9 + 7;
const int INF = 1e9 + 10;

ll N, Q;
ll x;

ll f(ll x){
	if(x&1) return (x+1)>>1;
	ll id = N - (x>>1);
	while(!(id&1)) id>>=1; id>>=1;
	return N - id;
}

void read(){
	cin >> N >> Q;
	while(Q--){
		cin >> x;
		cout << f(x) << "\n";
	}
	return;
}

int main(){
	ios_base::sync_with_stdio(false);
	read();
	return 0;
}


#include <bits/stdc++.h>
#define f0(i, n) for(int (i)=0; (i)<(n); ++(i))
#define f1(i, n) for(int (i)=1; (i)<=(n); ++(i))
#define stdf(it, a) for(auto (it): (a))
#define pb push_back
#define F first
#define S second

using namespace std;

typedef pair < int, int > ii;
typedef long long ll;
typedef long double ld;

const int MAXN = 0;
const int LOGN = 0;
const int MOD = 1e9 + 7;
const int INF = 1e9 + 10;

ll N, Q;
ll x;

ll f(ll x){
	if(x&1) return (x+1)>>1;
	ll id = N - (x>>1);
	id /= id & -id;
	id>>=1;
	return N - id;
}

void read(){
	cin >> N >> Q;
	while(Q--){
		cin >> x;
		cout << f(x) << "\n";
	}
	return;
}

int main(){
	ios_base::sync_with_stdio(false);
	read();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long n,q;
long long f(long long x){
    if(x%2!=0){
        return (x+1)/2;
    }
    else{
        return f(x+n-x/2);
    }
}
int main(){
    scanf("%lld %d",&n,&q);
    while(q--){
        long long x;
        scanf("%d",&x);
        printf("%lld\n",f(x));
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long n,q;
long long f(long long x){
    if(x%2!=0){
        return (x+1)/2;
    }
    else{
        return f(x+n-x/2);
    }
}
int main(){
    scanf("%lld %d",&n,&q);
    while(q--){
        long long x;
        scanf("%lld",&x);
        printf("%lld\n",f(x));
    }
    return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

int main()
{
	//freopen("input.txt", "r", stdin);
	long long n;
	int q;
	cin >> n >> q;

	for (int i = 0; i < q; i++) {
		long long x;
		cin >> x;

		while (x%2 == 0) {
			x += (n-x/2);
		}
		cout << (x+1)/2 << endl;
	}
}


#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

int main()
{
	//freopen("input.txt", "r", stdin);
	long long n;
	int q;
	cin >> n >> q;

	for (int i = 0; i < q; i++) {
		int x;
		cin >> x;

		while (x%2 == 0) {
			x += (n-x/2);
		}
		cout << (x+1)/2 << endl;
	}
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <iostream>
#include <cmath>
using namespace std;
#define lson l,(l+r)/2,rt<<1
#define rson (l+r)/2+1,r,rt<<1|1
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define pb push_back
#define fi first
#define se second
#define ll long long
#define sz(x) (int)(x).size()
#define pll pair<long long,long long>
#define pii pair<int,int>
ll n;
ll solve(ll r,ll c){
    if(c%2)return c/2+1;
    ll h=c/2;
    return solve(n-h,c+(n-h));
}
int main(){
    int q;
    cin>>n>>q;
    while(q--){
        ll x;
        scanf("%lld",&x);
        printf("%lld\n",solve(n,x));
    }
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <iostream>
#include <cmath>
using namespace std;
#define lson l,(l+r)/2,rt<<1
#define rson (l+r)/2+1,r,rt<<1|1
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define pb push_back
#define fi first
#define se second
#define ll long long
#define sz(x) (int)(x).size()
#define pll pair<long long,long long>
#define pii pair<int,int>
int a[1000000];
int main(){
	int n=9;
	while(cin>>n){

		memset(a,0,sizeof(a));
		for(int i=1;i<=n*2;i++){
			if(i%2)a[i]=i/2+1;
		}
			for(int i=1;i<=n*2;i++){
				if(a[i])printf("%2d ",a[i]);
				else printf("   ");
			}
			printf("\n");
		while(true){
			int pos=0;
			for(int i=2*n;i>=1;i--){
				if(a[i]){
					pos=i;
					break;
				}
			}
			if(pos==n)break;
			for(int j=pos;j>=1;j--){
				if(!a[j]){
					swap(a[j],a[pos]);
					break;
				}
			}
			for(int i=1;i<=n*2;i++){
				if(a[i])printf("%2d ",a[i]);
				else printf("   ");
			}
			printf("\n");
		}}
	return 0;
}


#include<iostream>
#include<vector>
#define ll long long
#include <queue>
#include <string.h>
#include <bitset>
#include <algorithm>
#include <fstream>
#include <map>
#define MAX 400050
#include <cmath>
#define pp pair < int , int >
#include <stack>

using namespace std;

ifstream f("date.in");

int n, v[MAX], m;

int main()
{
    cin>>n>>m;
    for(int i=1; i<=2*n; i++)
        if((i&1))
            v[i]=(i+1)/2;
    for(int i=2*n-2; i>=2; i-=2)
    {
        int posjump = i+(n-i/2);
        v[i]=v[posjump];
    }
    for(int i=1; i<=m; i++)
    {
        int idx;
        cin>>idx;
        cout<<v[idx]<<'\n';
    }
}


#include<iostream>
#include<vector>
#define ll long long unsigned int
#include <queue>
#include <string.h>
#include <bitset>
#include <algorithm>
#include <fstream>
#include <map>
#define MAX 400050
#include <cmath>
#define pp pair < int , int >
#include <stack>

using namespace std;

ifstream f("date.in");

ll n, m;

ll get_pos(ll idx)
{
    if(idx&1)
        return ((idx+1)>>1);
    while(!(idx&1))
        idx+=n-(idx>>1);
    return ((idx+1)>>1);
}

int main()
{
    cin>>n>>m;
    while(m)
    {
        ll pos;
        cin>>pos;
        cout<<get_pos(pos)<<'\n';
        m--;
    }
}


#include<iostream>
#include<vector>
#define ll long long unsigned int
#include <queue>
#include <string.h>
#include <bitset>
#include <algorithm>
#include <fstream>
#include <map>
#define MAX 400050
#include <cmath>
#define pp pair < int , int >
#include <stack>

using namespace std;

ifstream f("date.in");

ll n, v[MAX], m;

int main()
{
    cin>>n>>m;
    for(ll i=1; i<=2*n; i++)
        if((i&1))
            v[i]=(i+1)/2;
    for(ll i=2*n-2; i>=2; i-=2)
    {
        ll posjump = i+(n-i/2);
        v[i]=v[posjump];
    }
    for(ll i=1; i<=m; i++)
    {
        ll idx;
        cin>>idx;
        cout<<v[idx]<<'\n';
    }
}


#define _CRT_SECURE_NO_WARNINGS
#pragma comment(linker, "/STACK:256000000")
#define _USE_MATH_DEFINES
#include<iostream>
#include<vector>
#include<string>
#include<stack>
#include<algorithm>
#include<cmath>
#include<set>
#include<queue>
#include<sstream>
#include<utility>
#include<map>
#include<ctime>
#include<cstdio>
#include<cassert>
#include<functional>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef pair<char, char> pcc;
typedef pair<double, double> pdd;

#define show(x) cerr << x
#define debug(x) show(#x << ": " << (x) << endl)

const long double PI = 3.14159265358979323846;
const long double eps = 1e-5;
const int INF = numeric_limits<int>::max();
const ll LINF = numeric_limits<ll>::max();
const ll mod = 1000 * 1000 * 1000 + 7;

void solve() {
    ll n, q;
    cin >> n >> q;
    for (int i = 0; i < q; ++i) {
        ll x;
        cin >> x;
        while ((x & 1) == 0) {
            ll fr = x / 2;
            ll m = n - fr;
            x += m;
        }
        cout << (x + 1) / 2 << "\n";
    }

}

//#define LOCAL

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

#ifdef LOCAL
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#pragma comment(linker, "/STACK:256000000")
#define _USE_MATH_DEFINES
#include<iostream>
#include<vector>
#include<string>
#include<stack>
#include<algorithm>
#include<cmath>
#include<set>
#include<queue>
#include<sstream>
#include<utility>
#include<map>
#include<ctime>
#include<cstdio>
#include<cassert>
#include<functional>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef pair<char, char> pcc;
typedef pair<double, double> pdd;

#define show(x) cerr << x
#define debug(x) show(#x << ": " << (x) << endl)

const long double PI = 3.14159265358979323846;
const long double eps = 1e-5;
const int INF = numeric_limits<int>::max();
const ll LINF = numeric_limits<ll>::max();
const ll mod = 1000 * 1000 * 1000 + 7;

void solve() {
    ll n, q;
    cin >> n >> q;
    for (int i = 0; i < q; ++i) {
        int x;
        cin >> x;
        while ((x & 1) == 0) {
            ll fr = x / 2;
            ll m = n - fr;
            x += m;
        }
        cout << (x + 1) / 2 << "\n";
    }

}

//#define LOCAL

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

#ifdef LOCAL
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
    return 0;
}


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

long long func(long long m, long long n, bool bad) {
	if (bad == 0) {
		if (m % 2 == 1) return (m + 1) / 2;
		long long d = (n + 1) / 2;
		return d + func(m / 2, n - d, (2 * d - 1) == n);
	}
	else {
		if (m % 2 == 0) return m / 2;
		long long d = n / 2;
		return d + func((m + 1) / 2, n - d, (2 * d) == n);
	}
}

int main() {
	long long n, m;
	int q;
	scanf("%I64d %d", &n, &q);
	while (q --) {
		scanf("%I64d", &m);
		printf("%I64d\n", func(m, n, 0));
	}

	return 0;
}


#include <iostream>

using namespace std;
int ultimul(int sf,int a[101])
{
	for(int i=sf;i>=1;i--)
	 if(a[i]!=0)
	 return i;
}
bool goluri(int x,int a[101])
{
    for(int i=1;i<=x;i++)
        if(a[i]==0)
        return 1;
    return 0;
}
int primul(int i,int a[101])
{

    i--;
    while(i>=1)
    {

        if(a[i]!=0&&a[i-1]==0)
            return i-1;
        else i--;
    }
}
int main()
{
    int n,q,x=1,a[101]={0};
    cin>>n>>q;
    while(x<=n)
    {
        a[2*x-1]=x;
        x++;
        //cout<<x<<endl;
         //for(int i=1;i<=2*x-1;i++)
        //cout<<a[i]<<" ";
    //cout<<endl;
    }

    int sf=2*(x-1)-1;
    //cout<<"sfarsit "<<sf<<endl;
    //for(int i=1;i<=sf;i++)
       // cout<<a[i]<<" ";
    //cout<<endl;
    int i=sf;
    //cout<<primul(7,a)<<endl;
    //cout<<"goluri "<<goluri(sf,a)<<endl;
    while(goluri(i,a)!=0&&i>=1)
    {

        if(a[i]!=0)
        {
            int pozitie=primul(i,a);
            a[pozitie]=a[i];
            a[i]=0;
            i=ultimul(sf,a);
            // for(int i=1;i<=sf;i++)
        //cout<<a[i]<<" ";
    //cout<<endl;
        }
        else i--;
    }
   // for(int i=1;i<=n;i++)
     //   cout<<a[i]<<" ";
    //cout<<endl;
    for(int i=1;i<=q;i++)
    {   cin>>x;
        cout<<a[x]<<endl;}
    return 0;
}


#include <iostream>
using namespace std;

int main() {
	int n,q,x;
	cin>>n>>q;
	for(int i=1;i<=q;i++)
	{
		cin>>x;
		if(x&1)
		cout<<(x+1)/2<<endl;
		else
		{
			while(x%2==0)
			  x=x+n-x/2;
		            cout<<(x+1>>1)<<endl;
		}
	}
	return 0;
}


#include <iostream>
using namespace std;

int main() {
	int q;
	unsigned long long n,x;
	cin>>n>>q;
	while(q--){
            cin>>x;
                while(!(x&1)){
                  x+=n-x/2;
                        }
              cout<<(x+1>>1)<<endl;

    }

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
	long long N, Q; cin >> N >> Q;
	while (Q--){
		long long POS; cin >> POS;
		while (POS % 2 == 0) POS = N + (POS >> 1);
		cout << (POS + 1) / 2 << '\n';
	}
}


#include <bits/stdc++.h>
using namespace std;
int main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
	long long N, Q; cin >> N >> Q;
	while (Q--){
		long long POS; cin >> POS;
		while (POS % 2) POS = N + (POS >> 1);
		cout << (POS + 1) / 2 << '\n';
	}
}