#include <bits/stdc++.h>
#define v1 2*nodo
#define v2 2*nodo + 1
#define me (l+r)/2
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define sz(A) int(A.size())
#define FIFO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define all(A) begin(A),end(A)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<pair<ll,ll>> vll;
typedef vector<pair<int,int>> vii;
const int N = 100005;
struct segment {
	ll val, lazy;
	segment(){}
	segment(const ll _val){
		val = _val;
	}
};
vl a, b;
segment tree[4*N];
int n, m;
void build(int nodo = 1, int l = 0, int r = n-1) {
	if(l == r) {
		tree[nodo].val = a[l];
		return;
	}
	build(v1, l, me);
	build(v2, me + 1, r);
	tree[nodo].val = tree[v1].val + tree[v2].val;
}
void push(int nodo, int l, int r) {
	if(tree[nodo].lazy == 0)return;
	int len = r - l + 1;
	if(len % 2 == 1) {
		if(l%2 == 0)tree[nodo].val += tree[nodo].lazy;
		else tree[nodo].val -= tree[nodo].lazy;
	}
	if(l!=r) {
		tree[v1].lazy += tree[nodo].lazy;
		tree[v2].lazy += tree[nodo].lazy;
	}
	tree[nodo].lazy = 0;
}

void update(int lu, int ru, ll add, int nodo = 1, int l = 0, int r = n-1) {
	push(nodo, l, r);
	if(lu > r || ru < l) return;
	if(lu <= l && ru >= r) {
		tree[nodo].lazy += add;
		push(nodo, l, r);
		return;
	}
	update(lu, ru, add, v1, l, me);
	update(lu, ru, add, v2, me + 1, r);
	tree[nodo].val = tree[v1].val + tree[v2].val;
}

ll query(int lq, int rq, int nodo = 1, int l = 0, int r = n-1) {
	push(nodo, l, r);
	if(lq > r || rq < l) return 0;
	if(lq <= l && rq >= r) {
		return tree[nodo].val;
	}
	return query(lq, rq, v1, l, me) + query(lq, rq, v2, me + 1, r);
}

vl acumB;
int main() {
	FIFO;

	cin >> n >> m;
	int q;
	cin >> q;
	ll x;
	for(int i = 0; i < n; i++) {
		cin >> x;
		if(i%2==0)a.pb(x);
		else a.pb(-x);
	}
	for(int i = 0; i < m; i++) {
		cin >> x;
		b.pb(x);
	}

	x = 0;
	for(int i = 0; i < n; i++) {
		if(i%2 == 0) x += b[i];
		else x -= b[i];
	}
	acumB.pb(x);

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

		if(i%2 == 0) {
			x += b[i];
		}
		else x -= b[i];

		if((i - n + 1) % 2 == 1) {
			x -= b[i-n];
			acumB.pb(-x);
		}
		else {
			x += b[i-n];
			acumB.pb(x);
		}

	}
	sort(all(acumB));
	build();
	int uu = q + 1;
	while(uu--) {
		int l, r;
		ll ana;
		if(uu != q){
			cin >> l >> r >> ana;
			l--, r--;
			update(l,r,ana);
		}
		ll maxi = LLONG_MAX;
		ll resactual = query(0, n-1);
	//	cout << resactual << "\n";
		int indi = lower_bound(acumB.begin(), acumB.end(), resactual) - acumB.begin();
		ll rescompa = acumB[indi];
		if(indi!=m - n + 1)maxi = min(maxi, rescompa - resactual);
		if(indi != 0) {
			rescompa = acumB[indi-1];
			maxi = min(maxi, resactual - rescompa);
		}
		cout << maxi << "\n";
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <stack>
#include <deque>
#include <iterator>
#include <tuple>
#include <iomanip>
#define pb push_back
#define mp make_pair
#define int long long
#define fi first
#define se second
#define endl '\n'
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define MAXN   5000001
using namespace std;
int power(int x,int y, int p)
{
    int res = 1;
    x = x % p;
    if (x == 0) return 0;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
signed main()
{
    fast
    int n,m,q;
    cin>>n>>m>>q;
    int sum=0,tot=0;
    for(int i=0;i<n;i++)
    {
        int k;
        cin>>k;
        if(i%2)
            tot-=k;
        else tot+=k;
    }
    int b[m];
    for(int i=0;i<m;i++)
    {
        cin>>b[i];
    }
    vector<int> v;
    int l=0,r,x;
    sum=0;
    for(r=0;r<n;r++)
    {
        if(r%2)
            sum-=b[r];
        else sum+=b[r];
    }
    v.pb(sum);
    while(r<m)
    {
        sum-=b[l];
        sum=sum*-1;
        if((n-1)%2==0)
        {
            sum+=b[r];
        }
        else sum-=b[r];
        v.pb(sum);
        l++,r++;
    }
    sort(v.begin(),v.end());
    for(int i=0;i<=q;i++)
    {
        sum=0;
        if(i>0)
        {cin>>l>>r>>x;
        if(l%2&&r%2)
            tot+=x;
        else if(l%2==1&&r%2==1)
            tot-=x;
        auto it=lower_bound(v.begin(),v.end(),tot);
        sum=abs(tot-*it);
        auto it2=it;
        if(it!=v.begin())
        {
            it2--;
            int gg=abs(tot-*it2);
            sum=min(sum,gg);
        }
        cout<<sum<<endl;
        }
        else{
            auto it=lower_bound(v.begin(),v.end(),tot);
            sum=abs(tot-*it);
            auto it2=it;
            if(it!=v.begin())
            {
                it2--;
                int gg=abs(tot-*it2);
                sum=min(sum,gg);
            }
            cout<<sum<<endl;
        }

    }

}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <stack>
#include <deque>
#include <iterator>
#include <tuple>
#include <iomanip>
#define pb push_back
#define mp make_pair
#define int long long
#define fi first
#define se second
#define endl '\n'
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define MAXN   5000001
using namespace std;
int power(int x,int y, int p)
{
    int res = 1;
    x = x % p;
    if (x == 0) return 0;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
signed main()
{
    fast
    int n,m,q;
    cin>>n>>m>>q;
    int sum=0,tot=0;
    for(int i=0;i<n;i++)
    {
        int k;
        cin>>k;
        if(i%2)
            tot-=k;
        else tot+=k;
    }
    int b[m];
    for(int i=0;i<m;i++)
    {
        cin>>b[i];
    }
    vector<int> v;
    int l=0,r,x;
    sum=0;
    for(r=0;r<n;r++)
    {
        if(r%2)
            sum-=b[r];
        else sum+=b[r];
    }
    v.pb(sum);
    while(r<m)
    {
        sum-=b[l];
        sum=sum*-1;
        if((n-1)%2==0)
        {
            sum+=b[r];
        }
        else sum-=b[r];
        v.pb(sum);
        l++,r++;
    }
    sort(v.begin(),v.end());
    for(int i=0;i<=q;i++)
    {
        sum=1e18;
        if(i>0)
        {cin>>l>>r>>x;
            //l--,r--;
            if(l%2==1&&r%2==1)
                tot+=x;
            else if(l%2==0&&r%2==0)
                tot-=x;
            auto it=lower_bound(v.begin(),v.end(),tot);
            if(it!=v.end())
                sum=abs(tot-*it);
            auto it2=it;
            if(it!=v.begin())
            {
                it2--;
                int gg=abs(tot-*it2);
                sum=min(sum,gg);
            }
            cout<<sum<<endl;
        }
        else{
            auto it=lower_bound(v.begin(),v.end(),tot);
            if(it!=v.end())
                sum=abs(tot-*it);
            auto it2=it;
            if(it!=v.begin())
            {
                it2--;
                int gg=abs(tot-*it2);
                sum=min(sum,gg);
            }
            cout<<sum<<endl;
        }

    }

}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <stack>
#include <deque>
#include <iterator>
#include <tuple>
#include <iomanip>
#define pb push_back
#define mp make_pair
#define int long long
#define fi first
#define se second
#define endl '\n'
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define MAXN   5000001
using namespace std;
int power(int x,int y, int p)
{
    int res = 1;
    x = x % p;
    if (x == 0) return 0;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
signed main()
{
    fast
    int n,m,q;
    cin>>n>>m>>q;
    int sum=0,tot=0;
    for(int i=0;i<n;i++)
    {
        int k;
        cin>>k;
        if(i%2)
            tot-=k;
        else tot+=k;
    }
    int b[m];
    for(int i=0;i<m;i++)
    {
        cin>>b[i];
    }
    vector<int> v;
    int l=0,r,x;
    sum=0;
    for(r=0;r<n;r++)
    {
        if(r%2)
            sum-=b[r];
        else sum+=b[r];
    }
    v.pb(sum);
    while(r<m)
    {
        sum-=b[l];
        sum=sum*-1;
        if((n-1)%2==0)
        {
            sum+=b[r];
        }
        else sum-=b[r];
        v.pb(sum);
        l++,r++;
    }
    sort(v.begin(),v.end());
    for(int i=0;i<=q;i++)
    {
        sum=1e18;
        if(i>0)
        {cin>>l>>r>>x;
        if(l%2&&r%2)
            tot+=x;
        else if(l%2==1&&r%2==1)
            tot-=x;
        auto it=lower_bound(v.begin(),v.end(),tot);
        if(it!=v.end())
        sum=abs(tot-*it);
        auto it2=it;
        if(it!=v.begin())
        {
            it2--;
            int gg=abs(tot-*it2);
            sum=min(sum,gg);
        }
        cout<<sum<<endl;
        }
        else{
            auto it=lower_bound(v.begin(),v.end(),tot);
            if(it!=v.end())
                sum=abs(tot-*it);
            auto it2=it;
            if(it!=v.begin())
            {
                it2--;
                int gg=abs(tot-*it2);
                sum=min(sum,gg);
            }
            cout<<sum<<endl;
        }

    }

}


#include <algorithm>
#include <iostream>
#include <iomanip>
#include <utility>
#include <cstdlib>
#include <cstdint>
#include <cstdio>
#include <vector>
#include <string>
#include <deque>
#include <cmath>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <sstream>
#include <cstring>
#include <assert.h>
#include <chrono>
#include <random>
#include <functional>
#include <bitset>
#include <iterator>
#include <unordered_map>
#include <unordered_set>

#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

const int MAXN = 1.1e5;

struct Segtree {
  long long T[4 * MAXN];
  long long lazy[4 * MAXN];
  void push(int n) {
    if (lazy[n]) {
      lazy[2 * n] += lazy[n];
      lazy[2 * n + 1] += lazy[n];
      lazy[n] = 0;
    }
  }
  long long Get(int n, int L, int R) {
    return T[n] + lazy[n] * (long long)(R - L + 1);
  }
  void pull(int n, int L, int R) {
    int mid = (L + R) / 2;
    T[n] = Get(2 * n, L, mid) + Get(2 * n + 1, mid + 1, R);
  }
  long long query(int n, int lft, int rgt, int rs, int re) {
    if (lft > re || rgt < rs)
      return 0;
    if (lft >= rs && rgt <= re)
      return T[n];
    int mid = (lft + rgt) / 2;
    push(n);
    auto L = query(2 * n, lft, mid, rs, re);
    auto R = query(2 * n + 1, mid + 1, rgt, rs, re);
    pull(n, lft, rgt);
    return L + R;
  }
  void upd(int n, int lft, int rgt, int rs, int re, int v) {
    if (lft > re || rgt < rs)
      return;
    if (lft >= rs && rgt <= re) {
      lazy[n] += v;
      return;
    }
    push(n);
    int mid = (lft + rgt) / 2;
    upd(2 * n, lft, mid, rs, re, v);
    upd(2 * n + 1, mid + 1, rgt, rs, re, v);
    pull(n, lft, rgt);
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m, q;
  cin >> n >> m >> q;
  vector<long long> a(n);
  vector<long long> b(m);
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < m; i++) {
    cin >> b[i];
    if (i % 2 == 0) b[i] = -b[i];
  }
  static Segtree T_all;
  static Segtree T_even;
  for (int i = 0; i < n; i++) {
    T_all.upd(1, 0, n - 1, i, i, a[i]);
    if ((i + 1) % 2 == 0) {
      T_even.upd(1, 0, n + 10, (i + 1) / 2, (i + 1) / 2, a[i]);
    }
  }
  vector<long long> F(b);
  for (int i = 1; i < m; i++)
    F[i] += F[i - 1];
  vector<long long> Sums;
  for (int i = 0; i + n - 1 < m; i++) {
    long long pref = i == 0 ? 0 : F[i - 1];
    long long sm = F[i + n - 1] - pref;
    if (i % 2 == 1)
      sm = -sm;
    Sums.push_back(sm);
  }
  sort(Sums.begin(), Sums.end());
  auto Try = [&](long long x) {
    auto it = lower_bound(Sums.begin(), Sums.end(), -x);
    if (it == Sums.begin())
      return llabs(x + *it);
    if (it == Sums.end())
      return llabs(x + *--it);
    long long a = llabs(x + *it);
    long long b = llabs(x + *--it);
    return min(a, b);
  };
  long long GU = T_all.Get(1, 0, n - 1) - 2 * T_even.Get(1, 0, n + 10);
  cout << Try(GU) << '\n';
  for (int i = 0; i < q; i++) {
    int lft, rgt, x;
    cin >> lft >> rgt >> x;
    lft--, rgt--;
    T_all.upd(1, 0, n - 1, lft, rgt, x);
    if (lft < rgt) {
      lft++, rgt++;
      T_even.upd(1, 0, n + 10, (lft + 1) / 2, rgt / 2, x);
    }
    long long ans = T_all.Get(1, 0, n - 1);
    ans -= 2 * T_even.Get(1, 0, n + 10);
    cout << Try(ans) << '\n';
  }
  return 0;
}


#include <algorithm>
#include <iostream>
#include <iomanip>
#include <utility>
#include <cstdlib>
#include <cstdint>
#include <cstdio>
#include <vector>
#include <string>
#include <deque>
#include <cmath>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <sstream>
#include <cstring>
#include <assert.h>
#include <chrono>
#include <random>
#include <functional>
#include <bitset>
#include <iterator>
#include <unordered_map>
#include <unordered_set>

#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

const int MAXN = 1.1e5;

struct Segtree {
  long long T[4 * MAXN];
  long long lazy[4 * MAXN];
  void push(int n) {
    if (lazy[n]) {
      lazy[2 * n] += lazy[n];
      lazy[2 * n + 1] += lazy[n];
      lazy[n] = 0;
    }
  }
  long long Get(int n, int L, int R) {
    return T[n] + lazy[n] * (long long)(R - L + 1);
  }
  void pull(int n, int L, int R) {
    int mid = (L + R) / 2;
    T[n] = Get(2 * n, L, mid) + Get(2 * n + 1, mid + 1, R);
  }
  long long query(int n, int lft, int rgt, int rs, int re) {
    if (lft > re || rgt < rs)
      return 0;
    if (lft >= rs && rgt <= re)
      return T[n];
    int mid = (lft + rgt) / 2;
    push(n);
    long long L = query(2 * n, lft, mid, rs, re);
    long long R = query(2 * n + 1, mid + 1, rgt, rs, re);
    pull(n, lft, rgt);
    return L + R;
  }
  void upd(int n, int lft, int rgt, int rs, int re, int v) {
    if (lft > re || rgt < rs)
      return;
    if (lft >= rs && rgt <= re) {
      lazy[n] += v;
      return;
    }
    push(n);
    int mid = (lft + rgt) / 2;
    upd(2 * n, lft, mid, rs, re, v);
    upd(2 * n + 1, mid + 1, rgt, rs, re, v);
    pull(n, lft, rgt);
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m, q;
  cin >> n >> m >> q;
  vector<long long> a(n);
  vector<long long> b(m);
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < m; i++) {
    cin >> b[i];
    if (i % 2 == 0) b[i] = -b[i];
  }
  static Segtree T_all;
  static Segtree T_even;
  for (int i = 0; i < n; i++) {
    T_all.upd(1, 0, n - 1, i, i, a[i]);
    if ((i + 1) % 2 == 0) {
      T_even.upd(1, 0, n + 10, (i + 1) / 2, (i + 1) / 2, a[i]);
    }
  }
  vector<long long> F(b);
  for (int i = 1; i < m; i++)
    F[i] += F[i - 1];
  vector<long long> Sums;
  for (int i = 0; i + n - 1 < m; i++) {
    long long pref = i == 0 ? 0 : F[i - 1];
    long long sm = F[i + n - 1] - pref;
    if (i % 2 == 1)
      sm = -sm;
    Sums.push_back(sm);
  }
  sort(Sums.begin(), Sums.end());
  auto Try = [&](long long x) {
    auto it = lower_bound(Sums.begin(), Sums.end(), -x);
    if (it == Sums.begin())
      return llabs(x + *it);
    if (it == Sums.end())
      return llabs(x + *--it);
    long long a = llabs(x + *it);
    long long b = llabs(x + *--it);
    return min(a, b);
  };
  long long gu = T_all.Get(1, 0, n - 1) - 2 * T_even.Get(1, 0, n + 10);
  //long long GU = T_all.Get(1, 0, n - 1) - 2 * T_even.Get(1, 0, n + 10);
  cout << Try(gu) << '\n';
  for (int i = 0; i < q; i++) {
    int lft, rgt, x;
    cin >> lft >> rgt >> x;
    lft--, rgt--;
    if ((rgt - lft + 1) % 2 == 1)
      gu += (lft % 2 == 0) ? x : -x;
    cout << Try(gu) << '\n';
    /*lft--, rgt--;
    T_all.upd(1, 0, n - 1, lft, rgt, x);
    if (lft < rgt)
      T_even.upd(1, 0, n + 10, (lft + 2) / 2, (rgt + 1) / 2, x);
    long long ans = T_all.Get(1, 0, n - 1);
    ans -= 2 * T_even.Get(1, 0, n + 10);
    cout << Try(ans) << '\n';
    */
  }
  return 0;
}


#include <algorithm>
#include <iostream>
#include <iomanip>
#include <utility>
#include <cstdlib>
#include <cstdint>
#include <cstdio>
#include <vector>
#include <string>
#include <deque>
#include <cmath>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <sstream>
#include <cstring>
#include <assert.h>
#include <chrono>
#include <random>
#include <functional>
#include <bitset>
#include <iterator>
#include <unordered_map>
#include <unordered_set>

#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

const int MAXN = 1.1e5;

struct Segtree {
  long long T[4 * MAXN];
  long long lazy[4 * MAXN];
  void push(int n) {
    if (lazy[n]) {
      lazy[2 * n] += lazy[n];
      lazy[2 * n + 1] += lazy[n];
      lazy[n] = 0;
    }
  }
  long long Get(int n, int L, int R) {
    return T[n] + lazy[n] * (long long)(R - L + 1);
  }
  void pull(int n, int L, int R) {
    int mid = (L + R) / 2;
    T[n] = Get(2 * n, L, mid) + Get(2 * n + 1, mid + 1, R);
  }
  long long query(int n, int lft, int rgt, int rs, int re) {
    if (lft > re || rgt < rs)
      return 0;
    if (lft >= rs && rgt <= re)
      return T[n];
    int mid = (lft + rgt) / 2;
    push(n);
    long long L = query(2 * n, lft, mid, rs, re);
    long long R = query(2 * n + 1, mid + 1, rgt, rs, re);
    pull(n, lft, rgt);
    return L + R;
  }
  void upd(int n, int lft, int rgt, int rs, int re, int v) {
    if (lft > rgt) return;
    if (lft > re || rgt < rs)
      return;
    if (lft >= rs && rgt <= re) {
      lazy[n] += v;
      return;
    }
    push(n);
    int mid = (lft + rgt) / 2;
    upd(2 * n, lft, mid, rs, re, v);
    upd(2 * n + 1, mid + 1, rgt, rs, re, v);
    pull(n, lft, rgt);
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m, q;
  cin >> n >> m >> q;
  vector<long long> a(n);
  vector<long long> b(m);
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < m; i++) {
    cin >> b[i];
    if (i % 2 == 0) b[i] = -b[i];
  }
  static Segtree T_all;
  static Segtree T_even;
  for (int i = 0; i < n; i++) {
    T_all.upd(1, 0, n - 1, i, i, a[i]);
    if ((i + 1) % 2 == 0) {
      T_even.upd(1, 0, n + 10, (i + 1) / 2, (i + 1) / 2, a[i]);
    }
  }
  vector<long long> F(b);
  for (int i = 1; i < m; i++)
    F[i] += F[i - 1];
  vector<long long> Sums;
  for (int i = 0; i + n - 1 < m; i++) {
    long long pref = i == 0 ? 0 : F[i - 1];
    long long sm = F[i + n - 1] - pref;
    if (i % 2 == 1)
      sm = -sm;
    Sums.push_back(sm);
  }
  sort(Sums.begin(), Sums.end());
  auto Try = [&](long long x) {
    auto it = lower_bound(Sums.begin(), Sums.end(), -x);
    if (it == Sums.begin())
      return llabs(x + *it);
    if (it == Sums.end())
      return llabs(x + *--it);
    long long a = llabs(x + *it);
    long long b = llabs(x + *--it);
    return min(a, b);
  };
  long long gu = T_all.Get(1, 0, n - 1) - 2 * T_even.Get(1, 0, n + 10);
  //long long GU = T_all.Get(1, 0, n - 1) - 2 * T_even.Get(1, 0, n + 10);
  cout << Try(gu) << '\n';
  for (int i = 0; i < q; i++) {
    int lft, rgt, x;
    cin >> lft >> rgt >> x;
    lft--, rgt--;
    T_all.upd(1, 0, n - 1, lft, rgt, x);
    T_even.upd(1, 0, n + 10, (lft + 2) / 2, (rgt + 1) / 2, x);
    gu = T_all.Get(1, 0, n - 1) - 2 * T_even.Get(1, 0, n + 10);
    cout << Try(gu) << '\n';
  }
  return 0;
}


#include <algorithm>
#include <iostream>
#include <iomanip>
#include <utility>
#include <cstdlib>
#include <cstdint>
#include <cstdio>
#include <vector>
#include <string>
#include <deque>
#include <cmath>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <sstream>
#include <cstring>
#include <assert.h>
#include <chrono>
#include <random>
#include <functional>
#include <bitset>
#include <iterator>
#include <unordered_map>
#include <unordered_set>

#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

const int MAXN = 1.1e5;

struct Segtree {
  long long T[4 * MAXN];
  long long lazy[4 * MAXN];
  void push(int n) {
    if (lazy[n]) {
      lazy[2 * n] += lazy[n];
      lazy[2 * n + 1] += lazy[n];
      lazy[n] = 0;
    }
  }
  long long Get(int n, int L, int R) {
    return T[n] + lazy[n] * (long long)(R - L + 1);
  }
  void pull(int n, int L, int R) {
    int mid = (L + R) / 2;
    T[n] = Get(2 * n, L, mid) + Get(2 * n + 1, mid + 1, R);
  }
  long long query(int n, int lft, int rgt, int rs, int re) {
    if (lft > re || rgt < rs)
      return 0;
    if (lft >= rs && rgt <= re)
      return T[n];
    int mid = (lft + rgt) / 2;
    push(n);
    auto L = query(2 * n, lft, mid, rs, re);
    auto R = query(2 * n + 1, mid + 1, rgt, rs, re);
    pull(n, lft, rgt);
    return L + R;
  }
  void upd(int n, int lft, int rgt, int rs, int re, int v) {
    if (lft > re || rgt < rs)
      return;
    if (lft >= rs && rgt <= re) {
      lazy[n] += v;
      return;
    }
    push(n);
    int mid = (lft + rgt) / 2;
    upd(2 * n, lft, mid, rs, re, v);
    upd(2 * n + 1, mid + 1, rgt, rs, re, v);
    pull(n, lft, rgt);
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m, q;
  cin >> n >> m >> q;
  vector<long long> a(n);
  vector<long long> b(m);
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < m; i++) {
    cin >> b[i];
    if (i % 2 == 0) b[i] = -b[i];
  }
  static Segtree T_all;
  static Segtree T_even;
  for (int i = 0; i < n; i++) {
    T_all.upd(1, 0, n - 1, i, i, a[i]);
    if ((i + 1) % 2 == 0) {
      T_even.upd(1, 0, n + 10, (i + 1) / 2, (i + 1) / 2, a[i]);
    }
  }
  vector<long long> F(b);
  for (int i = 1; i < m; i++)
    F[i] += F[i - 1];
  vector<long long> Sums;
  for (int i = 0; i + n - 1 < m; i++) {
    long long pref = i == 0 ? 0 : F[i - 1];
    long long sm = F[i + n - 1] - pref;
    if (i % 2 == 1)
      sm = -sm;
    Sums.push_back(sm);
  }
  sort(Sums.begin(), Sums.end());
  auto Try = [&](long long x) {
    /*auto it = lower_bound(Sums.begin(), Sums.end(), -x);
    if (it == Sums.begin())
      return llabs(x + *it);
    if (it == Sums.end())
      return llabs(x + *--it);
    long long a = llabs(x + *it);
    long long b = llabs(x + *--it);
    return min(a, b);*/
    long long y = (long long)1e18;
    for (int i = 0; i < Sums.size(); i++)
      y = min(y, llabs(Sums[i] + x));
    return y;
  };
  long long GU = T_all.Get(1, 0, n - 1) - 2 * T_even.Get(1, 0, n + 10);
  cout << Try(GU) << '\n';
  for (int i = 0; i < q; i++) {
    int lft, rgt, x;
    cin >> lft >> rgt >> x;
    lft--, rgt--;
    T_all.upd(1, 0, n - 1, lft, rgt, x);
    if (lft < rgt) {
      lft++, rgt++;
      T_even.upd(1, 0, n + 10, (lft + 1) / 2, rgt / 2, x);
    }
    long long ans = T_all.Get(1, 0, n - 1);
    ans -= 2 * T_even.Get(1, 0, n + 10);
    cout << Try(ans) << '\n';
  }
  return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

set<ll> se;

const int maxn=1e5+10;

ll b[maxn];

ll ask(ll x){
    set<ll>::iterator it=se.lower_bound(x);
    ll ans=LONG_LONG_MAX;
    if(it!=se.end()) ans=min(ans,abs(*it-x));
    if(it!=se.begin()) ans=min(ans,abs(*--it-x));
    return ans;
}

int main(){

    int n,m,q;
    ll x;
    ll sum=0;
    scanf("%d%d%d",&n,&m,&q);
    for(int i=1;i<=n;i++){
        scanf("%lld",&x);
        sum+=i&1?x:-x;
    }
    for(int i=1;i<=m;i++){
        scanf("%lld",&b[i]);
    }
    ll sum2=0;
    for(int i=1;i<=n;i++){
        sum2+=i&1?-b[i]:b[i];
    }
    se.insert(sum2);
//    cout<<sum2<<endl;
    for(int i=n+1;i<=m;i++){
        sum2=-sum2;
        sum2-=b[i-n];
        sum2-=b[i];
        se.insert(sum2);
//        cout<<sum2<<endl;
    }

    printf("%lld\n",ask(-sum));
    while(q--){
        int l,r;
        ll tmp;
        scanf("%d%d%lld",&l,&r,&tmp);
        int len=r-l+1;
        if(len&1){
            if(l&1) sum+=tmp;
            else sum-=tmp;
        }
        printf("%lld\n",ask(-sum));
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

set<ll> se;

const int maxn=1e5+10;

ll b[maxn];

ll ask(ll x){
    set<ll>::iterator it=se.lower_bound(x);
    ll ans=LONG_LONG_MAX;
    if(it!=se.end()) ans=min(ans,abs(*it-x));
    if(it!=se.begin()) ans=min(ans,abs(*--it-x));
    return ans;
}

int main(){

    int n,m,q;
    ll x;
    ll sum=0;
    scanf("%d%d%d",&n,&m,&q);
    for(int i=1;i<=n;i++){
        scanf("%lld",&x);
        sum+=i&1?x:-x;
    }
    for(int i=1;i<=m;i++){
        scanf("%lld",&b[i]);
    }
    ll sum2=0;
    for(int i=1;i<=n;i++){
        sum2+=i&1?-b[i]:b[i];
    }
    se.insert(sum2);
//    cout<<sum2<<endl;
    for(int i=n+1;i<=m;i++){
        sum2=-sum2;
        sum2-=b[i-n];
        sum2+=n&1?-b[i]:b[i];
        se.insert(sum2);
//        cout<<sum2<<endl;
    }

    printf("%lld\n",ask(-sum));
    while(q--){
        int l,r;
        ll tmp;
        scanf("%d%d%lld",&l,&r,&tmp);
        int len=r-l+1;
        if(len&1){
            if(l&1) sum+=tmp;
            else sum-=tmp;
        }
        printf("%lld\n",ask(-sum));
    }
    return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
typedef vector<int> vi;

template<class T> inline T re(){
	T N = 0; char c = getchar(); bool neg = 0;
	for (; c < '0' || c > '9'; c = getchar()) neg |= c == '-';
	for (; c >= '0' && c <= '9'; c = getchar())
		N = (N<<3)+(N<<1) + c - '0';
	return neg ? -N : N;
}

const int MX = 1e5;
int n, m, q;
int a[MX + 5];
int b[MX + 5];
LL sumA;

vector<LL> possibleB;
void prepB(){
	LL cursum = 0;
	int mul = 1, sgnf = 1;
	for (int i = 1; i < n; i++){
		cursum += b[i] * mul;
		mul = -mul;
	}
	for (int i = n; i <= m; i++){
		cursum += b[i] * mul;
		possibleB.pb(cursum * sgnf);
		mul = -mul;
		cursum -= b[i - n + 1] * sgnf;
		sgnf = -sgnf;
	}
	sort(possibleB.begin(), possibleB.end());
}

LL getAns(){
	int pos = upper_bound(possibleB.begin(), possibleB.end(), sumA) - possibleB.begin();
	int sz = possibleB.size();
	LL mn = 1e18;
	for (int i = max(0, pos - 2); i < min(pos,  sz); i++){
		mn = min(mn, llabs(sumA - possibleB[i]));
	}
	return mn;
}
// f(j) = |c_1 - c_2 + c_3 - c_4 + ...|
// = |(a1 - b_{1+j}) - (a_2 - b_{2 + j}) + ... |
// = |(a1-a2+a3-a4+...)-(b_{1+j}-b_{2+j}+b_{3+j}-...)|
int main(){
	n = re<int>(); m = re<int>(); q = re<int>();
	for (int i = 1; i <= n; i++) a[i] = re<int>();
	for (int i = 1; i <= m; i++) b[i] = re<int>();

	for (int i = 1, sgn = 1; i <= n; i++){
		sumA += a[i] * sgn;
		sgn = -sgn;
	}

	prepB();
	printf("%lld\n", getAns());
	for (int i = 1, l, r, x; i <= q; i++){
		l = re<int>(); r = re<int>(); x = re<int>();
		if ((r - l + 1) % 2 == 1) {
			int sgn = l & 1 ? 1 : -1;
			sumA += x * sgn;
		}
		printf("%lld\n", getAns());
	}
	return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
typedef vector<int> vi;

template<class T> inline T re(){
	T N = 0; char c = getchar(); bool neg = 0;
	for (; c < '0' || c > '9'; c = getchar()) neg |= c == '-';
	for (; c >= '0' && c <= '9'; c = getchar())
		N = (N<<3)+(N<<1) + c - '0';
	return neg ? -N : N;
}

const int MX = 1e5;
int n, m, q;
int a[MX + 5];
int b[MX + 5];

LL st[4 * MX + 10], lz[4 * MX + 10];
void build(int p = 1, int l = 1, int r = n){
	if (l == r) return void(st[p] = a[l]);
	int md = (l + r) >> 1;
	build(p << 1, l, md);
	build(p << 1 | 1, md + 1, r);
	int sgn = 1 - (((md - l + 1) % 2) * 2);
	st[p] = st[p << 1] + st[p << 1 | 1] * sgn;
}

void prop(int p, int l, int r){
	if (lz[p]){
		st[p] += lz[p] * ((r - l + 1) % 2);
		lz[p] = 0;
	}
}
void upd(int i, int j, LL v, int p = 1, int l = 1, int r = n){
	prop(p, l, r);
	if (j < l || i > r) return;
	if (i <= l && j >= r) {
		lz[p] += v;
		prop(p, l, r);
		return;
	}
	int md = (l + r) >> 1;
	upd(i, j, v, p << 1, l, md);
	upd(i, j, v, p << 1 | 1, md + 1, r);
	int sgn = 1 - (((md - l + 1) % 2) * 2);
	st[p] = st[p << 1] + st[p << 1 | 1] * sgn;
}

LL get(int i, int j, int p = 1, int l = 1, int r = n){
	prop(p, l, r);
	if (j < l || i > r) return 0;
	if (i <= l && j >= r) return st[p];
	int md = (l + r) >> 1;
	int sg = 1 - (((md - l + 1) % 2) * 2);
	return get(i, j, p << 1, l, md) + get(i, j, p << 1 | 1, md + 1, r) * sg;
}

vector<LL> possibleB;
void prepB(){
	LL cursum = 0;
	int mul = 1, sgnf = 1;
	for (int i = 1; i < n; i++){
		cursum += b[i] * mul;
		mul = -mul;
	}
	for (int i = n; i <= m; i++){
		cursum += b[i] * mul;
		possibleB.pb(cursum * sgnf);
		mul = -mul;
		cursum -= b[i - n + 1] * sgnf;
		sgnf = -sgnf;
	}
	sort(possibleB.begin(), possibleB.end());
}

LL getAns(){
	LL fromA = get(1, n);
	int pos = lower_bound(possibleB.begin(), possibleB.end(), fromA) - possibleB.begin();
	int sz = possibleB.size();
	LL mn = 1e18;
	for (int i = max(0, pos - 1); i < min(pos + 1, sz); i++){
		mn = min(mn, llabs(fromA - possibleB[i]));
	}
	return mn;
}
// f(j) = |c_1 - c_2 + c_3 - c_4 + ...|
// = |(a1 - b_{1+j}) - (a_2 - b_{2 + j}) + ... |
// = |(a1-a2+a3-a4+...)-(b_{1+j}-b_{2+j}+b_{3+j}-...)|
int main(){
	n = re<int>(); m = re<int>(); q = re<int>();
	for (int i = 1; i <= n; i++) a[i] = re<int>();
	for (int i = 1; i <= m; i++) b[i] = re<int>();
	build();
	prepB();
	printf("%lld\n", getAns());
	for (int i = 1, l, r, x; i <= q; i++){
		l = re<int>(); r = re<int>(); x = re<int>();
		upd(l, r, x);
		printf("%lld\n", getAns());
	}
	return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
typedef vector<int> vi;

template<class T> inline T re(){
	T N = 0; char c = getchar(); bool neg = 0;
	for (; c < '0' || c > '9'; c = getchar()) neg |= c == '-';
	for (; c >= '0' && c <= '9'; c = getchar())
		N = (N<<3)+(N<<1) + c - '0';
	return neg ? -N : N;
}

const int MX = 1e5;
int n, m, q;
int a[MX + 5];
int b[MX + 5];
LL sumA;

vector<LL> possibleB;
void prepB(){
	LL cursum = 0;
	int mul = 1, sgnf = 1;
	for (int i = 1; i < n; i++){
		cursum += b[i] * mul;
		mul = -mul;
	}
	for (int i = n; i <= m; i++){
		cursum += b[i] * mul;
		possibleB.pb(cursum * sgnf);
		mul = -mul;
		cursum -= b[i - n + 1] * sgnf;
		sgnf = -sgnf;
	}
	sort(possibleB.begin(), possibleB.end());
}

LL getAns(){
	int pos = upper_bound(possibleB.begin(), possibleB.end(), sumA) - possibleB.begin();
	int sz = possibleB.size();
	LL mn = 1e18;
	for (int i = max(0, pos - 2); i < min(pos + 1 , sz); i++){
		mn = min(mn, llabs(sumA - possibleB[i]));
	}
	return mn;
}
// f(j) = |c_1 - c_2 + c_3 - c_4 + ...|
// = |(a1 - b_{1+j}) - (a_2 - b_{2 + j}) + ... |
// = |(a1-a2+a3-a4+...)-(b_{1+j}-b_{2+j}+b_{3+j}-...)|
int main(){
	n = re<int>(); m = re<int>(); q = re<int>();
	for (int i = 1; i <= n; i++) a[i] = re<int>();
	for (int i = 1; i <= m; i++) b[i] = re<int>();

	for (int i = 1, sgn = 1; i <= n; i++){
		sumA += a[i] * sgn;
		sgn = -sgn;
	}

	prepB();
	printf("%lld\n", getAns());
	for (int i = 1, l, r, x; i <= q; i++){
		l = re<int>(); r = re<int>(); x = re<int>();
		if ((r - l + 1) % 2 == 1) {
			int sgn = l & 1 ? 1 : -1;
			sumA += x * sgn;
		}
		printf("%lld\n", getAns());
	}
	return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
//#define int                 long long
#define ll              	long long
#define ld                  long double
#define rep(i,n)            for(int i=0;i<n;i++)
#define hell                998244853
#define inf                 (ll)1e15
#define vi                  vector<int>
#define vii                 vector< vi >
#define pb                  push_back
#define mp                  make_pair
#define fi                  first
#define se                  second
#define pii                 pair<int,int>
#define all(c)              c.begin(),c.end()
#define sz(c)               (int)c.size()
#define hash1               105727LL
#define hash2               hell
//#define endl                '\n'
#define eps                 1e-10
#define pi                  3.141592653589793238462643383279502884197
typedef tree<int,null_type,less<int>,
				rb_tree_tag,tree_order_statistics_node_update> statisticsTree;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a * (b / gcd(a, b)); }
ll power(ll x, ll y,ll p)
{
	ll res=1;
	x=x%p;
	while(y>0)
	{
		if(y&1)
			res=((res%p)*(x%p))%p;
		y=y>>1;
		x=((x%p)*(x%p))%p;
	}
	return res;
}
ll raichu(ll x,ll y)
{
	ll res=1;
	while(y>0)
	{
		if(y&1)
			res=((res)*(x));
		y=y>>1;
		x=((x)*(x));
	}
	return res;
}
bool isprime(ll n)
{
	if(n<2) return false;
	else if(n==2) return true;
	else if(n%2==0) return false;
	else
	{
		ll z=sqrt(n);
		rep(i,z-1) if(n%(i+2)==0) return false;
		return true;
	}
}
int32_t main()
{
	//freopen("B-large.in","r",stdin);
	//freopen("out4.txt","w",stdout);
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int tests=1,Tests;
	//cin>>tests;
	Tests=tests;
	while(tests--)
	{
		int n,m,q;
		cin>>n>>m>>q;
		vi a(n);
		rep(i,n) cin>>a[i];
		vi b(m);
		rep(i,m) cin>>b[i];
		ll A=0;
		rep(i,n)
		{
			if(i%2==0) A+=a[i];
			else A-=a[i];
		}
		vector<ll> B(m-n+1);
		ll sum=0;
		rep(i,n)
		{
			if(i%2==0) sum+=b[i];
			else sum-=b[i];
		}
		B[0]=sum;
		for(int i=1;i<m-n+1;i++)
		{
			if((i-1)%2==0) sum-=b[i-1];
			else sum+=b[i-1];
			if((i+n-1)%2==0) sum+=b[i+n-1];
			else sum-=b[i+n-1];
			if(i%2==0) B[i]=sum;
			else B[i]=-sum;
		}
		sort(all(B));
		int idx=upper_bound(B.begin(),B.end(),A)-B.begin();
		ll ans=LLONG_MAX;
		if(idx!=0) ans=min(ans,A-B[idx-1]);
		if(idx!=sz(B))ans=min(ans,B[idx]-A);
		cout<<ans<<endl;
		while(q--)
		{
			int l,r,x;
			cin>>l>>r>>x;
			l--;
			r--;
			if((l-r+1)%2==0) cout<<ans<<endl;
			else
			{
				if(l%2==0) A+=x;
				else A-=x;
				idx=upper_bound(B.begin(),B.end(),A)-B.begin();
				ans=LLONG_MAX;
				if(idx!=0) ans=min(ans,A-B[idx-1]);
				if(idx!=sz(B))ans=min(ans,B[idx]-A);
				cout<<ans<<endl;
			}
		}
	}
	return 0;
}


//                                               KEEP GRINDING
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

template <typename T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

#define int long long
#define ld long double
#define MOD 1000000007
#define precise(i) fixed << setprecision(10) << i
#define f(i,a,b) for(int i=a;i<b;++i)

#define endl '\n'
#define debug cout<<"\n========================================\n";
#define err1(a) cout<<#a<<" "<<a<<endl;
#define err2(a,b) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<endl;
#define err3(a,b,c) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<endl;
#define err4(a,b,c,d) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<" "<<#d<<" "<<d<<endl;

#define all(a) a.begin(),a.end()
#define show(a) for(auto xyz:a)cout<<xyz<<" ";cout<<endl;
#define PQ priority_queue
#define LB lower_bound
#define UB upper_bound

#define fr first
#define sc second
#define sz(x) (int)(x).size()
void solve(){
	int N,M,Q,S=0;	cin>>N>>M>>Q;
	vector<int> a(N+1),b(M+1,0),sum(M-N+1);
	f(i,1,N+1){
		cin>>a[i];
		S+=( i&1 ? (int)1 : (int)-1)*a[i];
	}
	f(i,1,M+1){
		cin>>b[i];
		if( !(i&1) )	b[i]*=(-1);
		b[i]+=b[i-1];
	}
	f(j,0,M-N+1){
		sum[j]=( j&1 ? (int)-1 : (int)1 )*(b[j+N]-b[j]);
	}
	sort(all(sum));
	auto fun=[&](int q){
		int ans=1e18;
		auto itr=LB(all(sum),q);
		if(itr!=sum.end()){
			ans=min(ans,(*itr)-q);
		}
		if(itr!=sum.begin()){
			itr--;
			ans=min(ans,q-(*itr));
		}
		return ans;
	};
	cout<<fun(S)<<endl;
	while(Q--){
		int l,r,x;	cin>>l>>r>>x;
		if( (r+1-l)&1 )	S+=( l&1 ? (int)1 : (int)-1 )*x;
		cout<<fun(S)<<endl;
	}

}
signed main(){
    fastio
    int t=1;
    for(;t--;solve());
}


#include <bits/stdc++.h>
#define ll long long
#define fastIO cout << fixed << setprecision(12), ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;

const int N = 2e5 + 5, M = 5e2 + 9, MOD = 1e9 + 7;

set<ll> st;
int B[N];

ll solve(ll num) {
	auto it = st.lower_bound(num);
	if(it == st.end()) --it;
	ll ans = llabs(num + (*it));
	if(it != st.begin()) --it;
	ans = min(ans, llabs(num + (*it)));
	return ans;
}

void runtestcase() {
	int n, m, q, l, r, x;
	cin >> n >> m >> q;
	ll sumA = 0, sumB = 0;
	for (int i = 0; i < n; ++i) {
		cin >> x;
		sumA += (i % 2 == 0 ? 1 : -1) * x;
	}
	for (int i = 0; i < n; ++i) {
		cin >> B[i];
		sumB += (i % 2 == 0 ? -1 : 1) * B[i];
	}
	st.insert(sumB);
	for (int i = n; i < m; ++i) {
		cin >> B[i];
		sumB += B[i - n];
		sumB = -sumB;
		sumB += (n % 2 == 0 ? 1 : -1) * B[i];
		st.insert(sumB);
	}
	cout << solve(sumA) << '\n';
	while(q--) {
		cin >> l >> r >> x;
		int dist = r - l + 1;
		if(dist % 2 != 0) {
			if(l & 1)	sumA += x;
			else sumA -= x;
		}
		cout << solve(sumA) << '\n';
	}
}

int main() {
	fastIO;
#ifdef LOCAL
	freopen("input.in", "rt", stdin);
#endif
	int t = 1;
//	cin >> t;
	while(t--)
		runtestcase();
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define fastIO cout << fixed << setprecision(12), ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;

const int N = 2e5 + 5, M = 5e2 + 9, MOD = 1e9 + 7;

set<ll> st;
int B[N];

ll solve(ll num) {
	auto it = st.lower_bound(num);
	if(it == st.end()) --it;
	ll ans = llabs(num - (*it));
	if(it != st.begin()) --it;
	ans = min(ans, llabs(num - (*it)));
	return ans;
}

void runtestcase() {
	int n, m, q, l, r, x;
	cin >> n >> m >> q;
	ll sumA = 0, sumB = 0;
	for (int i = 0; i < n; ++i) {
		cin >> x;
		sumA += (i % 2 == 0 ? 1 : -1) * x;
	}
	for (int i = 0; i < n; ++i) {
		cin >> B[i];
		sumB += (i % 2 == 0 ? -1 : 1) * B[i];
	}
	st.insert(sumB);
	for (int i = n; i < m; ++i) {
		cin >> B[i];
		sumB += B[i - n];
		sumB = -sumB;
		sumB += (n % 2 == 0 ? 1 : -1) * B[i];
		st.insert(sumB);
	}
	cout << solve(-sumA) << '\n';
	while(q--) {
		cin >> l >> r >> x;
		int dist = r - l + 1;
		if(dist % 2 != 0) {
			if(l & 1)	sumA += x;
			else sumA -= x;
		}
		cout << solve(-sumA) << '\n';
	}
}

int main() {
	fastIO;
#ifdef LOCAL
	freopen("input.in", "rt", stdin);
#endif
	int t = 1;
//	cin >> t;
	while(t--)
		runtestcase();
	return 0;
}


#define fast ios::sync_with_stdio(false); cin.tie(0)
#define foru(i, k, n) for (int i = k; i < n; i++)
#define ford(i, k, n) for (int i = k; i >= n; i--)
#define pb push_back

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <list>
#include <queue>
#include <unordered_map>

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int n, m, q;
vector<ll> a, b, bvals;
ll es = 0, os = 0;

void initBVals() {
	ll esb = 0, osb = 0;
	foru(i, 0, n) {
		if (i & 1)osb += b[i];
		else esb += b[i];
	}
	bvals.pb(esb - osb);
	foru(i, n, m) {
		if ((i - n) & 1)osb -= b[i - n];
		else esb -= b[i - n];
		if (i & 1)osb += b[i];
		else esb += b[i];
		int st = i - n + 1;
		if (st & 1)bvals.pb(osb - esb);
		else bvals.pb(esb - osb);
	}
	sort(bvals.begin(), bvals.end());
}

void input(){
	cin >> n >> m >> q;
	a.resize(n);
	foru(i, 0, n) {
		cin >> a[i];
		if (i & 1)os += a[i];
		else es += a[i];
	}
	b.resize(m);
	foru(i, 0, m)cin >> b[i];
}

inline ll closestTo(ll val) {
	int l = 1, r = m - n + 1, mid, bst = -1;
	while (l <= r) {
		mid = (l + r + 1) / 2;
		if (bvals[mid - 1] >= val) {
			bst = mid - 1;
			r = mid - 1;
		}
		else l = mid + 1;
	}
	if (bst == -1)return bvals.back();
	else {
		if (!bst)return bvals[bst];
		ll aopt = abs(val - bvals[bst]), bopt = abs(val - bvals[bst - 1]);
		if (aopt > bopt)return bvals[bst - 1];
		else return bvals[bst];
	}
}

inline ll getVal() { return es - os; }

inline void query(int l, int r, ll amt) {
	int len = r - l + 1;
	es += (len / 2)*amt;
	os += (len / 2)*amt;
	if (len & 1) {
		if (l & 1)os += amt;
		else es += amt;
	}
}

int main() {
	fast;
	input();
	initBVals();
	ll tmp = getVal();
	cout << abs(closestTo(tmp) - tmp) << endl;
	while (q--) {
		int l, r, x;
		cin >> l >> r >> x;
		query(--l, --r, x);
		tmp = getVal();
		cout<<abs(closestTo(tmp)-tmp)<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ii pair<int, int>
const int mxN = 1e5;
ll a[mxN + 5], b[mxN + 5];
int N, M, Q;
ll aSum = 0, bInit;
vector<ll> bSum;

void findMin() {
    ll ret = LLONG_MAX;
    int pos = (int)(lower_bound(bSum.begin(), bSum.end(), aSum) - bSum.begin());
    if (pos < bSum.size()) {
        ret = min(ret, abs(aSum - bSum[pos]));
    }
    if (pos > 0) {
        ret = min(ret, abs(aSum - bSum[pos - 1]));
    }
    cout << ret << "\n";
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> N >> M >> Q;
    for (int i = 1; i <= N; i++) {
        cin >> a[i];
        aSum += (i % 2 ? 1 : -1) * a[i];
    }
    for (int i = 1; i <= M; i++) {
        cin >> b[i];
        if (i <= N) {
            bInit += (i % 2 ? 1 : -1) * b[i];
        }
    }
    bSum.push_back(bInit);
    for (int i = N + 1; i <= M; i++) {
        bInit = -bInit + (b[i] + b[i - N]);
        bSum.push_back(bInit);
    }
    sort(bSum.begin(), bSum.end());
    findMin();

    for (int i = 0, l, r, x; i < Q; i++) {
        cin >> l >> r >> x;
        if (l % 2 == r % 2) {
            aSum += (l % 2 == 1 ? 1 : -1) * x;
        }
        findMin();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ii pair<int, int>
const int mxN = 1e5;
ll a[mxN + 5], b[mxN + 5];
int N, M, Q;
ll aSum = 0, bInit;
vector<ll> bSum;

void findMin() {
    ll ret = LLONG_MAX;
    int pos = (int)(upper_bound(bSum.begin(), bSum.end(), aSum) - bSum.begin());
    if (pos < bSum.size()) {
        ret = min(ret, abs(aSum - bSum[pos]));
    }
    if (pos > 0) {
        ret = min(ret, abs(aSum - bSum[pos - 1]));
    }
    cout << ret << "\n";
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> N >> M >> Q;
    for (int i = 1; i <= N; i++) {
        cin >> a[i];
        aSum += (i % 2 ? 1 : -1) * a[i];
    }
    for (int i = 1; i <= M; i++) {
        cin >> b[i];
        if (i <= N) {
            bInit += (i % 2 ? 1 : -1) * b[i];
        }
    }
    bSum.push_back(bInit);
    for (int i = N + 1; i <= M; i++) {
        bInit = -bInit + (b[i] + b[i - N]);
        bSum.push_back(bInit);
    }
    sort(bSum.begin(), bSum.end());
    findMin();

    for (int i = 0, l, r, x; i < Q; i++) {
        cin >> l >> r >> x;
        if (l % 2 == r % 2) {
            aSum += (l % 2 == 1 ? 1 : -1) * x;
        }
        findMin();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ii pair<int, int>
const int mxN = 1e5;
ll a[mxN + 5], b[mxN + 5];
int N, M, Q;
ll aSum = 0, bInit;
vector<ll> bSum;

void findMin() {
    ll ret = LLONG_MAX;
    int pos = (int)(lower_bound(bSum.begin(), bSum.end(), aSum) - bSum.begin());
    if (pos < bSum.size()) {
        ret = min(ret, abs(aSum - bSum[pos]));
    }
    if (pos > 0) {
        ret = min(ret, abs(aSum - bSum[pos - 1]));
    }
    cout << ret << "\n";
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> N >> M >> Q;
    for (int i = 1; i <= N; i++) {
        cin >> a[i];
        aSum += (i % 2 ? 1 : -1) * a[i];
    }
    for (int i = 1; i <= M; i++) {
        cin >> b[i];
        if (i <= N) {
            bInit += (i % 2 ? 1 : -1) * b[i];
        }
    }
    bSum.push_back(bInit);
    for (int i = N + 1; i <= M; i++) {
        bInit = -bInit + ((N % 2 == 1 ? 1 : -1) * b[i] + b[i - N]);
        bSum.push_back(bInit);
    }
    sort(bSum.begin(), bSum.end());
    findMin();

    for (int i = 0, l, r, x; i < Q; i++) {
        cin >> l >> r >> x;
        if (l % 2 == r % 2) {
            aSum += (l % 2 == 1 ? 1 : -1) * x;
        }
        findMin();
    }
    return 0;
}


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

const size_t max_string_length = 100000;
template<class T> struct is_cstr: std::false_type {}; template<int N> struct is_cstr<char[N]> : std::true_type {};
template<typename T> constexpr string formcomm(const T& a) {if constexpr (is_same_v<T, int> || is_same_v<T, size_t>) return "%d"; else if constexpr (is_same_v<T, long long int>) return "%lld"; else if constexpr (is_same_v<T, short int>) return "%hd"; else if constexpr (is_same_v<T, unsigned int>) return "%u"; else if constexpr (is_same_v<T, unsigned long long int>) return "%llu"; else if constexpr (is_same_v<T, unsigned short int>) return "%hu"; else if constexpr (is_same_v<T, char>) return "%c"; else {printf("Type %s non prévu\n", typeid(a).name()); terminate();}}
template<typename T> constexpr string formout(const T& a) {if constexpr (is_same_v<T, float>) return "%.16f"; else if constexpr (is_same_v<T, double>) return "%.16lf"; else if constexpr (is_same_v<T, long double>) return "%.16Lf"; else if constexpr (is_same_v<T, string>) return "%s"; else if constexpr (is_cstr<T>::value) return "%s"; else return formcomm(a);}
template<typename T> const T& addout(const T& a) {return a;} const char* addout(const string& a) {return a.c_str();}
template<typename... Ts> void out(Ts&&... ts) {printf((formout(ts)+...).c_str(), addout(ts)...);}
template<typename It> void outs(It a, It b, string mid = " ", string fin = "\n") {if (a != b) {out(*a); ++a; for (; a != b; ++a) out (mid, *a);} out(fin);}
template<typename T> void outs(const T& l, string mid = " ", string fin = "\n") {outs(l.begin(), l.end(), mid, fin);}
template<typename T> constexpr string formin(const T& a) {if constexpr (is_same_v<T, float>) return "%f"; else if constexpr (is_same_v<T, double>) return "%lf"; else if constexpr (is_same_v<T, long double>) return "%Lf"; else if constexpr (is_same_v<T, string>) return "%s"; else if constexpr (is_cstr<T>::value) return "%s"; else return formcomm(a);}
template<typename T> constexpr int nstring(const T& a) {if constexpr (is_same_v<T, string>) return 1; else return 0;}
template<typename T> T* addin(T& a, char*& buf, string**& as) {return &a;} char* addin(string& a, char*& buf, string**& as) {char* ob = buf;buf+=max_string_length+1;*as = &a;++as;return ob;}
template<typename... Ts> void in(Ts&&... ts) {int ns=(nstring(ts)+...);string* as[max(ns,1)];char buf[1+(max_string_length+1)*ns];char* b=buf;string** c=as;
 scanf((formin(ts)+...).c_str(), addin(ts, b, c)...); while (c > as) {--c; b-=max_string_length+1; **c = string(b);}}
template<typename It> void ins(It a, It b) {for (; a != b; ++a) in (*a);} template<typename T> void ins(T& l) {ins(l.begin(), l.end());}
void inl(char* s) {scanf("%[^\n]", s);} void inl(string& s) {char buf[max_string_length+1]; inl(buf); s = string(buf);}

int main()
{
 int n, m, q;
 in(n, m, q);
 vector<ll> a(n), b(m);
 ins(a); ins(b);
 ll sA = 0; ll s = 1;
 vector<ll> sB(m-n+1);
 for (int i = 0; i < n; ++i) {sA += s * a[i]; sB[0] += s*b[i]; s = -s;}
 for (int j = 1; j <= m-n; ++j) sB[j] = -sB[j-1] + b[j-1] - s*b[n+j-1];
 sort(sB.begin(), sB.end());
 for (int i = 0; i <= q; ++i)
 {
  if (i > 0)
  {
   int l, r; ll x;
   in(l, r, x);
   if (l%2 == 1 && r%2 == 1) sA += x;
   if (l%2 == 0 && r%2 == 0) sA -= x;
  }
  int u = 0;
  int v = m-n+1;
  while (u+1 < v)
  {
   int mid = (u+v) >> 1;
   if (sB[mid] <= sA) u = mid; else v = mid;
  }
  if (v == m-n+1) out(abs(sA-sB[u]), "\n");
  else out(min(abs(sA-sB[u]), abs(sA-sB[v])), "\n");
 }

 return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <time.h>
#include <string>
#include <set>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <ext/rope>
#include <algorithm>
#include <iostream>
using namespace std;
#define ll long long
#define minv 1e-6
#define inf 1e15
#define pi 3.1415926536
#define E  2.7182818284
const ll mod=1e9+7;//998244353
const int maxn=1e5+10;

ll b[maxn],f[maxn];

int main()
{
    int n,m,q,g=0,i,l,r;
    ll a,tot=0,sum=0,v;
    scanf("%d%d%d",&n,&m,&q);
    for (i=1;i<=n;i++)
    {
        scanf("%lld",&a);
        if (i & 1)
            sum+=a;
        else
            sum-=a;
    }
    for (i=1;i<=m;i++)
    {
        scanf("%lld",&b[i]);
        if (i & 1)
            tot-=b[i];
        else
            tot+=b[i];
        if (i>=n)
        {
            g++;
            f[g]=tot;
            tot-=b[i-n+1];
        }
    }

        i=lower_bound(f+1,f+g+1,sum)-f;
        v=inf;
        if (i-1>=1)
            v=min(v,abs(sum-f[i-1]));
        if (i<=g)
            v=min(v,abs(sum-f[i]));
        if (i+1<=g)
            v=min(v,abs(sum-f[i+1]));
        printf("%lld\n",v);

    while (q--)
    {
        scanf("%d%d%lld",&l,&r,&a);
        if ((r-l)%2==0)
        {
            if (l & 1)
                sum+=r-l+1;
            else
                sum-=r-l+1;
        }

        i=lower_bound(f+1,f+g+1,sum)-f;
        v=inf;
        if (i-1>=1)
            v=min(v,abs(sum-f[i-1]));
        if (i<=g)
            v=min(v,abs(sum-f[i]));
        if (i+1<=g)
            v=min(v,abs(sum-f[i+1]));
        printf("%lld\n",v);
    }
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <time.h>
#include <string>
#include <set>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <ext/rope>
#include <algorithm>
#include <iostream>
using namespace std;
#define ll long long
#define minv 1e-6
#define inf 1e18
#define pi 3.1415926536
#define E  2.7182818284
const ll mod=1e9+7;//998244353
const int maxn=1e5+10;

ll b[maxn],f[maxn];

int main()
{
    int n,m,q,g=0,i,l,r;
    ll a,tot=0,sum=0,v;
    scanf("%d%d%d",&n,&m,&q);
    for (i=1;i<=n;i++)
    {
        scanf("%lld",&a);
        if (i & 1)
            sum+=a;
        else
            sum-=a;
    }
    for (i=1;i<=m;i++)
    {
        scanf("%lld",&b[i]);
        if (i & 1)
            tot+=b[i];
        else
            tot-=b[i];
        if (i>=n)
        {
            g++;
            if ((i-n) & 1)
                f[g]=-tot;
            else
                f[g]=tot;
            if ((i-n+1) & 1)
                tot-=b[i-n+1];
            else
                tot+=b[i-n+1];
        }
    }

        i=lower_bound(f+1,f+g+1,sum)-f;
        v=inf;
        if (i-1>=1 && i-1<=g)
            v=min(v,abs(sum-f[i-1]));
        if (i>=1 && i<=g)
            v=min(v,abs(sum-f[i]));
        if (i+1>=1 && i+1<=g)
            v=min(v,abs(sum-f[i+1]));
        printf("%lld\n",v);

    while (q--)
    {
        scanf("%d%d%lld",&l,&r,&a);
        if ((r-l)%2==0)
        {
            if (l & 1)
                sum+=a;
            else
                sum-=a;
        }

        i=lower_bound(f+1,f+g+1,sum)-f;
        v=inf;
        if (i-1>=1 && i-1<=g)
            v=min(v,abs(sum-f[i-1]));
        if (i>=1 && i<=g)
            v=min(v,abs(sum-f[i]));
        if (i+1>=1 && i+1<=g)
            v=min(v,abs(sum-f[i+1]));
        printf("%lld\n",v);
    }
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <time.h>
#include <string>
#include <set>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <ext/rope>
#include <algorithm>
#include <iostream>
using namespace std;
#define ll long long
#define minv 1e-6
#define inf 1e18
#define pi 3.1415926536
#define E  2.7182818284
const ll mod=1e9+7;//998244353
const int maxn=1e5+10;

ll b[maxn],f[maxn];

int main()
{
    int n,m,q,g=0,i,l,r;
    ll a,tot=0,sum=0,v;
    scanf("%d%d%d",&n,&m,&q);
    for (i=1;i<=n;i++)
    {
        scanf("%lld",&a);
        if (i & 1)
            sum+=a;
        else
            sum-=a;
    }
    for (i=1;i<=m;i++)
    {
        scanf("%lld",&b[i]);
        if (i & 1)
            tot+=b[i];
        else
            tot-=b[i];
        if (i>=n)
        {
            g++;
            if ((i-n) & 1)
                f[g]=-tot;
            else
                f[g]=tot;
            if ((i-n+1) & 1)
                tot-=b[i-n+1];
            else
                tot+=b[i-n+1];
        }
    }
    sort(f+1,f+g+1);

        i=lower_bound(f+1,f+g+1,sum)-f;
        v=inf;
        if (i-1>=1 && i-1<=g)
            v=min(v,abs(sum-f[i-1]));
        if (i>=1 && i<=g)
            v=min(v,abs(sum-f[i]));
        if (i+1>=1 && i+1<=g)
            v=min(v,abs(sum-f[i+1]));
        printf("%lld\n",v);

    while (q--)
    {
        scanf("%d%d%lld",&l,&r,&a);
        if ((r-l)%2==0)
        {
            if (l & 1)
                sum+=a;
            else
                sum-=a;
        }

        i=lower_bound(f+1,f+g+1,sum)-f;
        v=inf;
        if (i-1>=1 && i-1<=g)
            v=min(v,abs(sum-f[i-1]));
        if (i>=1 && i<=g)
            v=min(v,abs(sum-f[i]));
        if (i+1>=1 && i+1<=g)
            v=min(v,abs(sum-f[i+1]));
        printf("%lld\n",v);
    }
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <time.h>
#include <string>
#include <set>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <ext/rope>
#include <algorithm>
#include <iostream>
using namespace std;
#define ll long long
#define minv 1e-6
#define inf 1e18
#define pi 3.1415926536
#define E  2.7182818284
const ll mod=1e9+7;//998244353
const int maxn=1e5+10;

ll b[maxn],f[maxn];

int main()
{
    int n,m,q,g=0,i,l,r;
    ll a,tot=0,sum=0,v;
    scanf("%d%d%d",&n,&m,&q);
    for (i=1;i<=n;i++)
    {
        scanf("%lld",&a);
        if (i & 1)
            sum+=a;
        else
            sum-=a;
    }
    for (i=1;i<=m;i++)
    {
        scanf("%lld",&b[i]);
        if (i & 1)
            tot+=b[i];
        else
            tot-=b[i];
        if (i>=n)
        {
            g++;
            if ((i-n) & 1)
                f[g]=-tot;
            else
                f[g]=tot;
            if ((i-n+1) & 1)
                tot-=b[i-n+1];
            else
                tot+=b[i-n+1];
        }
    }

        i=lower_bound(f+1,f+g+1,sum)-f;
        v=inf;
        if (i-1<=g)
            v=min(v,abs(sum-f[i-1]));
        if (i<=g)
            v=min(v,abs(sum-f[i]));
        if (i+1<=g)
            v=min(v,abs(sum-f[i+1]));
        printf("%lld\n",v);

    while (q--)
    {
        scanf("%d%d%lld",&l,&r,&a);
        if ((r-l)%2==0)
        {
            if (l & 1)
                sum+=a;
            else
                sum-=a;
        }

        i=lower_bound(f+1,f+g+1,sum)-f;
        v=inf;
        if (i-1>=1)
            v=min(v,abs(sum-f[i-1]));
        if (i<=g)
            v=min(v,abs(sum-f[i]));
        if (i+1<=g)
            v=min(v,abs(sum-f[i+1]));
        printf("%lld\n",v);
    }
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <time.h>
#include <string>
#include <set>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <ext/rope>
#include <algorithm>
#include <iostream>
using namespace std;
#define ll long long
#define minv 1e-6
#define inf 1e15
#define pi 3.1415926536
#define E  2.7182818284
const ll mod=1e9+7;//998244353
const int maxn=1e5+10;

ll b[maxn],f[maxn];

int main()
{
    int n,m,q,g=0,i,l,r;
    ll a,tot=0,sum=0,v;
    scanf("%d%d%d",&n,&m,&q);
    for (i=1;i<=n;i++)
    {
        scanf("%lld",&a);
        if (i & 1)
            sum+=a;
        else
            sum-=a;
    }
    for (i=1;i<=m;i++)
    {
        scanf("%lld",&b[i]);
        if (i & 1)
            tot+=b[i];
        else
            tot-=b[i];
        if (i>=n)
        {
            g++;
            if ((i-n) & 1)
                f[g]=-tot;
            else
                f[g]=tot;
            if ((i-n+1) & 1)
                tot-=b[i-n+1];
            else
                tot+=b[i-n+1];
        }
    }

        i=lower_bound(f+1,f+g+1,sum)-f;
        v=inf;
        if (i-1>=1)
            v=min(v,abs(sum-f[i-1]));
        if (i<=g)
            v=min(v,abs(sum-f[i]));
        if (i+1<=g)
            v=min(v,abs(sum-f[i+1]));
        printf("%lld\n",v);

    while (q--)
    {
        scanf("%d%d%lld",&l,&r,&a);
        if ((r-l)%2==0)
        {
            if (l & 1)
                sum+=a;
            else
                sum-=a;
        }

        i=lower_bound(f+1,f+g+1,sum)-f;
        v=inf;
        if (i-1>=1)
            v=min(v,abs(sum-f[i-1]));
        if (i<=g)
            v=min(v,abs(sum-f[i]));
        if (i+1<=g)
            v=min(v,abs(sum-f[i+1]));
        printf("%lld\n",v);
    }
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <time.h>
#include <string>
#include <set>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <ext/rope>
#include <algorithm>
#include <iostream>
using namespace std;
#define ll long long
#define minv 1e-6
#define inf 1e18
#define pi 3.1415926536
#define E  2.7182818284
const ll mod=1e9+7;//998244353
const int maxn=1e5+10;

ll b[maxn],f[maxn];

int main()
{
    int n,m,q,g=0,i,l,r;
    ll a,tot=0,sum=0,v;
    scanf("%d%d%d",&n,&m,&q);
    for (i=1;i<=n;i++)
    {
        scanf("%lld",&a);
        if (i & 1)
            sum+=a;
        else
            sum-=a;
    }
    for (i=1;i<=m;i++)
    {
        scanf("%lld",&b[i]);
        if (i & 1)
            tot+=b[i];
        else
            tot-=b[i];
        if (i>=n)
        {
            g++;
            if ((i-n) & 1)
                f[g]=-tot;
            else
                f[g]=tot;
            if ((i-n+1) & 1)
                tot-=b[i-n+1];
            else
                tot+=b[i-n+1];
        }
    }

        i=lower_bound(f+1,f+g+1,sum)-f;
        v=inf;
        if (i!=1)
            v=min(v,abs(sum-f[i-1]));
        if (i<=g)
            v=min(v,abs(sum-f[i]));
        if (i+1<=g)
            v=min(v,abs(sum-f[i+1]));
        printf("%lld\n",v);

    while (q--)
    {
        scanf("%d%d%lld",&l,&r,&a);
        if ((r-l)%2==0)
        {
            if (l & 1)
                sum+=a;
            else
                sum-=a;
        }

        i=lower_bound(f+1,f+g+1,sum)-f;
        v=inf;
        if (i!=1)
            v=min(v,abs(sum-f[i-1]));
        if (i<=g)
            v=min(v,abs(sum-f[i]));
        if (i+1<=g)
            v=min(v,abs(sum-f[i+1]));
        printf("%lld\n",v);
    }
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <time.h>
#include <string>
#include <set>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <ext/rope>
#include <algorithm>
#include <iostream>
using namespace std;
#define ll long long
#define minv 1e-6
#define inf 1e15
#define pi 3.1415926536
#define E  2.7182818284
const ll mod=1e9+7;//998244353
const int maxn=1e5+10;

ll b[maxn],f[maxn];

int main()
{
    int n,m,q,g=0,i,l,r;
    ll a,tot=0,sum=0,v;
    scanf("%d%d%d",&n,&m,&q);
    for (i=1;i<=n;i++)
    {
        scanf("%lld",&a);
        if (i & 1)
            sum+=a;
        else
            sum-=a;
    }
    for (i=1;i<=m;i++)
    {
        scanf("%lld",&b[i]);
        if (i & 1)
            tot+=b[i];
        else
            tot-=b[i];
        if (i>=n)
        {
            g++;
            if ((i-n) & 1)
                f[g]=-tot;
            else
                f[g]=tot;
            if ((i-n+1) & 1)
                tot-=b[i-n+1];
            else
                tot+=b[i-n+1];
        }
    }

        i=lower_bound(f+1,f+g+1,sum)-f;
        v=inf;
        if (i-1>=1 && i-1<=g)
            v=min(v,abs(sum-f[i-1]));
        if (i>=1 && i<=g)
            v=min(v,abs(sum-f[i]));
        if (i+1>=1 && i+1<=g)
            v=min(v,abs(sum-f[i+1]));
        printf("%lld\n",v);

    while (q--)
    {
        scanf("%d%d%lld",&l,&r,&a);
        if ((r-l)%2==0)
        {
            if (l & 1)
                sum+=a;
            else
                sum-=a;
        }

        i=lower_bound(f+1,f+g+1,sum)-f;
        v=inf;
        if (i-1>=1)
            v=min(v,abs(sum-f[i-1]));
        if (i<=g)
            v=min(v,abs(sum-f[i]));
        if (i+1<=g)
            v=min(v,abs(sum-f[i+1]));
        printf("%lld\n",v);
    }
	return 0;
}


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

template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl

using ll = long long;

const ll mod = 1e9+7;
const int maxn = 1e6 + 5;
const ll inf = 1e18;

int n, m, q;
ll atot=0;

ll a[maxn];
ll b[maxn][2];
ll t[maxn*4], o[maxn*4];

set<ll> B;

ll solve(int l, int r, ll dx) {
    int len = r-l+1;
    if (l%2 == 1) {
	atot += dx*((len+1)/2);
	atot -= dx*(len/2);
    } else {
	atot -= dx*((len+1)/2);
	atot += dx*(len/2);
    }

    ll res = inf;
    // pick best j

    auto iter = B.lower_bound(-atot);
    if (iter == B.end()) --iter;
    res = min(res, abs(atot + *iter));
    ++iter;
    if (iter != B.end())  {
	res = min(res, abs(atot + *iter));
    }
    --iter;
    if (iter != B.begin()) {
	--iter;
	res = min(res, abs(atot + *iter));
    }

    return res;
}

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

    cin>>n>>m>>q;

    for (int i=1; i<=n; i++) {
	cin>>a[i];
	if (i%2==1) {
	    atot += a[i];
	} else {
	    atot -= a[i];
	}
    }

    for (int i=1; i<=m; i++) {
	cin>>b[i][i%2];
    }

    for (int i=1; i<=m; i++) {
	b[i][0] += b[i-1][0];
	b[i][1] += b[i-1][1];
    }

    for (int i=1; i+n-1<=m; i++) {
	ll x = b[i+n-1][0] - b[i-1][0];
	ll y = b[i+n-1][1] - b[i-1][1];

	if (i%2==1) {
	    y *= -1;
	} else {
	    x *= -1;
	}
	B.insert(x+y);
    }

    cout<<solve(1,1,0)<<"\n";

    while (q--) {
	int l,r; cin>>l>>r;
	ll dx; cin>>dx;
	cout<<solve(l,r,dx)<<"\n";
    }

    return 0;
}


/// IN THE NAME OF METAL
#include <bits/stdc++.h>

using namespace std;

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

#define sz(x)                       (ll) x.size()
#define all(x)                      (x).begin(),(x).end()
#define F                           first
#define S                           second

ll Pow(ll a, ll b, ll md, ll ans = 1) {
    for (; b; b >>= 1, a = a * a % md)
        if (b & 1)
            ans = ans * a % md;
    return ans % md;
}

const ll MAXN = 1e6 + 10;
const ll INF = 8e18;
const ll MOD = 1e9 + 7;
ll n, m, q, sum = 0, F[MAXN], A[MAXN], B[MAXN], ps[MAXN];
set<ll> st;

ll Query() {
    auto it = st.lower_bound(-sum);
    ll ans = INF;
    if (it != st.end()) ans = abs(*it + sum);
    if (it != st.begin()) it--, ans = min(ans, abs(sum + *it));
    return ans;
}

int main() {
    scanf("%lld%lld%lld", &n, &m, &q);
    for (ll i = 1; i <= n; i++) scanf("%lld", &A[i]), sum += (i & 1 ? A[i] : -A[i]);
    for (ll i = 1; i <= m; i++) {
        scanf("%lld", &B[i]);
        ps[i] = ps[i - 1] + (i & 1 ? -B[i] : B[i]);
    }
    for (ll i = 0; i <= m - n; i++) {
        F[i] = (ps[n + i] - ps[i]) * (i & 1 ? -1 : 1);
        st.insert(F[i]);
    }
    printf("%lld\n", Query());
    while (q--) {
        ll l, r, val;
        scanf("%lld%lld%lld", &l, &r, &val);
        if ((r - l + 1) & 1) sum += (r & 1 ? val : -val);
        printf("%lld\n", Query());
    }
    return 0;
}


#pragma GCC optimize ("Ofast")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define pb push_back
#define all(_obj) _obj.begin(),_obj.end()
#define F first
#define S second
#define INF 1e18
#define pll pair<ll, ll>
#define vll vector<ll>
#define deb(x) cerr<<#x<<' '<<'='<<' '<<x<<'\n'
ll n,m,a,b,c,k,temp,x,y;
const int MAXN=1e5+11;
inline ll max(ll a,ll b) {return ((a>b)?a:b);}
inline ll min(ll a,ll b) {return ((a>b)?b:a);}
inline ll gcd(ll a ,ll b) {if(b==0) return a;a%=b;return gcd(b,a);}
inline vll read(int n) {vll v(n);for (int i = 0; i < v.size(); i++)cin>>v[i];return v;}
void solveforthiscase(const int & test)
{
int q;
cin>>n>>m>>q;
vll a=read(n);
vll b=read(m);
vector<ll> v(m-n+1);
ll psbo[m];
ll psbe[m];
psbo[0]=0;
psbe[0]=b[0];
for(int i=1;i<m;i++)
{
psbo[i]=psbo[i-1],psbe[i]=psbe[i-1];
if(i&1)
psbo[i]+=b[i];
else
{
    psbe[i]+=b[i];
}
}
for(int i=0;i<n;i++)
if(i%2==0)
v[0]+=(a[i]-b[i]);
else
v[0]-=(a[i]-b[i]);
for(int i=1;i<=m-n;i++)
{
v[i]=v[i-1]+b[i-1];
if(n%2==0)
v[i]+=b[i+n-1];
else
v[i]-=b[i+n-1];
if(i+n-2>=i)
{
    v[i]-=2*(psbo[i+n-2]-psbo[i-1]);
    v[i]+=2*(psbe[i+n-2]-psbe[i-1]);
}
}
sort(all(v));
auto it=lower_bound(all(v),0);
ll ans=INF;
if(it!=v.end())
ans=min(*it,ans);
if(it!=v.begin())
ans=min(ans,abs(*(--it)));
ll extra=0;
cout<<ans<<'\n';
while(q--)
{
    ll l,r,x;
    cin>>l>>r>>x;
    if((r-l+1)%2==0)
    {
    cout<<ans<<'\n';
    continue;
    }
    else
    {
       if(l&1)
       extra+=x;
       else
       extra-=x;
       ll t=INF;
       auto it=lower_bound(all(v),-extra);
       if(it!=v.end())
       {
        t=min(t,abs(*it+extra));
        ++it;
        if(it!=v.end())
        t=min(t,abs(extra+*it));
        it--;
       }
       if(it!=v.begin())
       {
           it--;
           t=min(t,abs(*it+extra));
       }
       //cout<<t<<'\n';
       ans=t;
    }
    cout<<ans<<'\n';
}
return ;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
    #endif
    clock_t start, end;
    start = clock();
    int test=1;
    //cin>>test;
    for(int i=1;i<=test;i++)
    solveforthiscase(i);
    end = clock();
    long double t_t = (long double)(end - start) /(long double)(CLOCKS_PER_SEC);
    cerr << (long double)t_t*1000<< " ms ";
}


#pragma GCC optimize ("Ofast")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define pb push_back
#define all(_obj) _obj.begin(),_obj.end()
#define F first
#define S second
#define INF 1e18
#define pll pair<ll, ll>
#define vll vector<ll>
#define deb(x) cerr<<#x<<' '<<'='<<' '<<x<<'\n'
ll n,m,a,b,c,k,temp,x,y;
const int MAXN=1e5+11;
inline ll max(ll a,ll b) {return ((a>b)?a:b);}
inline ll min(ll a,ll b) {return ((a>b)?b:a);}
inline ll gcd(ll a ,ll b) {if(b==0) return a;a%=b;return gcd(b,a);}
inline vll read(int n) {vll v(n);for (int i = 0; i < v.size(); i++)cin>>v[i];return v;}
void solveforthiscase(const int & test)
{
int q;
cin>>n>>m>>q;
vll a=read(n);
vll b=read(m);
vector<ll> v(m-n+1);
ll psbo[m];
ll psbe[m];
psbo[0]=0;
psbe[0]=b[0];
for(int i=1;i<m;i++)
{
psbo[i]=psbo[i-1],psbe[i]=psbe[i-1];
if(i&1)
psbo[i]+=b[i];
else
{
    psbe[i]+=b[i];
}
}
for(int i=0;i<n;i++)
if(i%2==0)
v[0]+=(a[i]-b[i]);
else
v[0]-=(a[i]-b[i]);
for(int i=1;i<=m-n;i++)
{
v[i]=v[i-1]+b[i-1];
if(n%2==0)
v[i]+=b[i+n-1];
else
v[i]-=b[i+n-1];
if(i+n-2>=i)
{
    if(i&1)
    {
    v[i]-=2*(psbo[i+n-2]-psbo[i-1]);
    v[i]+=2*(psbe[i+n-2]-psbe[i-1]);
    }
    else
    {
    v[i]+=2*(psbo[i+n-2]-psbo[i-1]);
    v[i]-=2*(psbe[i+n-2]-psbe[i-1]);
    }
}
}
sort(all(v));
auto it=lower_bound(all(v),0);
ll ans=INF;
if(it!=v.end())
ans=min(*it,ans);
if(it!=v.begin())
{
it--;
ans=min(ans,abs(*it));
}
ll extra=0;
cout<<ans<<'\n';
while(q--)
{
    ll l,r,x;
    cin>>l>>r>>x;
    if((r-l+1)%2==0)
    {
    cout<<ans<<'\n';
    continue;
    }
    else
    {
       if(l&1)
       extra+=x;
       else
       extra-=x;
       ll t=INF;
       auto it=lower_bound(all(v),-extra);
       if(it!=v.end())
       {
        t=min(t,abs(*it+extra));
        ++it;
        if(it!=v.end())
        t=min(t,abs(extra+*it));
        it--;
       }
       if(it!=v.begin())
       {
           it--;
           t=min(t,abs(*it+extra));
       }
       ans=t;
    }
    cout<<ans<<'\n';
}
return ;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
    #endif
    clock_t start, end;
    start = clock();
    int test=1;
    //cin>>test;
    for(int i=1;i<=test;i++)
    solveforthiscase(i);
    end = clock();
    long double t_t = (long double)(end - start) /(long double)(CLOCKS_PER_SEC);
    cerr << (long double)t_t*1000<< " ms ";
}


#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

int main() {
    int tests;
    scanf("%d", &tests);
    int incom[200004];
    int ribs[200004][4];
    vector<pair<int, int>> r;
    vector<int> e;
    vector<int> a;
    r.reserve(200004);
    e.reserve(200004);
    a.reserve(200004);
    for (int test = 0; test < tests; ++test) {
        int n, m;
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= n; ++i) {
            incom[i] = 0;
        }
        r.resize(0);
        e.resize(0);
        for (int i = 0; i < m; ++i) {
            scanf("%d%d%d", &ribs[i][0], &ribs[i][1], &ribs[i][2]);
            if (ribs[i][0] == 1) {
                incom[ribs[i][2]]++;
                r.push_back(make_pair(ribs[i][1], ribs[i][2]));
            }
        }
        sort(r.begin(), r.end());

        for (int i = 1; i <= n; ++i) {
            if (incom[i] == 0) {
                e.push_back(i);
            }
        }
        a.resize(n+1, 0);
        int pos = 0;
        while (e.size() > 0) {
            int v = e.back();
            a[v] = ++pos;
            e.pop_back();
            auto x = lower_bound(r.begin(), r.end(), make_pair(v, 0));
            for (auto i = x; i != r.end(); ++i) {
                if (i->first != v) {
                    break;
                }
                incom[i->second]--;
                if (incom[i->second] == 0) {
                    e.push_back(i->second);
                }
            }
        }

        bool answer = true;
        for (int i = 1; i <= n; ++i) {
            if (incom[i] > 0) {
                answer = false;
                break;
            }
            if (a[i] == 0) {
                a[i] = ++pos;
            }
        }
        if (answer) {
            printf("YES\n");
            for (int i = 0; i < m; ++i) {
                if (ribs[i][0] == 1) {
                    printf("%d %d\n", ribs[i][1], ribs[i][2]);
                }
                else {
                    if (a[ribs[i][1]] < a[ribs[i][2]]) {
                        printf("%d %d\n", ribs[i][1], ribs[i][2]);
                    }
                    else {
                        printf("%d %d\n", ribs[i][2], ribs[i][1]);
                    }
                }
            }
        }
        else {
            printf("NO\n");
        }
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <stdlib.h>
#include <fstream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <deque>
#include <iterator>
#include <iomanip>
#include <climits>
#include <numeric>
#include <cstring>
using namespace std;
#define ll long long int

vector<int> s;
vector<char> used;
vector<vector<int> > g;

void dfs(int v){
	used[v] = true;

	for(int to: g[v]){
		if(!used[to]) dfs(to);
	}

	s.push_back(v);
}

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

	int t;
	cin >> t;

	while(t--){
		int n, m;
		cin >> n >> m;
		g.clear();
		g.resize(n);
		vector<pair<int, int> > ed;

		int fr, to, f;

		for(int i = 0; i < m; ++i){
			cin >> f >> fr >> to;
			fr--, to--;

			if(f) g[fr].push_back(to);

			ed.push_back(make_pair(fr, to));
		}

		used.assign(n, false);
		s.clear();

		for(int i = 0; i < n; ++i){
			if(!used[i]) dfs(i);
		}

		reverse(s.begin(), s.end());
		vector<int> pos(n);

		for(int i = 0; i < n; ++i){
			pos[s[i]] = i;
		}

		bool bad = false;

		for(int i = 0; i < n; ++i){
			for(int j: g[i]){
				if(pos[i] > pos[j]) bad = true;
			}
		}

		if(bad){
			cout << "NO\n";
		}
		else{
			cout << "YES\n";

			for(pair<int, int> a: ed){
				int x = a.first, y = a.second;
				if(pos[x] < pos[y]) cout << x + 1 << " " << y + 1 << "\n";
				else cout << y + 1 << ' ' << x + 1 << "\n";
			}
		}
	}
}


#include <cstdio>
#include <vector>

using namespace std;

vector<int> order;
vector<char> cnt;
vector<vector<int>> e;
bool bad;

void dfs(int x)
{
	if (cnt[x])
		return;
	cnt[x] = 1;
	for (int i = 0; i < e[x].size(); ++i)
	{
		if (cnt[e[x][i]] == 1)
		{
			bad = true;
			return;
		}
		dfs(e[x][i]);
	}
	cnt[x] = 2;
	order.push_back(x);
}

void solve()
{
	int n, m;
	scanf("%d%d", &n, &m);
	e.resize(n);
	for (int i = 0; i < n; ++i)
		e[i].clear();
	vector<pair<int,int>> q;
	for (int i = 0; i < m; ++i)
	{
		int t, x, y;
		scanf("%d%d%d", &t, &x, &y);
		--x; --y;
		if (t == 0)
			q.push_back(pair<int,int>(x, y));
		else
			e[x].push_back(y);
	}
	bad = false;
	cnt.assign(n, 0);
	order.clear();
	for (int i = 0; !bad && i < n; ++i)
		dfs(i);
	if (bad)
		printf("NO\n");
	else
	{
		printf("YES\n");
		vector<int> order_idx(n);
		for (int i = 0; i < n; ++i)
			order_idx[order[i]] = i;
		for (int i = 0; i < q.size(); ++i)
		{
			int x, y;
			x = q[i].first;
			y = q[i].second;
			if (order_idx[x] > order_idx[y])
				e[x].push_back(y);
			else
				e[y].push_back(x);
		}
		for (int i = 0; i < n; ++i)
		{
			for (int j = 0; j < e[i].size(); ++j)
			{
				printf("%d %d\n", i + 1, e[i][j] + 1);
			}
		}
	}
}

int main()
{
	int T;
	scanf("%d", &T);
	for (int i = 0; i < T; ++i)
		solve();
	return 0;
}


#include <cstdio>
#include <vector>

using namespace std;

vector<int> order;
vector<char> cnt;
vector<vector<int>> e;
bool bad;

void dfs(int x)
{
	if (cnt[x])
		return;
	cnt[x] = 1;
	for (int i = 0; i < e[x].size(); ++i)
	{
		if (cnt[e[x][i]] == 1)
		{
			bad = true;
			return;
		}
		dfs(e[x][i]);
	}
	cnt[x] = 2;
	order.push_back(x);
}

void solve()
{
	int n, m;
	scanf("%d%d", &n, &m);
	e.resize(n);
	for (int i = 0; i < n; ++i)
		e[i].clear();
	vector<pair<int,int>> q;
	for (int i = 0; i < m; ++i)
	{
		int t, x, y;
		scanf("%d%d%d", &t, &x, &y);
		--x; --y;
		if (t == 0)
			q.push_back(pair<int,int>(x, y));
		else
			e[x].push_back(y);
	}
	bad = false;
	cnt.assign(n, 0);
	order.clear();
	for (int i = 0; !bad && i < n; ++i)
		dfs(i);
	if (bad)
		printf("NO\n");
	else
	{
		printf("YES\n");
		vector<int> order_idx(n);
		for (int i = 0; i < n; ++i)
			order_idx[order[i]] = i;
		for (int i = 0; i < q.size(); ++i)
		{
			int x, y;
			x = q[i].first;
			y = q[i].second;
			if (order[x] > order[y])
				e[y].push_back(x);
			else
				e[x].push_back(y);
		}
		for (int i = 0; i < n; ++i)
		{
			for (int j = 0; j < e[i].size(); ++j)
				printf("%d %d\n", i + 1, e[i][j] + 1);
		}
	}
}

int main()
{
	int T;
	scanf("%d", &T);
	for (int i = 0; i < T; ++i)
		solve();
	return 0;
}


from collections import defaultdict, deque
for _ in range(int(input())):
    n,m = map(int,input().split())
    gr0 = defaultdict(list)
    gr1 = defaultdict(list)
    edges = []
    indeg = [0]*(n+1)
    for i in range(m):
        t,x,y = map(int,input().split())
        if t==0:
            edges.append([x,y])
        else:
            gr0[x].append(y)
            indeg[y]+=1
    q = deque()
    # vis = {}
    for i in range(1,n+1):
        if indeg[i]==0:
            q.append(i)
            # vis[i] = 1
    cnt = 0
    top = []
    while q:
        s = q.popleft()
        top.append(s)
        for i in gr0[s]:
            indeg[i]-=1
            if indeg[i]==0:
                q.append(i)
        cnt+=1

    if cnt!=n:
        print("NO")
        continue
    mp = {}
    for i in range(n):
        mp[top[i]] = i
    for x,y in edges:
        if mp[x]<mp[y]:
            gr0[x].append(y)
        else:
            gr0[y].append(x)
    print("YES")
    for i in gr0.keys():
        for j in gr0[i]:
            print(i,j)


# from math import factorial as fac
from collections import defaultdict
# from copy import deepcopy
import sys, math
f = None
try:
	f = open('q1.input', 'r')
except IOError:
	f = sys.stdin
if 'xrange' in dir(__builtins__):
	range = xrange
# print(f.readline())
sys.setrecursionlimit(10**6)

def print_case_iterable(case_num, iterable):
	print("Case #{}: {}".format(case_num," ".join(map(str,iterable))))

def print_case_number(case_num, iterable):
	print("Case #{}: {}".format(case_num,iterable))

def print_iterable(A):
	print (' '.join(A))

def read_int():
	return int(f.readline().strip())
def read_int_array():
	return [int(x) for x in f.readline().strip().split(" ")]
def rns():
	a =  [x for x in f.readline().split(" ")]
	return int(a[0]), a[1].strip()
def read_string():
	return list(f.readline().strip())
def ri():
	return int(f.readline().strip())
def ria():
	return [int(x) for x in f.readline().strip().split(" ")]
def rns():
	a =  [x for x in f.readline().split(" ")]
	return int(a[0]), a[1].strip()
def rs():
	return list(f.readline().strip())
def bi(x):
	return bin(x)[2:]

from collections import deque
import math
NUMBER = 10**9 + 7
# NUMBER = 998244353
def factorial(n) :
	M = NUMBER
	f = 1

	for i in range(1, n + 1):
		f = (f * i) % M # Now f never can
						# exceed 10^9+7
	return f
def mult(a,b):
	return (a * b) % NUMBER

def minus(a , b):
	return (a - b) % NUMBER

def plus(a , b):
	return (a + b) % NUMBER

def egcd(a, b):
	if a == 0:
		return (b, 0, 1)
	else:
		g, y, x = egcd(b % a, a)
		return (g, x - (b // a) * y, y)

def modinv(a):
	m = NUMBER
	g, x, y = egcd(a, m)
	if g != 1:
		raise Exception('modular inverse does not exist')
	else:
		return x % m
def choose(n,k):
	if n < k:
		assert false
	return  mult(factorial(n), modinv(mult(factorial(k),factorial(n-k)))) % NUMBER
from collections import deque, defaultdict
import heapq

from types import GeneratorType
def bootstrap(f, stack=[]):
	def wrappedfunc(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		else:
			to = f(*args, **kwargs)
			while True:
				if type(to) is GeneratorType:
					stack.append(to)
					to = next(to)
				else:
					stack.pop()
					if not stack:
						break
					to = stack[-1].send(to)
			return to

	return wrappedfunc

def dfs(g, timeIn, timeOut,depths,parents):
	# assign In-time to node u
	cnt = 0
	# node, neig_i, parent, depth
	stack = [[1,0,0,0]]
	while stack:
		v,neig_i,parent,depth = stack[-1]
		parents[v] = parent
		depths[v] = depth
		# print (v)
		if neig_i == 0:
			timeIn[v] = cnt
			cnt+=1
		while neig_i < len(g[v]):
			u = g[v][neig_i]
			if u == parent:
				neig_i+=1
				continue
			stack[-1][1] = neig_i + 1
			stack.append([u,0,v,depth+1])
			break
		if neig_i == len(g[v]):
			stack.pop()
			timeOut[v] = cnt
			cnt += 1

# def isAncestor(u: int, v: int, timeIn: list, timeOut: list) -> str:
# 	return timeIn[u] <= timeIn[v] and timeOut[v] <= timeOut[u]

cnt = 0
@bootstrap
def dfs(v,adj,timeIn, timeOut,depths,parents,parent=0,depth=0):
	global cnt
	parents[v] = parent
	depths[v] = depth
	timeIn[v] = cnt
	cnt+=1
	for u in adj[v]:
		if u == parent:
			continue
		yield dfs(u,adj,timeIn,timeOut,depths,parents,v,depth+1)
	timeOut[v] = cnt
	cnt+=1
	yield

def gcd(a,b):
	if a == 0:
		return b
	return gcd(b % a, a)

# Function to return LCM of two numbers
def lcm(a,b):
	return (a*b) / gcd(a,b)
def get_num_2_5(n):
	twos = 0
	fives = 0
	while n>0 and n%2 == 0:
		n//=2
		twos+=1
	while n>0 and n%5 == 0:
		n//=5
		fives+=1
	return (twos,fives)
def shift(a,i,num):
	for _ in range(num):
		a[i],a[i+1],a[i+2] = a[i+2],a[i],a[i+1]

def equal(x,y):
	return abs(x-y) <= 1e-9
# def leq(x,y):
# 	return x-y <= 1e-9
def getAngle(a, b, c):
	ang = math.degrees(math.atan2(c[1]-b[1], c[0]-b[0]) - math.atan2(a[1]-b[1], a[0]-b[0]))
	return ang + 360 if ang < 0 else ang
def getLength(a,b):
	return math.sqrt((a[0]-b[0])**2 + (a[1]-b[1])**2)

from heapq import heapify, heappush, heappop

def bfs(adj):
	s = set()
	d = defaultdict(set)
	# print('\n'.join(map(str,E.items())))
	q = deque([])
	s.add(0)
	d[0].add(0)
	q.append((0,0))
	while len(q) > 0:
		v,depth = q.popleft()
		for u in adj[v]:
			if u not in s:
				s.add(u)
				d[depth+1].add(u)
				q.append((u,depth+1))
		# print(q)
	return d

def dfs(u,top, adj,vis):
	vis[u] = 1
	for v,d in adj[u]:
		if vis[v]==2 or d==0:
			continue
		if vis[v] == 1:
			return "No"
		if dfs(v,top,adj,vis) == "No":
			return "No"
	vis[u] = 2
	top.append(u)

def solution(adj,n,m):
	top = []
	vis = [0] * n
	for v in range(n):
		if vis[v] == 0:
			if dfs(v,top,adj,vis) == "No":
				return "No"
	top = top[::-1]
	done = set()
	ans = []
	for v in top:
		for u,d in adj[v]:
			if (min(u,v),max(u,v)) in done:
				continue
			ans.append(f'{v+1} {u+1}')
			done.add((min(u,v),max(u,v)))
	print("YES")
	return '\n'.join(ans)

def main():
	T = 1
	T = ri()
	for i in range(T):
		# n = ri()
		# s = rs()
		n,m	= ria()
		# a = ria()
		adj = [list() for _ in range(n)]
		for _ in range(m):
			d,a,b=ria();
			a-=1; b-=1
			adj[a].append((b,d))
			if d == 0:
				adj[b].append((a,d))

		x = solution(adj,n,m)

		# continue
		if 'xrange' not in dir(__builtins__):
			print(x)
		else:
			print >>output,str(x)# "Case #"+str(i+1)+':',
	if 'xrange' in dir(__builtins__):
		print(output.getvalue())
		output.close()

if 'xrange' in dir(__builtins__):
	import cStringIO
	output = cStringIO.StringIO()
#example usage:
#    for l in res:
#       print >>output, str(len(l)) + ' ' +  ' '.join(l)

if __name__ == '__main__':
	main()


# from math import factorial as fac
from collections import defaultdict
# from copy import deepcopy
import sys, math
f = None
try:
	f = open('q1.input', 'r')
except IOError:
	f = sys.stdin
if 'xrange' in dir(__builtins__):
	range = xrange
# print(f.readline())
sys.setrecursionlimit(10**6)

def print_case_iterable(case_num, iterable):
	print("Case #{}: {}".format(case_num," ".join(map(str,iterable))))

def print_case_number(case_num, iterable):
	print("Case #{}: {}".format(case_num,iterable))

def print_iterable(A):
	print (' '.join(A))

def read_int():
	return int(f.readline().strip())
def read_int_array():
	return [int(x) for x in f.readline().strip().split(" ")]
def rns():
	a =  [x for x in f.readline().split(" ")]
	return int(a[0]), a[1].strip()
def read_string():
	return list(f.readline().strip())
def ri():
	return int(f.readline().strip())
def ria():
	return [int(x) for x in f.readline().strip().split(" ")]
def rns():
	a =  [x for x in f.readline().split(" ")]
	return int(a[0]), a[1].strip()
def rs():
	return list(f.readline().strip())
def bi(x):
	return bin(x)[2:]

from collections import deque
import math
NUMBER = 10**9 + 7
# NUMBER = 998244353
def factorial(n) :
	M = NUMBER
	f = 1

	for i in range(1, n + 1):
		f = (f * i) % M # Now f never can
						# exceed 10^9+7
	return f
def mult(a,b):
	return (a * b) % NUMBER

def minus(a , b):
	return (a - b) % NUMBER

def plus(a , b):
	return (a + b) % NUMBER

def egcd(a, b):
	if a == 0:
		return (b, 0, 1)
	else:
		g, y, x = egcd(b % a, a)
		return (g, x - (b // a) * y, y)

def modinv(a):
	m = NUMBER
	g, x, y = egcd(a, m)
	if g != 1:
		raise Exception('modular inverse does not exist')
	else:
		return x % m
def choose(n,k):
	if n < k:
		assert false
	return  mult(factorial(n), modinv(mult(factorial(k),factorial(n-k)))) % NUMBER
from collections import deque, defaultdict
import heapq

from types import GeneratorType
def bootstrap(f, stack=[]):
	def wrappedfunc(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		else:
			to = f(*args, **kwargs)
			while True:
				if type(to) is GeneratorType:
					stack.append(to)
					to = next(to)
				else:
					stack.pop()
					if not stack:
						break
					to = stack[-1].send(to)
			return to

	return wrappedfunc

def dfs(g, timeIn, timeOut,depths,parents):
	# assign In-time to node u
	cnt = 0
	# node, neig_i, parent, depth
	stack = [[1,0,0,0]]
	while stack:
		v,neig_i,parent,depth = stack[-1]
		parents[v] = parent
		depths[v] = depth
		# print (v)
		if neig_i == 0:
			timeIn[v] = cnt
			cnt+=1
		while neig_i < len(g[v]):
			u = g[v][neig_i]
			if u == parent:
				neig_i+=1
				continue
			stack[-1][1] = neig_i + 1
			stack.append([u,0,v,depth+1])
			break
		if neig_i == len(g[v]):
			stack.pop()
			timeOut[v] = cnt
			cnt += 1

# def isAncestor(u: int, v: int, timeIn: list, timeOut: list) -> str:
# 	return timeIn[u] <= timeIn[v] and timeOut[v] <= timeOut[u]

cnt = 0
@bootstrap
def dfs(v,adj,timeIn, timeOut,depths,parents,parent=0,depth=0):
	global cnt
	parents[v] = parent
	depths[v] = depth
	timeIn[v] = cnt
	cnt+=1
	for u in adj[v]:
		if u == parent:
			continue
		yield dfs(u,adj,timeIn,timeOut,depths,parents,v,depth+1)
	timeOut[v] = cnt
	cnt+=1
	yield

def gcd(a,b):
	if a == 0:
		return b
	return gcd(b % a, a)

# Function to return LCM of two numbers
def lcm(a,b):
	return (a*b) / gcd(a,b)
def get_num_2_5(n):
	twos = 0
	fives = 0
	while n>0 and n%2 == 0:
		n//=2
		twos+=1
	while n>0 and n%5 == 0:
		n//=5
		fives+=1
	return (twos,fives)
def shift(a,i,num):
	for _ in range(num):
		a[i],a[i+1],a[i+2] = a[i+2],a[i],a[i+1]

def equal(x,y):
	return abs(x-y) <= 1e-9
# def leq(x,y):
# 	return x-y <= 1e-9
def getAngle(a, b, c):
	ang = math.degrees(math.atan2(c[1]-b[1], c[0]-b[0]) - math.atan2(a[1]-b[1], a[0]-b[0]))
	return ang + 360 if ang < 0 else ang
def getLength(a,b):
	return math.sqrt((a[0]-b[0])**2 + (a[1]-b[1])**2)

from heapq import heapify, heappush, heappop

def bfs(adj):
	s = set()
	d = defaultdict(set)
	# print('\n'.join(map(str,E.items())))
	q = deque([])
	s.add(0)
	d[0].add(0)
	q.append((0,0))
	while len(q) > 0:
		v,depth = q.popleft()
		for u in adj[v]:
			if u not in s:
				s.add(u)
				d[depth+1].add(u)
				q.append((u,depth+1))
		# print(q)
	return d

@bootstrap
def dfs(u,top, adj,vis):
	vis[u] = 1
	for v,d in adj[u]:
		if vis[v]==2 or d==0:
			continue
		if vis[v] == 1:
			return "No"
		x = yield dfs(v,top,adj,vis)
		if x == "No":
			return "No"
	vis[u] = 2
	top.append(u)
	yield

def solution(adj,n,m):
	top = []
	vis = [0] * n
	for v in range(n):
		if vis[v] == 0:
			if dfs(v,top,adj,vis) == "No":
				return "No"
	top = top[::-1]
	done = set()
	ans = []
	for v in top:
		for u,d in adj[v]:
			if (min(u,v),max(u,v)) in done:
				continue
			ans.append(f'{v+1} {u+1}')
			done.add((min(u,v),max(u,v)))
	print("YES")
	return '\n'.join(ans)

def main():
	T = 1
	T = ri()
	for i in range(T):
		# n = ri()
		# s = rs()
		n,m	= ria()
		# a = ria()
		adj = [list() for _ in range(n)]
		for _ in range(m):
			d,a,b=ria();
			a-=1; b-=1
			adj[a].append((b,d))
			if d == 0:
				adj[b].append((a,d))

		x = solution(adj,n,m)

		# continue
		if 'xrange' not in dir(__builtins__):
			print(x)
		else:
			print >>output,str(x)# "Case #"+str(i+1)+':',
	if 'xrange' in dir(__builtins__):
		print(output.getvalue())
		output.close()

if 'xrange' in dir(__builtins__):
	import cStringIO
	output = cStringIO.StringIO()
#example usage:
#    for l in res:
#       print >>output, str(len(l)) + ' ' +  ' '.join(l)

if __name__ == '__main__':
	main()


# from math import factorial as fac
from collections import defaultdict
# from copy import deepcopy
import sys, math
f = None
try:
	f = open('q1.input', 'r')
except IOError:
	f = sys.stdin
if 'xrange' in dir(__builtins__):
	range = xrange
# print(f.readline())
# sys.setrecursionlimit(10**6)

def print_case_iterable(case_num, iterable):
	print("Case #{}: {}".format(case_num," ".join(map(str,iterable))))

def print_case_number(case_num, iterable):
	print("Case #{}: {}".format(case_num,iterable))

def print_iterable(A):
	print (' '.join(A))

def read_int():
	return int(f.readline().strip())
def read_int_array():
	return [int(x) for x in f.readline().strip().split(" ")]
def rns():
	a =  [x for x in f.readline().split(" ")]
	return int(a[0]), a[1].strip()
def read_string():
	return list(f.readline().strip())
def ri():
	return int(f.readline().strip())
def ria():
	return [int(x) for x in f.readline().strip().split(" ")]
def rns():
	a =  [x for x in f.readline().split(" ")]
	return int(a[0]), a[1].strip()
def rs():
	return list(f.readline().strip())
def bi(x):
	return bin(x)[2:]

from collections import deque
import math
NUMBER = 10**9 + 7
# NUMBER = 998244353
def factorial(n) :
	M = NUMBER
	f = 1

	for i in range(1, n + 1):
		f = (f * i) % M # Now f never can
						# exceed 10^9+7
	return f
def mult(a,b):
	return (a * b) % NUMBER

def minus(a , b):
	return (a - b) % NUMBER

def plus(a , b):
	return (a + b) % NUMBER

def egcd(a, b):
	if a == 0:
		return (b, 0, 1)
	else:
		g, y, x = egcd(b % a, a)
		return (g, x - (b // a) * y, y)

def modinv(a):
	m = NUMBER
	g, x, y = egcd(a, m)
	if g != 1:
		raise Exception('modular inverse does not exist')
	else:
		return x % m
def choose(n,k):
	if n < k:
		assert false
	return  mult(factorial(n), modinv(mult(factorial(k),factorial(n-k)))) % NUMBER
from collections import deque, defaultdict
import heapq

from types import GeneratorType
def bootstrap(f, stack=[]):
	def wrappedfunc(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		else:
			to = f(*args, **kwargs)
			while True:
				if type(to) is GeneratorType:
					stack.append(to)
					to = next(to)
				else:
					stack.pop()
					if not stack:
						break
					to = stack[-1].send(to)
			return to

	return wrappedfunc

def dfs(g, timeIn, timeOut,depths,parents):
	# assign In-time to node u
	cnt = 0
	# node, neig_i, parent, depth
	stack = [[1,0,0,0]]
	while stack:
		v,neig_i,parent,depth = stack[-1]
		parents[v] = parent
		depths[v] = depth
		# print (v)
		if neig_i == 0:
			timeIn[v] = cnt
			cnt+=1
		while neig_i < len(g[v]):
			u = g[v][neig_i]
			if u == parent:
				neig_i+=1
				continue
			stack[-1][1] = neig_i + 1
			stack.append([u,0,v,depth+1])
			break
		if neig_i == len(g[v]):
			stack.pop()
			timeOut[v] = cnt
			cnt += 1

# def isAncestor(u: int, v: int, timeIn: list, timeOut: list) -> str:
# 	return timeIn[u] <= timeIn[v] and timeOut[v] <= timeOut[u]

cnt = 0
@bootstrap
def dfs(v,adj,timeIn, timeOut,depths,parents,parent=0,depth=0):
	global cnt
	parents[v] = parent
	depths[v] = depth
	timeIn[v] = cnt
	cnt+=1
	for u in adj[v]:
		if u == parent:
			continue
		yield dfs(u,adj,timeIn,timeOut,depths,parents,v,depth+1)
	timeOut[v] = cnt
	cnt+=1
	yield

def gcd(a,b):
	if a == 0:
		return b
	return gcd(b % a, a)

# Function to return LCM of two numbers
def lcm(a,b):
	return (a*b) / gcd(a,b)
def get_num_2_5(n):
	twos = 0
	fives = 0
	while n>0 and n%2 == 0:
		n//=2
		twos+=1
	while n>0 and n%5 == 0:
		n//=5
		fives+=1
	return (twos,fives)
def shift(a,i,num):
	for _ in range(num):
		a[i],a[i+1],a[i+2] = a[i+2],a[i],a[i+1]

def equal(x,y):
	return abs(x-y) <= 1e-9
# def leq(x,y):
# 	return x-y <= 1e-9
def getAngle(a, b, c):
	ang = math.degrees(math.atan2(c[1]-b[1], c[0]-b[0]) - math.atan2(a[1]-b[1], a[0]-b[0]))
	return ang + 360 if ang < 0 else ang
def getLength(a,b):
	return math.sqrt((a[0]-b[0])**2 + (a[1]-b[1])**2)

from heapq import heapify, heappush, heappop

def bfs(adj):
	s = set()
	d = defaultdict(set)
	# print('\n'.join(map(str,E.items())))
	q = deque([])
	s.add(0)
	d[0].add(0)
	q.append((0,0))
	while len(q) > 0:
		v,depth = q.popleft()
		for u in adj[v]:
			if u not in s:
				s.add(u)
				d[depth+1].add(u)
				q.append((u,depth+1))
		# print(q)
	return d

@bootstrap
def dfs(u,top, adj,vis):
	vis[u] = 1
	for v,d in adj[u]:
		if vis[v]==2 or d==0:
			continue
		if vis[v] == 1:
			yield "No"
		x = yield dfs(v,top,adj,vis)
		if x == "No":
			yield "No"
	vis[u] = 2
	top.append(u)
	yield

def solution(adj,n,m):
	top = []
	vis = [0] * n
	for v in range(n):
		if vis[v] == 0:
			if dfs(v,top,adj,vis) == "No":
				return "No"
	top = top[::-1]
	done = set()
	ans = []
	for v in top:
		for u,d in adj[v]:
			if (min(u,v),max(u,v)) in done:
				continue
			ans.append(f'{v+1} {u+1}')
			done.add((min(u,v),max(u,v)))
	print("YES")
	return '\n'.join(ans)

def main():
	T = 1
	T = ri()
	for i in range(T):
		# n = ri()
		# s = rs()
		n,m	= ria()
		# a = ria()
		adj = [list() for _ in range(n)]
		for _ in range(m):
			d,a,b=ria();
			a-=1; b-=1
			adj[a].append((b,d))
			if d == 0:
				adj[b].append((a,d))

		x = solution(adj,n,m)

		# continue
		if 'xrange' not in dir(__builtins__):
			print(x)
		else:
			print >>output,str(x)# "Case #"+str(i+1)+':',
	if 'xrange' in dir(__builtins__):
		print(output.getvalue())
		output.close()

if 'xrange' in dir(__builtins__):
	import cStringIO
	output = cStringIO.StringIO()
#example usage:
#    for l in res:
#       print >>output, str(len(l)) + ' ' +  ' '.join(l)

if __name__ == '__main__':
	main()


t=int(input())
Maxn=210000
while(t>0):
	t-=1
	n,m=map(int,input().split())
	to=[[]]
	for i in range(0,n):
		to.append([])
	d=[0]*(n+1)
	ran=[0]*(n+1)
	q=[]
	cnt=0
	to[0].append(1)
	for i in range(0,m):
		bj,u,v=map(int,input().split())
		if(bj):
			(to[u]).append(v)
			d[v]+=1
			q.append((u,v))
		else:
			q.append((u,v))
	que=[]
	for i in range(1,n+1):
		if(d[i]==0): que.append(i)
	while(len(que)):
		now=que.pop()
		cnt+=1
		ran[now]=cnt
		for i in to[now]:
			d[i]-=1
			if(d[i]==0):
				que.append(i)
	if(cnt!=n):
		print("NO")
		continue
	print("YES")
	for i in q:
		if(ran[i[0]]<ran[i[1]]):
			print(i[0],i[1])
		else :
			print(i[1],i[0])


t=int(input())
Maxn=210000
while(t>0):
	t-=1
	n,m=map(int,input().split())
	to=[[]]*(n+1)
	d=[0]*(n+1)
	ran=[0]*(n+1)
	q=[]
	cnt=0
	for i in range(0,m):
		bj,u,v=map(int,input().split())
		if(bj):
			to[u].append(v)
			d[v]+=1
			q.append((u,v))
		else:
			q.append((u,v))
	que=[]
	for i in range(1,n+1):
		if(d[i]==0): que.append(i)
	while(len(que)):
		now=que.pop()
		cnt+=1
		ran[now]=cnt
		for i in to[now]:
			d[i]-=1
			if(d[i]==0):
				que.append(i)
	if(cnt!=n):
		print("NO")
		continue
	print("YES")
	for i in q:
		if(ran[i[0]]<ran[i[1]]):
			print(i[0],i[1])
		else :
			print(i[1],i[0])


t=int(input())
Maxn=210000
while(t>0):
	t-=1
	n,m=map(int,input().split())
	to=[[]]
	for i in range(0,n):
		to.append([])
	d=[0]*(n+1)
	ran=[0]*(n+1)
	q=[]
	cnt=0
	to[0].append(1)
	for i in range(0,m):
		bj,u,v=map(int,input().split())
		if(bj):
			(to[u]).append(v)
			d[v]+=1
			q.append((u,v))
		else:
			q.append((u,v))
	que=[]
	for i in range(1,n+1):
		if(d[i]==0): que.append(i)
	while(len(que)):
		for i in range(1,n+1):
			print(d[i],end=' ')
		now=que.pop()
		print(now)
		cnt+=1
		ran[now]=cnt
		for i in to[now]:
			d[i]-=1
			if(d[i]==0):
				que.append(i)
	if(cnt!=n):
		print("NO")
		continue
	print("YES")
	for i in q:
		if(ran[i[0]]<ran[i[1]]):
			print(i[0],i[1])
		else :
			print(i[1],i[0])


t=int(input())
Maxn=210000
while(t>0):
	t-=1
	n,m=map(int,input().split())
	to=[[]]
	for i in range(0,n):
		to.append([])
	d=[0]*(n+1)
	ran=[0]*(n+1)
	q=[]
	cnt=0
	to[0].append(1)
	for i in range(0,m):
		bj,u,v=map(int,input().split())
		if(bj):
			(to[u]).append(v)
			d[v]+=1
			q.append((u,v))
		else:
			q.append((u,v))
	que=[]
	for i in range(1,n+1):
		if(d[i]==0): que.append(i)
	while(len(que)):
		now=que.pop()
		print(now)
		cnt+=1
		ran[now]=cnt
		for i in to[now]:
			d[i]-=1
			if(d[i]==0):
				que.append(i)
	if(cnt!=n):
		print("NO")
		continue
	print("YES")
	for i in q:
		if(ran[i[0]]<ran[i[1]]):
			print(i[0],i[1])
		else :
			print(i[1],i[0])


# -*- coding: utf-8 -*-
"""
Created on Tue Jul 28 09:12:35 2020

@author: Harshal
"""

import collections
test=int(input())
for _ in range(test):
    n,m=map(int,input().split())
    res=[]
    gress=[0]*(n+1)
    dn=collections.defaultdict(list)

    for _ in range(m):
        d,x,y=map(int,input().split())
        if d==1:
            dn[x].append(y)
            gress[y]+=1
        res.append([d,x,y])
    zp=[i for i in range(1,n+1) if gress[i]==0]
    count=0
    p=[0]*(n+1)

    while zp:
        u=zp.pop()
        count+=1
        p[u]=count
        for v in dn[u]:
            gress[v]-=1
            if gress[v]==0:
                zp.append(v)
    if count==n:
        print("YES")
        for d,x,y in res:
            if d==1:
                print(x,y)
            elif p[x]<p[y]:
                print(x,y)
            else:
                print(y,x)
    else:
        print("NO")


from collections import deque
import sys
sys.setrecursionlimit(10**7)

def solve():
    n, m = map(int, input().split())
    G = [[]for i in range(n+1)]
    edge = []
    for i in range(m):
        t, x, y = map(int, input().split())
        if t == 1:
            G[x].append(y)
        edge.append((t, x, y))

    visited = [False]*(n+1)
    order = [-1]*(n+1)
    que = deque([])

    def bfs(s):
        if visited[s] == False:
            visited[s] = True
            for node in G[s]:
                if visited[node] == False:
                    bfs(node)
            que.appendleft(s)
        else:
            pass

    for i in range(1, n+1):
        bfs(i)

    for i, v in enumerate(que):
        order[v] = i + 1

    not_dag = False

    ans = []
    for t, x, y in edge:
        if t == 1 and order[x] > order[y]:
            not_dag = True
            break
        else:
            if t == 1:
                ans.append((x, y))
            else:
                if order[x] > order[y]:
                    ans.append((y, x))
                else:
                    ans.append((x, y))

    if not_dag:
        print("NO")
    else:
        print("YES")
        for x, y in ans:
            print(x, y)

def main():
    t = int(input())
    for i in range(t):
        solve()
    return

if __name__ == "__main__":
    main()