#include<bits/stdc++.h>
#define endl '\n'
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define lg2(x) __lg(x)
#define rem_dupl(x) sort(all(x)); x.erase(unique(all(x)), x.end())
using namespace std;

typedef long long ll;
typedef long double db;
typedef pair<ll,ll> ii;
#define x first
#define y second

const int maxn = 2005;
ll l[maxn], r[maxn], a[maxn];
ll sa[maxn];

vector<ii> dp[maxn]; /// dp[n][k]: min cost of ending at the end of the n-th wave with k bullets left

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cout.setf(ios::fixed); cout.precision(0);

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

    l[n+1] = r[n] + 1;
    r[n+1] = l[n+1];
    a[n+1] = 0;

    for(int i = n; i >= 0; i--)
        sa[i] = sa[i+1] + a[i];

    dp[0] = {{k, 0}};
    for(int i = 0; i < n; i++){
        for(auto [j, val]:dp[i]){

            int x = a[i+1] % k;
            if(x == 0)
                x = k;

            ll reloads = (a[i+1] + k - 1)/k;

            if(j >= x){
                j -= x;
                x = 0;
                reloads--;

                if(reloads > r[i+1] - l[i+1])
                    continue;

                dp[i+1].push_back({j, val});

                if(reloads < r[i+1] - l[i+1] or (i+2 <= n and l[i+2] > r[i+1]))
                    dp[i+1].push_back({k, val + j});

                continue;

            }
            if(j < x) {

                x -= j;
                j = 0;

                if(reloads > r[i+1] - l[i+1])
                    continue;

                dp[i+1].push_back({k - x, val});

                if(reloads < r[i+1] - l[i+1] or (i+2 <= n and l[i+2] > r[i+1]) )
                    dp[i+1].push_back({k, val + k - x});
            }
        }
        rem_dupl(dp[i+1]);
        dp[i].clear();
        dp[i].shrink_to_fit();
    }

    if(dp[n].empty()) cout << -1;
    else{
        ll ans = LLONG_MAX;
        for(auto i:dp[n])
            ans = min(ans, i.y);
        for(int i = 1; i <= n; i++)
            ans += a[i];
        cout << ans;
    }

    return 0;
}


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

const long long inf = 0x3f3f3f3f3f3f3f3fLL;
int n, k;
struct hh
{
    int l, r, a;
} a[2010];
long long sum[2010], f[2010], d[2010][2010];
bool tf[2010][2010];

int main()
{
    scanf("%d%d", &n, &k);
    for (int i = 1; i <= n; i++)
    {
        scanf("%d%d%d", &a[i].l, &a[i].r, &a[i].a);
        sum[i] = sum[i - 1] + a[i].a;
    }
    memset(f, 63, sizeof(f));
    memset(d, -1, sizeof(d));
    for (int i = 1; i <= n; i++)
    {
        int now = k;
        if (i == 1) f[i] = 0;
        // printf("%lld\n", f[i]);
        for (int j = i; j <= n; j++)
        {
            int hh = a[j].l;
            if (a[j].a <= now)
            {
                now -= a[j].a;
            }
            else
            {
                int oo = a[j].a - now;
                hh += oo / k + (oo % k > 0);
                now = (k - oo % k) % k;
            }
            if (hh > a[j].r) break;
            else if (hh < a[j].r || a[j].r < a[j + 1].r) tf[i][j] = true;
            d[i][j] = now;
            if (tf[i][j])
                f[j + 1] = min(f[j + 1], f[i] + sum[j] - sum[i - 1] + now);
            else if (j == n)
                f[j + 1] = min(f[j + 1], f[i] + sum[j] - sum[i - 1]);
        }
    }
    // printf("%lld\n", d[2][2]);
    long long ans = f[n + 1] == inf ? -1 : f[n + 1];
    if (~d[1][n]) ans = min(ans, sum[n]);
    printf("%lld\n", ans);
}


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

const long long inf = 0x3f3f3f3f3f3f3f3fLL;
int n, k;
struct hh
{
    int l, r, a;
} a[2010];
long long sum[2010], f[2010], d[2010][2010];
bool tf[2010][2010];

int main()
{
    scanf("%d%d", &n, &k);
    for (int i = 1; i <= n; i++)
    {
        scanf("%d%d%d", &a[i].l, &a[i].r, &a[i].a);
        sum[i] = sum[i - 1] + a[i].a;
    }
    memset(f, 63, sizeof(f));
    memset(d, -1, sizeof(d));
    for (int i = 1; i <= n; i++)
    {
        int now = k;
        if (i == 1) f[i] = 0;
        // printf("%lld\n", f[i]);
        for (int j = i; j <= n; j++)
        {
            int hh = a[j].l;
            if (a[j].a <= now)
            {
                now -= a[j].a;
            }
            else
            {
                int oo = a[j].a - now;
                hh += oo / k + (oo % k > 0);
                now = (k - oo % k) % k;
            }
            if (hh > a[j].r) break;
            else if (hh < a[j].r || a[j].r < a[j + 1].l) tf[i][j] = true;
            d[i][j] = now;
            if (tf[i][j])
                f[j + 1] = min(f[j + 1], f[i] + sum[j] - sum[i - 1] + now);
            else if (j == n)
                f[j + 1] = min(f[j + 1], f[i] + sum[j] - sum[i - 1]);
        }
    }
    // printf("%lld\n", d[2][2]);
    long long ans = f[n + 1] == inf ? -1 : f[n + 1];
    if (~d[1][n]) ans = min(ans, sum[n]);
    printf("%lld\n", ans);
}


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

const long long inf = 0x3f3f3f3f3f3f3f3fLL;
int n, k;
struct hh
{
    int l, r, a;
} a[2010];
long long sum[2010], f[2010], d[2010][2010];
bool tf[2010][2010];

int main()
{
    scanf("%d%d", &n, &k);
    for (int i = 1; i <= n; i++)
    {
        scanf("%d%d%d", &a[i].l, &a[i].r, &a[i].a);
        sum[i] = sum[i - 1] + a[i].a;
    }
    memset(f, 63, sizeof(f));
    memset(d, -1, sizeof(d));
    for (int i = 1; i <= n; i++)
    {
        int now = k;
        if (i == 1) f[i] = 0;
        // printf("%lld\n", f[i]);
        for (int j = i; j <= n; j++)
        {
            int hh = a[j].l;
            if (a[j].a <= now)
            {
                now -= a[j].a;
            }
            else
            {
                int oo = a[j].a - now;
                hh += oo / k + (oo % k > 0);
                now = (k - oo % k) % k;
            }
            if (hh > a[j].r) break;
            else if (hh < a[j].r || a[j].r < a[j + 1].l) tf[i][j] = true;
            d[i][j] = now;
            if (j == n)
                f[j + 1] = min(f[j + 1], f[i] + sum[j] - sum[i - 1]);
            else if (tf[i][j])
                f[j + 1] = min(f[j + 1], f[i] + sum[j] - sum[i - 1] + now);
            // printf("%d %d %d %lld\n", i, j, now, f[i] + sum[j] - sum[i - 1]);
        }
    }
    // printf("%lld\n", d[2][2]);
    long long ans = (f[n + 1] == inf ? -1 : f[n + 1]);
    if (~d[1][n]) ans = min(ans, sum[n]);
    printf("%lld\n", ans);
}


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

const long long inf = 0x3f3f3f3f3f3f3f3fLL;
int n, k;
struct hh
{
    int l, r, a;
} a[2010];
long long sum[2010], f[2010], d[2010][2010];
bool tf[2010][2010];

int main()
{
    scanf("%d%d", &n, &k);
    for (int i = 1; i <= n; i++)
    {
        scanf("%d%d%d", &a[i].l, &a[i].r, &a[i].a);
        sum[i] = sum[i - 1] + a[i].a;
    }
    memset(f, 63, sizeof(f));
    memset(d, -1, sizeof(d));
    for (int i = 1; i <= n; i++)
    {
        int now = k;
        if (i == 1) f[i] = 0;
        // printf("%lld\n", f[i]);
        for (int j = i; j <= n; j++)
        {
            int hh = a[j].l;
            if (a[j].a <= now)
            {
                now -= a[j].a;
            }
            else
            {
                int oo = a[j].a - now;
                hh += oo / k + (oo % k > 0);
                now = (k - oo % k) % k;
            }
            if (hh > a[j].r) break;
            else if (hh < a[j].r) tf[i][j] = true;
            d[i][j] = now;
            if (j == n || tf[i][j])
                f[j + 1] = min(f[j + 1], f[i] + sum[j] - sum[i - 1] + now);
        }
    }
    // printf("%lld\n", d[2][2]);
    long long ans = f[n + 1] == inf ? -1 : f[n + 1];
    if (~d[1][n]) ans = min(ans, sum[n]);
    printf("%lld\n", ans);
}


// Author -- xyr2005

#include<bits/stdc++.h>

#define lowbit(x) ((x)&(-(x)))
#define DEBUG fprintf(stderr,"Running on Line %d in Function %s\n",__LINE__,__FUNCTION__)
#define SZ(x) ((int)x.size())
#define mkpr std::make_pair
#define pb push_back

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef std::pair<int,int> pi;
typedef std::pair<ll,ll> pl;
using std::min;
using std::max;

const int inf=0x3f3f3f3f,Inf=0x7fffffff;
const ll INF=0x3f3f3f3f3f3f3f3f;

std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count());
template <typename _Tp>_Tp gcd(const _Tp &a,const _Tp &b){return (!b)?a:gcd(b,a%b);}
template <typename _Tp>inline _Tp abs(const _Tp &a){return a>=0?a:-a;}
template <typename _Tp>inline void chmax(_Tp &a,const _Tp &b){(a<b)&&(a=b);}
template <typename _Tp>inline void chmin(_Tp &a,const _Tp &b){(b<a)&&(a=b);}
template <typename _Tp>inline void read(_Tp &x)
{
	char ch(getchar());bool f(false);while(!isdigit(ch)) f|=ch==45,ch=getchar();
	x=ch&15,ch=getchar();while(isdigit(ch)) x=(((x<<2)+x)<<1)+(ch&15),ch=getchar();
	f&&(x=-x);
}
template <typename _Tp,typename... Args>inline void read(_Tp &t,Args &...args){read(t);read(args...);}
inline int read_str(char *s)
{
	char ch(getchar());while(ch==' '||ch=='\r'||ch=='\n') ch=getchar();
	char *tar=s;*tar=ch,ch=getchar();while(ch!=' '&&ch!='\r'&&ch!='\n'&&ch!=EOF) *(++tar)=ch,ch=getchar();
	return tar-s+1;
}

const int N=2005;

int main()
{
	int n,k;read(n,k);
	std::vector<pl> f,g;
	f.pb({(ll)k,0LL});
	int l,r,a;
	int last=0;
	for(int i=1;i<=n;++i)
	{
		read(l,r,a);
		if(l>last)
		{
			ll minn=1e18;
			for(auto it:f) chmin(minn,(it.first==k?0:it.first)+it.second);
			for(auto it:f) if(it.first!=k) g.pb(it);
			g.pb({k,minn});
			f=g,g.clear();
		}
		for(auto it:f)
		{
			ll rest=it.first,tot=it.second+a;
			ll need=(max(0LL,a-rest)+k-1)/k;
			if(l+need>r) continue;
			ll tmp=need*k-(a-rest);
			g.pb({tmp,tot});
			if(l+need!=r) g.pb({k,tot+tmp});
		}
		last=r,f=g,g.clear();
	}
	ll ans=INF;
	for(auto it:f) chmin(ans,it.second);
	if(ans>=INF) ans=-1;
	printf("%lld\n",ans);
	return 0;
}


// Author -- xyr2005

#include<bits/stdc++.h>

#define lowbit(x) ((x)&(-(x)))
#define DEBUG fprintf(stderr,"Running on Line %d in Function %s\n",__LINE__,__FUNCTION__)
#define SZ(x) ((int)x.size())
#define mkpr std::make_pair
#define pb push_back

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef std::pair<int,int> pi;
typedef std::pair<ll,ll> pl;
using std::min;
using std::max;

const int inf=0x3f3f3f3f,Inf=0x7fffffff;
const ll INF=0x3f3f3f3f3f3f3f3f;

std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count());
template <typename _Tp>_Tp gcd(const _Tp &a,const _Tp &b){return (!b)?a:gcd(b,a%b);}
template <typename _Tp>inline _Tp abs(const _Tp &a){return a>=0?a:-a;}
template <typename _Tp>inline void chmax(_Tp &a,const _Tp &b){(a<b)&&(a=b);}
template <typename _Tp>inline void chmin(_Tp &a,const _Tp &b){(b<a)&&(a=b);}
template <typename _Tp>inline void read(_Tp &x)
{
	char ch(getchar());bool f(false);while(!isdigit(ch)) f|=ch==45,ch=getchar();
	x=ch&15,ch=getchar();while(isdigit(ch)) x=(((x<<2)+x)<<1)+(ch&15),ch=getchar();
	f&&(x=-x);
}
template <typename _Tp,typename... Args>inline void read(_Tp &t,Args &...args){read(t);read(args...);}
inline int read_str(char *s)
{
	char ch(getchar());while(ch==' '||ch=='\r'||ch=='\n') ch=getchar();
	char *tar=s;*tar=ch,ch=getchar();while(ch!=' '&&ch!='\r'&&ch!='\n'&&ch!=EOF) *(++tar)=ch,ch=getchar();
	return tar-s+1;
}

const int N=2005;

int main()
{
	int n,k;read(n,k);
	std::vector<pl> f,g;
	f.pb({(ll)k,0LL});
	int l,r,a;
	int last=0;
	for(int i=1;i<=n;++i)
	{
		read(l,r,a);
		if(l>last)
		{
			ll minn=INF;
			for(auto it:f) chmin(minn,(it.first==k?0:it.first)+it.second);
			for(auto it:f) if(it.first!=k) g.pb(it);
			if(minn<INF) g.pb({k,minn});
			f=g,g.clear();
		}
		for(auto it:f)
		{
			ll rest=it.first,tot=it.second+a;
			ll need=(max(0LL,a-rest)+k-1)/k;
			if(l+need>r) continue;
			ll tmp=need*k-(a-rest);
			g.pb({tmp,tot});
			if(l+need!=r) g.pb({k,tot+tmp});
		}
		last=r,f=g,g.clear();
	}
	ll ans=INF;
	for(auto it:f) chmin(ans,it.second);
	if(ans>=INF) ans=-1;
	printf("%lld\n",ans);
	return 0;
}


// Author -- xyr2005

#include<bits/stdc++.h>

#define lowbit(x) ((x)&(-(x)))
#define DEBUG fprintf(stderr,"Running on Line %d in Function %s\n",__LINE__,__FUNCTION__)
#define SZ(x) ((int)x.size())
#define mkpr std::make_pair
#define pb push_back

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef std::pair<int,int> pi;
typedef std::pair<ll,ll> pl;
using std::min;
using std::max;

const int inf=0x3f3f3f3f,Inf=0x7fffffff;
const ll INF=0x3f3f3f3f3f3f3f3f;

std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count());
template <typename _Tp>_Tp gcd(const _Tp &a,const _Tp &b){return (!b)?a:gcd(b,a%b);}
template <typename _Tp>inline _Tp abs(const _Tp &a){return a>=0?a:-a;}
template <typename _Tp>inline void chmax(_Tp &a,const _Tp &b){(a<b)&&(a=b);}
template <typename _Tp>inline void chmin(_Tp &a,const _Tp &b){(b<a)&&(a=b);}
template <typename _Tp>inline void read(_Tp &x)
{
	char ch(getchar());bool f(false);while(!isdigit(ch)) f|=ch==45,ch=getchar();
	x=ch&15,ch=getchar();while(isdigit(ch)) x=(((x<<2)+x)<<1)+(ch&15),ch=getchar();
	f&&(x=-x);
}
template <typename _Tp,typename... Args>inline void read(_Tp &t,Args &...args){read(t);read(args...);}
inline int read_str(char *s)
{
	char ch(getchar());while(ch==' '||ch=='\r'||ch=='\n') ch=getchar();
	char *tar=s;*tar=ch,ch=getchar();while(ch!=' '&&ch!='\r'&&ch!='\n'&&ch!=EOF) *(++tar)=ch,ch=getchar();
	return tar-s+1;
}

const int N=2005;

int main()
{
	int n,k;read(n,k);
	std::vector<pl> f,g;
	f.pb({(ll)k,0LL});
	int l,r,a;
	int last=0;
	for(int i=1;i<=n;++i)
	{
		read(l,r,a);
		if(l>last)
		{
			ll minn=1e18;
			for(auto it:f) if(it.first+it.second<minn) minn=it.first+it.second;
			f.pb({k,minn});
		}
		for(auto it:f)
		{
			ll rest=it.first,tot=it.second+a;
			ll need=(a-rest+k-1)/k;
			if(l+need>r) continue;
			ll tmp=need*k-(a-rest);
			g.pb({tmp,tot});
		}
		last=r,f=g,g.clear();
	}
	ll ans=INF;
	for(auto it:f) chmin(ans,it.second);
	if(ans>=INF) ans=-1;
	printf("%lld\n",ans);
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <cassert>
#include <iostream>
#include <string>
#define ll long long
#define pii pair<int,int>
#define pli pair<ll,int>
#define pll pair<ll,ll>
#define mod 1000000000
#define mod1 1000000009
#define mod2 1000000021
#define INF 1987654321987654231
using namespace std;

/* 🐣🐥 */
struct info {
	int l, r, a;
} arr[2001];
int main() {
	int n, k, a, b, c;
	scanf("%d %d", &n, &k);
	for (int i = 0; i < n; i++) {
		scanf("%d %d %d", &a, &b, &c);
		arr[i] = { a,b,c };
	}
	ll dp[2001][2001][2];
	memset(dp, 0x3f, sizeof(dp));
	for (int i = 0; i < n; i++) {
		for (int kk = 0; kk < 2; kk++) { // kk = 1의 의미는 i-1의 아이가 r시간에 쐈다.
			ll tmp = 0, mn = INF;

			// 이전 애가 r에 안 쐈거나, 이전 애가 r에 쐈어도 나의 l과 다르다면, 장전한 채로 오겠다.
			int cnt = ((kk == 0 || i == 0 || arr[i - 1].r != arr[i].l) ? k : 0);

			if (i == 0) mn = 0;
			else {
				for (int j = 0; j < i; j++) {
					if (dp[j][i - 1][kk] >= INF) continue;
					mn = min(mn, dp[j][i - 1][kk] + (k - dp[j][i - 1][kk] % k) % k);
				}
			}

			if (mn >= INF) continue;

			for (int j = i; j < n; j++) {
				// j - 1에 남은 총알 cnt를 쏜다.
				int curM = max(0, (arr[j].a - cnt));

				// 현재 몬스터 죽일 최소 장전 수
				int t = curM / k + (curM % k > 0);

				// 장전할 수 있는 횟수
				int d = arr[j].r - arr[j].l;

				int rm = curM % k;

				if (t > d)
					break;

				tmp += arr[j].a;

				dp[i][j][1] = min(dp[i][j][1], mn + arr[j].a);
				if (t <= d - 1) {
					dp[i][j][0] = min(dp[i][j][0], mn + arr[j].a);
				}

				if (curM == 0) cnt -= arr[j].a;
				else if (t == d) {
					cnt = (k - rm)%k;
				}
				else cnt = k - rm;
			}
		}
	}
	ll ans = INF;
	for (int i = 0; i < n; i++) {
		ans = min(ans, dp[i][n - 1][0]);
		ans = min(ans, dp[i][n - 1][1]);
	}
	if (ans >= INF) printf("-1");
	else printf("%lld", ans);
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <cassert>
#include <iostream>
#include <string>
#define ll long long
#define pii pair<int,int>
#define pli pair<ll,int>
#define pll pair<ll,ll>
#define mod 1000000000
#define mod1 1000000009
#define mod2 1000000021
#define INF 1987654321987654231
using namespace std;

/* 🐣🐥 */
struct info {
	int l, r, a;
} arr[2001];
int main() {
	int n, k, a, b, c;
	scanf("%d %d", &n, &k);
	for (int i = 0; i < n; i++) {
		scanf("%d %d %d", &a, &b, &c);
		arr[i] = { a,b,c };
	}
	ll dp[2001][2001][2];
	memset(dp, 0x3f, sizeof(dp));
	for (int i = 0; i < n; i++) {
		for (int kk = 0; kk < 2; kk++) { // kk = 1의 의미는 i-1의 아이가 r시간에 장전을 했다.
			ll tmp = 0, mn = INF;
			// 이전 애가 r에 장전을 안 했거나, 이전 애가 장전을 했어도 나의 시작과 다르다면, 장전한 채로 오겠다.
			int cnt = ((kk == 0 || i == 0 || arr[i - 1].r != arr[i].l) ? k : 0);
			//int cnt = 0;
			// 이전 애가 장전을 했고, 이전 r과 현재 l이 같다면 나는 장전기회 하나를 잃는다.
			bool flag = (i != 0 && kk && arr[i - 1].r == arr[i].l);

			if (i == 0) mn = 0;
			else {
				for (int j = 0; j < i; j++) {
					if (dp[j][i - 1][kk] >= INF) continue;
					mn = min(mn, dp[j][i - 1][kk] + (k - dp[j][i - 1][kk] % k)%k);
				}
			}

			if (mn >= INF) continue;

			for (int j = i; j < n; j++) {
				int curM = max(0, (arr[j].a - cnt));
				// 현재 몬스터 죽일 최소 장전 수
				int t = curM / k;
				// 장전할 수 있는 횟수
				int d = arr[j].r - arr[j].l + 1 - ((cnt > 0)| flag);
				int rm = curM - k * t;

				// 앞에 총알이 남았으니..
				if (t > d - (rm >= 1))
					break;

				dp[i][j][1] = min(dp[i][j][1], mn + tmp + k * t + rm + min(cnt, arr[j].a));
				if (t <= d - (rm >= 1) - 1) {
					dp[i][j][0] = min(dp[i][j][0], mn + tmp + k * t + rm + min(cnt, arr[j].a));
				}

				tmp += arr[j].a;
				if (cnt >= arr[j].a) cnt -= arr[j].a;
				else cnt = k - rm;

				//if (cnt == 0 && t <= d - (arr[j].r == arr[j + 1].l))
					//cnt = k;

				if ((d - (rm >= 1)) == t && arr[j].r == arr[j + 1].l) flag = true;
				else flag = false;
			}
		}
	}
	ll ans = INF;
	for (int i = 0; i < n; i++) {
		ans = min(ans, dp[i][n - 1][0]);
		ans = min(ans, dp[i][n - 1][1]);
	}
	if (ans >= INF) printf("-1");
	else printf("%lld", ans);
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <cassert>
#include <iostream>
#include <string>
#define ll long long
#define pii pair<int,int>
#define pli pair<ll,int>
#define pll pair<ll,ll>
#define mod 1000000000
#define mod1 1000000009
#define mod2 1000000021
#define INF 1987654321987654231
using namespace std;

/* 🐣🐥 */
struct info {
	int l, r, a;
} arr[2001];
int main() {
	int n, k, a, b, c;
	scanf("%d %d", &n, &k);
	for (int i = 0; i < n; i++) {
		scanf("%d %d %d", &a, &b, &c);
		arr[i] = { a,b,c };
	}
	ll dp[2001][2001][2];
	memset(dp, 0x3f, sizeof(dp));
	for (int i = 0; i < n; i++) {
		for (int kk = 0; kk < 2; kk++) {
			ll tmp = 0, mn = INF;
			int cnt = ((kk == 0 || i == 0 || arr[i - 1].r != arr[i].l) ? k : 0);
			bool flag = (i != 0 && kk && arr[i - 1].r == arr[i].l);

			if (i == 0) mn = 0;
			else {
				for (int j = 0; j < i; j++) {
					if (dp[j][i - 1][kk] >= INF) continue;
					int tt = dp[j][i - 1][kk] % k;
					tt = tt ? tt : k;
					mn = min(mn, dp[j][i - 1][kk] + k - tt);
				}
			}

			for (int j = i; j < n; j++) {
				int t = (arr[j].a - cnt) / k;
				int d = arr[j].r - arr[j].l - flag;
				int rm = arr[j].a - min(cnt, arr[j].a) - k * t;

				if (t + (rm >= 1) > d)
					break;

				dp[i][j][1] = min(dp[i][j][1], mn + tmp + k * t + rm + min(cnt, arr[j].a));
				if (t + (rm >= 1) <= d - 1 + !kk) {
					dp[i][j][0] = min(dp[i][j][0], mn + tmp + k * t + rm + min(cnt, arr[j].a));
				}

				tmp += arr[j].a;
				if (cnt >= arr[i].a) cnt -= arr[j].a;
				else cnt = k - rm;

				if (cnt == 0 && t <= d - (arr[j].r == arr[j + 1].l))
					cnt = k;

				if (d == t && arr[j].r == arr[j + 1].l) flag = true;
				else flag = false;
			}
		}
	}
	ll ans = INF;
	for (int i = 0; i < n; i++) {
		ans = min(ans, dp[i][n - 1][0]);
		ans = min(ans, dp[i][n - 1][1]);
	}
	if (ans >= INF) printf("-1");
	else printf("%lld", ans);
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <cassert>
#include <iostream>
#include <string>
#define ll long long
#define pii pair<int,int>
#define pli pair<ll,int>
#define pll pair<ll,ll>
#define mod 1000000000
#define mod1 1000000009
#define mod2 1000000021
#define INF 1987654321987654231
using namespace std;

/* 🐣🐥 */
struct info {
	int l, r, a;
} arr[2001];
int main() {
	int n, k, a, b, c;
	scanf("%d %d", &n, &k);
	for (int i = 0; i < n; i++) {
		scanf("%d %d %d", &a, &b, &c);
		arr[i] = { a,b,c };
	}
	ll dp[2001][2001][2];
	memset(dp, 0x3f, sizeof(dp));
	for (int i = 0; i < n; i++) {
		for (int kk = 0; kk < 2; kk++) { // kk = 1의 의미는 i-1의 아이가 r시간에 쐈다.
			ll tmp = 0, mn = INF;

			// 이전 애가 r에 안 쐈거나, 이전 애가 r에 쐈어도 나의 l과 다르다면, 장전한 채로 오겠다.
			int cnt = ((kk == 0 || i == 0 || arr[i - 1].r != arr[i].l) ? k : 0);

			// 이전 애가 r에 쐈고, 이전 r과 현재 l이 같다면 나는 장전기회 하나를 잃는다.
			//bool flag = (i != 0 && kk && arr[i - 1].r == arr[i].l);
			bool flag = false;

			if (i == 0) mn = 0;
			else {
				for (int j = 0; j < i; j++) {
					if (dp[j][i - 1][kk] >= INF) continue;
					mn = min(mn, dp[j][i - 1][kk] + (k - dp[j][i - 1][kk] % k) % k);
				}
			}

			if (mn >= INF) continue;

			for (int j = i; j < n; j++) {
				// j - 1에 남은 총알 cnt를 쏜다.
				int curM = max(0, (arr[j].a - cnt));

				// 현재 몬스터 죽일 최소 장전 수
				int t = curM / k + (curM % k > 0);

				// 장전할 수 있는 횟수
				int d = arr[j].r - arr[j].l - flag;
				//int d = arr[j].r - arr[j].l + 1 - ((cnt > 0) | flag);

				//int rm = curM - k * t;
				int rm = curM % k;

				//if (t > d - (rm >= 1))
				if (t > d)
					break;

				//dp[i][j][1] = min(dp[i][j][1], mn + tmp + k * t + rm + min(cnt, arr[j].a));
				dp[i][j][1] = min(dp[i][j][1], mn + tmp + arr[j].a);

				//if (t <= d - (rm >= 1) - 1) {
				if (t <= d - 1) {
					//dp[i][j][0] = min(dp[i][j][0], mn + tmp + k * t + rm + min(cnt, arr[j].a));
					dp[i][j][0] = min(dp[i][j][0], mn + tmp + arr[j].a);
				}

				tmp += arr[j].a;

				if (curM == 0) cnt -= arr[j].a;
				else if (t == d) {
					cnt = (k - rm)%k;
				}
				else cnt = k - rm;

				//if (cnt == 0 && d - (rm >= 1) - t >= 1) cnt = k;

				// arr[j].r에 이미 장전을 했다 (flag)
				flag = false;
				//if ((d - (rm >= 1)) == t && arr[j].r == arr[j + 1].l) flag = true;
				//else flag = false;
			}
		}
	}
	ll ans = INF;
	for (int i = 0; i < n; i++) {
		ans = min(ans, dp[i][n - 1][0]);
		ans = min(ans, dp[i][n - 1][1]);
	}
	if (ans >= INF) printf("-1");
	else printf("%lld", ans);
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <cassert>
#include <iostream>
#include <string>
#define ll long long
#define pii pair<int,int>
#define pli pair<ll,int>
#define pll pair<ll,ll>
#define mod 1000000000
#define mod1 1000000009
#define mod2 1000000021
#define INF 1987654321987654231
using namespace std;

/* 🐣🐥 */
struct info {
	int l, r, a;
} arr[2001];
int main() {
	int n, k, a, b, c;
	scanf("%d %d", &n, &k);
	for (int i = 0; i < n; i++) {
		scanf("%d %d %d", &a, &b, &c);
		arr[i] = { a,b,c };
	}
	ll dp[2001][2001][2];
	memset(dp, 0x3f, sizeof(dp));
	for (int i = 0; i < n; i++) {
		for (int kk = 0; kk < 2; kk++) {
			ll tmp = 0, mn = INF;
			int cnt = ((kk == 0 || i == 0 || arr[i - 1].r != arr[i].l) ? k : 0);
			bool flag = (i != 0 && kk && arr[i - 1].r == arr[i].l);

			if (i == 0) mn = 0;
			else {
				for (int j = 0; j < i; j++) {
					if (dp[j][i - 1][kk] >= INF) continue;
					int tt = dp[j][i - 1][kk] % k;
					tt = tt ? tt : k;
					mn = min(mn, dp[j][i - 1][kk] + k - tt);
				}
			}

			for (int j = i; j < n; j++) {
				int t = (arr[j].a - cnt) / k;
				int d = arr[j].r - arr[j].l - flag;
				int rm = arr[j].a - min(cnt, arr[j].a) - k * t;

				if (t + (rm >= 1) > d)
					break;

				dp[i][j][1] = min(dp[i][j][1], mn + tmp + k * t + rm + min(cnt, arr[j].a));
				if (t + (rm >= 1) <= d - 1) {
					dp[i][j][0] = min(dp[i][j][0], mn + tmp + k * t + rm + min(cnt, arr[j].a));
				}

				tmp += arr[j].a;
				if (cnt >= arr[i].a) cnt -= arr[i].a;
				else cnt = k - rm;

				if (cnt == 0 && t <= d - (arr[j].r == arr[j + 1].l))
					cnt = k;

				if (d == t && arr[j].r == arr[j + 1].l) flag = true;
				else flag = false;
			}
		}
	}
	ll ans = INF;
	for (int i = 0; i < n; i++) {
		ans = min(ans, dp[i][n - 1][0]);
		ans = min(ans, dp[i][n - 1][1]);
	}
	if (ans >= INF) printf("-1");
	else printf("%lld", ans);
}


#include<bits/stdc++.h>

using namespace std;

int main() {

  int t; cin>>t;
     while(t--) {
         int n,m;
         bool ans=true;
         cin>>n>>m;
         for(int i=0;i<102;i++) f[i]=0;
         for(int i=0;i<n;i++) cin>>a[i];
         for(int i=0;i<m;i++) {
             cin>>p[i]; f[p[i]-1]++;
         }
         sort(p,p+m);
         for(int i=0;i<n-1;i++) {
             for(int j=0;j<n-1;j++) {
                 if(a[j]>a[j+1]) {
                     if(f[j]) {
                         int temp=a[j];
                         a[j]=a[j+1];
                         a[j+1]=temp;
                     }
                 }
             }
         }
         for(int i=0;i<n-1;i++) {
             if(a[i]>a[i+1]) {
                 ans=false;
             }
         }
         if(ans) cout<<"YES"<<endl;
         else cout<<"NO"<<endl;
     }
 }


#include<bits/stdc++.h>

using namespace std;

int main() {

int t;

cin>>t;

for(int i=1;i<=t;i++) {

int n,m;

cin>>n>>m;

int a[n];
int b[m];

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

for(int j=0;j<m;j++) {
  cin>>b[j];
}

//int p=0;
//int count=0;

for(int i=-0;i<n-1;i++) {
  if(a[i]>a[i+1]) {
    p++;
    int c=i;
    for(int j=0;j<m;j++) {
      if(b[j]-1==c) {
        count++;
      }
    }
  }
}

cout<<p;
cout<<count;

if(p==count) {
  cout<<"YES";
}

else {
  cout<<"NO";
}

cout<<endl;

}

  return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main() {

int t;

cin>>t;

for(int i=1;i<=t;i++) {

int n,m;

cin>>n>>m;

int a[n];
int b[m];

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

for(int j=0;j<m;j++) {
  cin>>b[j];
}

int p=0;
int count=0;

for(int i=-0;i<n-1;i++) {
  if(a[i]>a[i+1]) {
    p++;
    int c=i;
    for(int j=0;j<m;j++) {
      if(b[j]-1==c) {
        count++;
      }
    }
  }
}

cout<<p;
cout<<count;

if(p==count) {
  cout<<"YES";
}

else {
  cout<<"NO";
}

cout<<endl;

}

  return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main() {

int t;

cin>>t;

for(int i=1;i<=t;i++) {

int n,m;

cin>>n>>m;

int a[n];
int b[m];

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

for(int j=0;j<m;j++) {
  cin>>b[j];
}

int p=0;
int count=0;

for(int i=-0;i<n-1;i++) {
  if(a[i]>a[i+1]) {
    p++;
    int c=i;
    for(int j=0;j<m;j++) {
      if(b[j]-1==c) {
        count++;
        swap(a[i],a[i+1]);
      }
    }
  }
}

//cout<<p;
//cout<<count;

if(p==count) {
  cout<<"YES";
}

else {
  cout<<"NO";
}

cout<<endl;

}

  return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main() {

  int t; cin>>t;
     while(t--) {
         int n,m;
         bool ans=true;
         cin>>n>>m;
         int f[102];
         int p[m];
         int a[n];
         for(int i=0;i<102;i++) f[i]=0;
         for(int i=0;i<n;i++) cin>>a[i];
         for(int i=0;i<m;i++) {
             cin>>p[i]; f[p[i]-1]++;
         }
         sort(p,p+m);
         for(int i=0;i<n-1;i++) {
             for(int j=0;j<n-1;j++) {
                 if(a[j]>a[j+1]) {
                     if(f[j]) {
                         int temp=a[j];
                         a[j]=a[j+1];
                         a[j+1]=temp;
                     }
                 }
             }
         }
         for(int i=0;i<n-1;i++) {
             if(a[i]>a[i+1]) {
                 ans=false;
             }
         }
         if(ans) cout<<"YES"<<endl;
         else cout<<"NO"<<endl;
     }
 }


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

using namespace std;
using namespace __gnu_pbds;

typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> pbds;

// when we want to insert same element multiple times into pbds
// typedef tree<pair<int,int>,null_type,less<pair<int,int > >,rb_tree_tag,tree_order_statistics_node_update> pbds;

#define int                     long long int
#define pb                      push_back
#define setbits(x)              __builtin_popcountll(x)
#define zerobits(x)             __builtin_ctzll(x)
#define ps(x,y)                 fixed<<setprecision(y)<<x // cout<<ps(1.255114,4)
#define max3(a,b,c)             max(max((a),(b)),(c))
#define max4(a,b,c,d)           max(max((a),(b)),max((c),(d)))
#define min3(a,b,c)             min(min((a),(b)),(c))
#define min4(a,b,c,d)           min(min((a),(b)),min((c),(d)))
#define tc(t)                   int t; cin>>t; while(t--)

const long long mod=1000000007;
const int inf=1000000000000000000;
const long double pi = 3.14159265358979323846264338;

bool double_compare(double a, double b, double epsilon = 0.001)
{
    return std::abs(a - b) < epsilon;
}
int lcm(int a,int b){
    return (a*b)/__gcd(a,b);
}
bool isPrime(int n)
{
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;

    if (n % 2 == 0 || n % 3 == 0)
        return false;

    for (int i = 5; i * i <= n; i = i + 6){
        if (n % i == 0 || n % (i + 2) == 0)
            return false;
    }

    return true;
}
// Fermat's little theorem
int pow(int a, unsigned int b,int mod1){

    int ans=1;
    while(b){

        if(b&1ll){

            ans*=a;
            ans%=mod1;
        }
        b/=2;
        a*=a;
        a%=mod1;
    }

    return ans;
}
int inverse(int a,int mod1){

    return pow(a,mod1-2,mod1);    // a^-1%p
}
///////////////////////////////////////////////////////////////////////////////////////////////////

signed main()
{
    tc(t)
    {
        int n,m;
        cin>>n>>m;

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

        int b[n+1]={0};
        int k=1;
        for(int i=0;i<m;i++){
            int x;
            cin>>x;
            if(b[x-1]!=0){
                b[x]=b[x-1];
                continue;
            }
            b[x-1]=k;
            b[x]=k;
            k++;
        }

        map<int,int> indx;
        sort(c,c+n);

        for(int i=0;i<n;i++){
            if(indx[c[i]]!=0){
                b[i]=1;
            }
            indx[c[i]]=i;
        }

        bool no=false;
        for(int i=0;i<n;i++){

            if(indx[a[i]]==i){
                i++;
                continue;
            }

            int j=i;
            int x=b[j];
            for(int j=i;j<=indx[a[i]];j++){
                if(b[j]!=x){
                    no=true;
                    break;
                }
            }
            if(j!=indx[a[i]]+1){
                break;
            }
        }

        if(no){
            cout<<"NO"<<endl;
        }
        else{
            cout<<"YES"<<endl;
        }
    }

    return 0;
}

/*

*/


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

using namespace std;
using namespace __gnu_pbds;

typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> pbds;

// when we want to insert same element multiple times into pbds
// typedef tree<pair<int,int>,null_type,less<pair<int,int > >,rb_tree_tag,tree_order_statistics_node_update> pbds;

#define int                     long long int
#define pb                      push_back
#define setbits(x)              __builtin_popcountll(x)
#define zerobits(x)             __builtin_ctzll(x)
#define ps(x,y)                 fixed<<setprecision(y)<<x // cout<<ps(1.255114,4)
#define max3(a,b,c)             max(max((a),(b)),(c))
#define max4(a,b,c,d)           max(max((a),(b)),max((c),(d)))
#define min3(a,b,c)             min(min((a),(b)),(c))
#define min4(a,b,c,d)           min(min((a),(b)),min((c),(d)))
#define tc(t)                   int t; cin>>t; while(t--)

const long long mod=1000000007;
const int inf=1000000000000000000;
const long double pi = 3.14159265358979323846264338;

bool double_compare(double a, double b, double epsilon = 0.001)
{
    return std::abs(a - b) < epsilon;
}
int lcm(int a,int b){
    return (a*b)/__gcd(a,b);
}
bool isPrime(int n)
{
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;

    if (n % 2 == 0 || n % 3 == 0)
        return false;

    for (int i = 5; i * i <= n; i = i + 6){
        if (n % i == 0 || n % (i + 2) == 0)
            return false;
    }

    return true;
}
// Fermat's little theorem
int pow(int a, unsigned int b,int mod1){

    int ans=1;
    while(b){

        if(b&1ll){

            ans*=a;
            ans%=mod1;
        }
        b/=2;
        a*=a;
        a%=mod1;
    }

    return ans;
}
int inverse(int a,int mod1){

    return pow(a,mod1-2,mod1);    // a^-1%p
}
///////////////////////////////////////////////////////////////////////////////////////////////////

signed main()
{
    tc(t)
    {
        int n,m;
        cin>>n>>m;

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

        int b[n+1]={0};
        int k=1;
        for(int i=0;i<m;i++){
            int x;
            cin>>x;
            if(b[x-1]!=0){
                b[x]=b[x-1];
                continue;
            }
            b[x-1]=k;
            b[x]=k;
            k++;
        }

        map<int,int> indx;
        sort(c,c+n);

        for(int i=0;i<n;i++){
            if(indx[c[i]]!=0){
                b[i]=1;
            }
            indx[c[i]]=i;
        }

        bool no=false;
        for(int i=0;i<n;i++){

            if(indx[a[i]]==i){
                continue;
            }

            int j=i;
            int x=b[i];
            for(int j=i;j<=indx[a[i]];j++){
                if(b[j]!=x){
                    no=true;
                    break;
                }
            }
            if(j!=indx[a[i]]+1){
                break;
            }
        }

        if(no){
            cout<<"NO"<<endl;
        }
        else{
            cout<<"YES"<<endl;
        }
    }

    return 0;
}

/*

*/


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

using namespace std;
using namespace __gnu_pbds;

typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> pbds;

// when we want to insert same element multiple times into pbds
// typedef tree<pair<int,int>,null_type,less<pair<int,int > >,rb_tree_tag,tree_order_statistics_node_update> pbds;

#define int                     long long int
#define pb                      push_back
#define setbits(x)              __builtin_popcountll(x)
#define zerobits(x)             __builtin_ctzll(x)
#define ps(x,y)                 fixed<<setprecision(y)<<x // cout<<ps(1.255114,4)
#define max3(a,b,c)             max(max((a),(b)),(c))
#define max4(a,b,c,d)           max(max((a),(b)),max((c),(d)))
#define min3(a,b,c)             min(min((a),(b)),(c))
#define min4(a,b,c,d)           min(min((a),(b)),min((c),(d)))
#define tc(t)                   int t; cin>>t; while(t--)

const long long mod=1000000007;
const int inf=1000000000000000000;
const long double pi = 3.14159265358979323846264338;

bool double_compare(double a, double b, double epsilon = 0.001)
{
    return std::abs(a - b) < epsilon;
}
int lcm(int a,int b){
    return (a*b)/__gcd(a,b);
}
bool isPrime(int n)
{
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;

    if (n % 2 == 0 || n % 3 == 0)
        return false;

    for (int i = 5; i * i <= n; i = i + 6){
        if (n % i == 0 || n % (i + 2) == 0)
            return false;
    }

    return true;
}
// Fermat's little theorem
int pow(int a, unsigned int b,int mod1){

    int ans=1;
    while(b){

        if(b&1ll){

            ans*=a;
            ans%=mod1;
        }
        b/=2;
        a*=a;
        a%=mod1;
    }

    return ans;
}
int inverse(int a,int mod1){

    return pow(a,mod1-2,mod1);    // a^-1%p
}
///////////////////////////////////////////////////////////////////////////////////////////////////

signed main()
{
    tc(t)
    {
        int n,m;
        cin>>n>>m;

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

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

        bool yo=true;
        while(yo){

            yo=false;
            for(int i=0;i<n;i++){

                if(a[i]>a[i+1] && indx[i]){
                    swap(a[i],a[i+1]);
                    yo=true;
                }
            }
        }

        yo=true;
        for(int i=0;i<n-1;i++){
            if(a[i]>a[i+1]){
                yo=false;
                break;
            }
        }

        if(!yo){
            cout<<"NO"<<endl;
        }
        else{
            cout<<"YES"<<endl;
        }
    }

    return 0;
}

/*

*/


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

using namespace std;
using namespace __gnu_pbds;

typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> pbds;

// when we want to insert same element multiple times into pbds
// typedef tree<pair<int,int>,null_type,less<pair<int,int > >,rb_tree_tag,tree_order_statistics_node_update> pbds;

#define int                     long long int
#define pb                      push_back
#define setbits(x)              __builtin_popcountll(x)
#define zerobits(x)             __builtin_ctzll(x)
#define ps(x,y)                 fixed<<setprecision(y)<<x // cout<<ps(1.255114,4)
#define max3(a,b,c)             max(max((a),(b)),(c))
#define max4(a,b,c,d)           max(max((a),(b)),max((c),(d)))
#define min3(a,b,c)             min(min((a),(b)),(c))
#define min4(a,b,c,d)           min(min((a),(b)),min((c),(d)))
#define tc(t)                   int t; cin>>t; while(t--)

const long long mod=1000000007;
const int inf=1000000000000000000;
const long double pi = 3.14159265358979323846264338;

bool double_compare(double a, double b, double epsilon = 0.001)
{
    return std::abs(a - b) < epsilon;
}
int lcm(int a,int b){
    return (a*b)/__gcd(a,b);
}
bool isPrime(int n)
{
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;

    if (n % 2 == 0 || n % 3 == 0)
        return false;

    for (int i = 5; i * i <= n; i = i + 6){
        if (n % i == 0 || n % (i + 2) == 0)
            return false;
    }

    return true;
}
// Fermat's little theorem
int pow(int a, unsigned int b,int mod1){

    int ans=1;
    while(b){

        if(b&1ll){

            ans*=a;
            ans%=mod1;
        }
        b/=2;
        a*=a;
        a%=mod1;
    }

    return ans;
}
int inverse(int a,int mod1){

    return pow(a,mod1-2,mod1);    // a^-1%p
}
///////////////////////////////////////////////////////////////////////////////////////////////////

signed main()
{
    tc(t)
    {
        int n,m;
        cin>>n>>m;

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

        vector<int> v;
        for(int i=0;i<m;i++){
            int x;
            cin>>x;
            v.pb(x);
        }

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

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

            if(b[v[i]-1]!=0){
                b[v[i]]=b[v[i]-1];

                continue;
            }
            b[v[i]-1]=k;
            b[v[i]]=k;
            k++;
        }

        map<int,int> indx;
        sort(c,c+n);

        for(int i=0;i<n;i++){
            if(indx[c[i]]!=0){
                b[i]=1;
            }
            indx[c[i]]=i;
        }

        bool nn=false;
        for(int i=0;i<n;i++){

            if(indx[a[i]]==i){
                continue;
            }

            int j=i;
            int x=b[i];
            for(int j=i;j<=indx[a[i]];j++){
                if(b[j]!=x){
                    nn=true;
                    break;
                }
            }
        }

        if(nn){
            cout<<"NO"<<endl;
        }
        else{
            cout<<"YES"<<endl;
        }
    }

    return 0;
}

/*

*/


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

using namespace std;
using namespace __gnu_pbds;

typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> pbds;

// when we want to insert same element multiple times into pbds
// typedef tree<pair<int,int>,null_type,less<pair<int,int > >,rb_tree_tag,tree_order_statistics_node_update> pbds;

#define int                     long long int
#define pb                      push_back
#define setbits(x)              __builtin_popcountll(x)
#define zerobits(x)             __builtin_ctzll(x)
#define ps(x,y)                 fixed<<setprecision(y)<<x // cout<<ps(1.255114,4)
#define max3(a,b,c)             max(max((a),(b)),(c))
#define max4(a,b,c,d)           max(max((a),(b)),max((c),(d)))
#define min3(a,b,c)             min(min((a),(b)),(c))
#define min4(a,b,c,d)           min(min((a),(b)),min((c),(d)))
#define tc(t)                   int t; cin>>t; while(t--)

const long long mod=1000000007;
const int inf=1000000000000000000;
const long double pi = 3.14159265358979323846264338;

bool double_compare(double a, double b, double epsilon = 0.001)
{
    return std::abs(a - b) < epsilon;
}
int lcm(int a,int b){
    return (a*b)/__gcd(a,b);
}
bool isPrime(int n)
{
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;

    if (n % 2 == 0 || n % 3 == 0)
        return false;

    for (int i = 5; i * i <= n; i = i + 6){
        if (n % i == 0 || n % (i + 2) == 0)
            return false;
    }

    return true;
}
// Fermat's little theorem
int pow(int a, unsigned int b,int mod1){

    int ans=1;
    while(b){

        if(b&1ll){

            ans*=a;
            ans%=mod1;
        }
        b/=2;
        a*=a;
        a%=mod1;
    }

    return ans;
}
int inverse(int a,int mod1){

    return pow(a,mod1-2,mod1);    // a^-1%p
}
///////////////////////////////////////////////////////////////////////////////////////////////////

signed main()
{
    tc(t)
    {
        int n,m;
        cin>>n>>m;

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

        vector<int> v;
        for(int i=0;i<m;i++){
            int x;
            cin>>x;
            v.pb(x);
        }

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

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

            if(b[v[i]-1]!=0){
                b[v[i]]=b[v[i]-1];

                continue;
            }
            b[v[i]-1]=k;
            b[v[i]]=k;
            k++;
        }

        map<int,int> indx;
        sort(c,c+n);

        for(int i=0;i<n;i++){
            if(indx[c[i]]!=0){
                b[i]=1;
            }
            indx[c[i]]=i;
        }

        bool nn=false;
        for(int i=0;i<n;i++){

            if(indx[a[i]]==i){
                continue;
            }

            int j=i;
            int x=b[i];
            for(int j=i;j<=indx[a[i]];j++){
                if(b[j]!=x){
                    nn=true;
                    break;
                }
            }
            if(j!=indx[a[i]]+1){
                break;
            }
        }

        if(!nn){
            cout<<"NO"<<endl;
        }
        else{
            cout<<"YES"<<endl;
        }
    }

    return 0;
}

/*

*/


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

using namespace std;
using namespace __gnu_pbds;

typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> pbds;

// when we want to insert same element multiple times into pbds
// typedef tree<pair<int,int>,null_type,less<pair<int,int > >,rb_tree_tag,tree_order_statistics_node_update> pbds;

#define int                     long long int
#define pb                      push_back
#define setbits(x)              __builtin_popcountll(x)
#define zerobits(x)             __builtin_ctzll(x)
#define ps(x,y)                 fixed<<setprecision(y)<<x // cout<<ps(1.255114,4)
#define max3(a,b,c)             max(max((a),(b)),(c))
#define max4(a,b,c,d)           max(max((a),(b)),max((c),(d)))
#define min3(a,b,c)             min(min((a),(b)),(c))
#define min4(a,b,c,d)           min(min((a),(b)),min((c),(d)))
#define tc(t)                   int t; cin>>t; while(t--)

const long long mod=1000000007;
const int inf=1000000000000000000;
const long double pi = 3.14159265358979323846264338;

bool double_compare(double a, double b, double epsilon = 0.001)
{
    return std::abs(a - b) < epsilon;
}
int lcm(int a,int b){
    return (a*b)/__gcd(a,b);
}
bool isPrime(int n)
{
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;

    if (n % 2 == 0 || n % 3 == 0)
        return false;

    for (int i = 5; i * i <= n; i = i + 6){
        if (n % i == 0 || n % (i + 2) == 0)
            return false;
    }

    return true;
}
// Fermat's little theorem
int pow(int a, unsigned int b,int mod1){

    int ans=1;
    while(b){

        if(b&1ll){

            ans*=a;
            ans%=mod1;
        }
        b/=2;
        a*=a;
        a%=mod1;
    }

    return ans;
}
int inverse(int a,int mod1){

    return pow(a,mod1-2,mod1);    // a^-1%p
}
///////////////////////////////////////////////////////////////////////////////////////////////////

signed main()
{
    tc(t)
    {
        int n,m;
        cin>>n>>m;

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

        vector<int> v;
        for(int i=0;i<m;i++){
            int x;
            cin>>x;
            v.pb(x);
        }

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

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

            if(b[v[i]-1]!=0){
                b[v[i]]=b[v[i]-1];

                continue;
            }
            b[v[i]-1]=k;
            b[v[i]]=k;
            k++;
        }

        map<int,int> indx;
        sort(c,c+n);

        for(int i=0;i<n;i++){
            if(indx[c[i]]!=0){
                b[i]=1;
            }
            indx[c[i]]=i;
        }

        bool nn=false;
        for(int i=0;i<n;i++){

            if(indx[a[i]]==i){
                continue;
            }

            int j=i;
            int x=b[i];
            for(int j=i;j<=indx[a[i]];j++){
                if(b[j]!=x){
                    nn=true;
                    break;
                }
            }
            if(j!=indx[a[i]]+1){
                break;
            }
        }

        if(nn){
            cout<<"NO"<<endl;
        }
        else{
            cout<<"YES"<<endl;
        }
    }

    return 0;
}

/*

*/


#include <iostream>
using namespace std;
int issorted(int z[],int y)
{    int flag=0;
    for(int i=0;i<y-1;i++)
    {
        if(z[i]>z[i+1])
        {
            flag=1;
            break;
        }

    }
    if(flag==1)
    return 0;
    else
    return 1;

}
int main() {
    int t;
    cin>>t;
    while(t--)
    {

	int n,m;
	cin>>n>>m;
	int a[n],p[m];
	for(int i=0;i<n;i++)
	cin>>a[i];
	for(int i=0;i<m;i++)
	cin>>p[i];
	int r,vot=0;
	for(int i=0;i<n;i++)
	{
	    swap(a[p[i]-1],a[p[i]]);
	    r=issorted(a,n);
	    if(r==1)
	    {
	        vot=1;
	        break;
	    }

	}
	if(vot==1)
	cout<<"YES"<<"\n";
	else
	cout<<"NO"<<"\n";
    }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
int n,m;
cin>>n>>m;
int a[n],p[m];
for(int i=0;i<n;i++){
cin>>a[i];
}
for(int j=0;j<m;j++){
cin>>p[j];
}
sort(p,p+m);
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(a[p[j]]<a[p[j]-1]){
swap(a[p[j]],a[p[j]-1]);
}
}
}
if(is_sorted(a,a+n))
cout<<"YES"<<"\n";
else
cout<<"NO"<<endl;
}
}


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

int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--)
	{
	    int n,m;
	    cin>>n>>m;
	    int a[n],p[m];
	    for(int i=0;i<n;i++)cin>>a[i];
	    for(int i=0;i<m;i++)
	    {
	        cin>>p[i];
	    }
	    for(int i=0;i<m;i++)
	    {
	        if(a[p[i]]<a[p[i]-1])
	       {
	           swap(a[p[i]],a[p[i]-1]);
	       }

	    }
	    if(is_sorted(a,a+n))
cout<<"YES"<<"\n";
else
cout<<"NO"<<endl;
	}

	return 0;
}


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

int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--)
	{
	    int n,m;
	    cin>>n>>m;
	    int a[n],p[m];
	    for(int i=0;i<n;i++)cin>>a[i];
	    for(int i=0;i<m;i++)
	    {
	        cin>>p[i];
	    }
	    for(int i=0;i<m;i++)
	    {
	        if(a[p[i]]<a[p[i]-1])
	       {
	           swap(a[p[i]],a[p[i]-1]);
	       }

	    }
	    if(is_sorted(a,a+n))
cout<<"YES"<<"\n";
else
cout<<"NO"<<endl;
	}

	return 0;
}


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

int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--)
	{
	    int n,m;
	    cin>>n>>m;
	    int a[n],p[m];
	    for(int i=0;i<n;i++)cin>>a[i];
	    for(int i=0;i<m;i++)
	    {
	        cin>>p[i];
	    }
	    sort(p,p+m);
	    for(int i=0;i<m;i++)
	    {
	        if(a[p[i]]<a[p[i]-1])
	       {
	           swap(a[p[i]],a[p[i]-1]);
	       }

	    }
	    if(is_sorted(a,a+n))
cout<<"YES"<<"\n";
else
cout<<"NO"<<endl;
	}

	return 0;
}


#include <iostream>
using namespace std;

int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--)
	{
	    int n,m;
	    cin>>n>>m;
	    int a[n],b[m];
	    for(int i=0;i<n;i++)cin>>a[i];
	    for(int i=0;i<m;i++)
	    {
	        cin>>p[i];
	    }
	    for(int i=0;i<m;i++)
	    {
	        if(a[p[i]]<a[p[i]-1])
	       {
	           swap(a[p[i]],a[p[i]]-1);
	       }

	    }
	    if(is_sorted(a,a+n))
cout<<"YES"<<"\n";
else
cout<<"NO"<<endl;
	}

	return 0;
}


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

typedef long long int ll;
#define REP(i,a,b) for(ll i = ll(a); i < ll(b); i++)

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

	ll test;
	cin>>test;
	while(test--){
		ll n, m, a;
		cin>>n>>m;
		vector<ll> nums(n), temp(n);
		ll pos[n], checked[n];
		memset(pos, 0, sizeof(pos));
		memset(checked, false, sizeof(checked));

		REP(i,0,n){
			cin>>nums[i];
			temp[i] = nums[i];
		}
		REP(i,0,m){
			cin>>a;
			pos[a-1] += 1;
			pos[a] += 2;
		}
		// REP(i,0,n){
		// 	cout<<pos[i]<<" ";
		// }
		sort(temp.begin(), temp.end());
		bool flag = false;
		REP(i,0,n){
			if(nums[i] != temp[i]){
				ll y = n-1, z = 0;
				while(temp[y] != nums[i]){
					y--;
					while(checked[y])
						y--;
				}
				while(temp[z] != nums[i]){
					z++;
					while(checked[z])
						z++;
				}
				if(labs(z-i) < labs(y-i))
					y = z;
				checked[y] = true;
				bool flag2 = false;
				if(y < i){
					for(ll x = i; x > y; x--){
						if(pos[x] == 1 || pos[x] == 0){
							cout<<"NO\n";
							flag2 = true;
							break;
						}
					}
				} else {
					for( ll x = i; x < y; x++){
						if(pos[x] == 2 || pos[x] == 0){
							cout<<"NO\n";
							flag2 = true;
							break;
						}
					}
				}
				if(flag2){
					flag = true;
					break;
				}
			}
		}
		if(!flag){
			cout<<"YES\n";
		}
	}
	return 0;
}


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

typedef long long int ll;
#define REP(i,a,b) for(ll i = ll(a); i < ll(b); i++)

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

	ll test;
	cin>>test;
	while(test--){
		ll n, m, a;
		cin>>n>>m;
		vector<ll> nums(n), temp(n);
		ll pos[n];
		memset(pos, 0, sizeof(pos));

		REP(i,0,n){
			cin>>nums[i];
			temp[i] = nums[i];
		}
		REP(i,0,m){
			cin>>a;
			pos[a-1] += 1;
			pos[a] += 2;
		}
		sort(temp.begin(), temp.end());
		bool flag = false;
		REP(i,0,n){
			if(nums[i] != temp[i]){
				auto it = find(temp.begin(), temp.end(), nums[i]);
				ll y = it - temp.begin();
				bool flag2 = false;
				if(y < i){
					for(ll x = i; x > y; x--){
						if(pos[x] == 1){
							cout<<"NO\n";
							flag2 = true;
							break;
						}
					}
				} else {
					for( ll x = i; x < y; x++){
						if(pos[x] == 2){
							cout<<"NO\n";
							flag2 = true;
							break;
						}
					}
				}
				if(flag2){
					flag = true;
					break;
				}
			}
		}
		if(!flag){
			cout<<"YES\n";
		}
	}
	return 0;
}


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

typedef long long int ll;
#define REP(i,a,b) for(ll i = ll(a); i < ll(b); i++)

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

	ll test;
	cin>>test;
	while(test--){
		ll n, m, a;
		cin>>n>>m;
		vector<ll> nums(n), temp(n);
		ll pos[n];
		memset(pos, false, sizeof(pos));

		REP(i,0,n){
			cin>>nums[i];
			temp[i] = nums[i];
		}
		REP(i,0,m){
			cin>>a;
			pos[a-1] += 1;
			pos[a] += 2;
		}
		sort(temp.begin(), temp.end());
		bool flag = false;
		REP(i,0,n){
			if(nums[i] < temp[i] && pos[i] == 1){
				cout<<"NO\n";
				flag = true;
				break;
			} else if(nums[i] > temp[i] && pos[i] == 2){
				cout<<"NO\n";
				flag = true;
				break;
			}
		}
		if(flag){
			continue;
		} else {
			cout<<"YES\n";
		}
	}
	return 0;
}


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

typedef long long int ll;
#define REP(i,a,b) for(ll i = ll(a); i < ll(b); i++)

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

	ll test;
	cin>>test;
	while(test--){
		ll n, m, a;
		cin>>n>>m;
		vector<ll> nums(n), temp(n);
		ll pos[n], checked[n];
		memset(pos, 0, sizeof(pos));
		memset(checked, false, sizeof(checked));

		REP(i,0,n){
			cin>>nums[i];
			temp[i] = nums[i];
		}
		REP(i,0,m){
			cin>>a;
			pos[a-1] += 1;
			pos[a] += 2;
		}
		sort(temp.begin(), temp.end());
		bool flag = false;
		REP(i,0,n){
			if(nums[i] != temp[i]){
				ll y = 0;
				while(temp[y] != nums[i] || checked[y]){
					y = (y+1)%n;
				}
				checked[y] = true;
				bool flag2 = false;
				if(y < i){
					for(ll x = i; x > y; x--){
						if(pos[x] == 1){
							cout<<"NO\n";
							flag2 = true;
							break;
						}
					}
				} else {
					for( ll x = i; x < y; x++){
						if(pos[x] == 2){
							cout<<"NO\n";
							flag2 = true;
							break;
						}
					}
				}
				if(flag2){
					flag = true;
					break;
				}
			}
		}
		if(!flag){
			cout<<"YES\n";
		}
	}
	return 0;
}


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

typedef long long int ll;
#define REP(i,a,b) for(ll i = ll(a); i < ll(b); i++)

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

	ll test;
	cin>>test;
	while(test--){
		ll n, m, a;
		cin>>n>>m;
		vector<ll> nums(n), temp(n);
		ll pos[n];
		memset(pos, false, sizeof(pos));

		REP(i,0,n){
			cin>>nums[i];
			temp[i] = nums[i];
		}
		REP(i,0,m){
			cin>>a;
			pos[a-1] += 1;
			pos[a] += 2;
		}
		sort(temp.begin(), temp.end());
		bool flag = false;
		REP(i,0,n){
			if(nums[i] != temp[i]){
				auto it = find(temp.begin(), temp.end(), nums[i]);
				ll y = it - temp.begin();
				bool flag2 = false;
				if(y < i){
					for(ll x = i; x > y; x--){
						if(pos[x] == 1){
							cout<<"NO\n";
							flag2 = true;
							break;
						}
					}
				} else {
					for( ll x = i; x < y; x++){
						if(pos[x] == 2){
							cout<<"NO\n";
							flag2 = true;
							break;
						}
					}
				}
				if(flag2){
					flag = true;
					break;
				}
			}
		}
		if(flag){
			continue;
		} else {
			cout<<"YES\n";
		}
	}
	return 0;
}


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

typedef long long int ll;
#define REP(i,a,b) for(ll i = ll(a); i < ll(b); i++)

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

	ll test;
	cin>>test;
	while(test--){
		ll n, m, a;
		cin>>n>>m;
		vector<ll> nums(n), temp(n);
		ll pos[n], checked[n];
		memset(pos, 0, sizeof(pos));
		memset(checked, false, sizeof(checked));

		REP(i,0,n){
			cin>>nums[i];
			temp[i] = nums[i];
		}
		REP(i,0,m){
			cin>>a;
			pos[a-1] += 1;
			pos[a] += 2;
		}
		// REP(i,0,n){
		// 	cout<<pos[i]<<" ";
		// }
		sort(temp.begin(), temp.end());
		bool flag = false;
		REP(i,0,n){
			if(nums[i] != temp[i]){
				ll y = n-1;
				while(temp[y] != nums[i] || checked[y]){
					y--;
				}
				checked[y] = true;
				bool flag2 = false;
				if(y < i){
					for(ll x = i; x > y; x--){
						if(pos[x] == 1 || pos[x] == 0){
							cout<<"NO\n";
							flag2 = true;
							break;
						}
					}
				} else {
					for( ll x = i; x < y; x++){
						if(pos[x] == 2 || pos[x] == 0){
							cout<<"NO\n";
							flag2 = true;
							break;
						}
					}
				}
				if(flag2){
					flag = true;
					break;
				}
			}
		}
		if(!flag){
			cout<<"YES\n";
		}
	}
	return 0;
}


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

typedef long long int ll;
#define REP(i,a,b) for(ll i = ll(a); i < ll(b); i++)

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

	ll test;
	cin>>test;
	while(test--){
		ll n, m, a;
		cin>>n>>m;
		vector<ll> nums(n), temp(n);
		ll pos[n], checked[n];
		memset(pos, 0, sizeof(pos));
		memset(checked, false, sizeof(checked));

		REP(i,0,n){
			cin>>nums[i];
			temp[i] = nums[i];
		}
		REP(i,0,m){
			cin>>a;
			pos[a-1] += 1;
			pos[a] += 2;
		}
		sort(temp.begin(), temp.end());
		bool flag = false;
		REP(i,0,n){
			if(nums[i] != temp[i]){
				ll y = 0;
				while(temp[y] != nums[i] || checked[y]){
					y = (y+1)%n;
				}
				checked[y] = true;
				bool flag2 = false;
				if(y < i){
					for(ll x = i; x > y; x--){
						if(pos[x] == 1 || pos[x] == 0){
							cout<<"NO\n";
							flag2 = true;
							break;
						}
					}
				} else {
					for( ll x = i; x < y; x++){
						if(pos[x] == 2 || pos[x] == 0){
							cout<<"NO\n";
							flag2 = true;
							break;
						}
					}
				}
				if(flag2){
					flag = true;
					break;
				}
			}
		}
		if(!flag){
			cout<<"YES\n";
		}
	}
	return 0;
}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t,i;
    cin>>t;
    for(i=0;i<t;i++)
    {  int n,m,j,k,a;
       cin>>n>>m;
       int arr[n],p[m];
       for(j=0;j<n;j++)
       {
           cin>>arr[j];
       }
       for(k=0;k<m;k++)
       {
           cin>>p[k];
       }
       sort(p,p+m);
       k=0;

       while(k<m)
       {
           if(k!=m-1)
           {
          if(p[k+1]-p[k]==1)
          {
            if(p[k]-1==0)
            {
              sort(arr+p[k]-1,arr+p[k+1]+1);
            }
            else
               sort(arr+p[k],arr+p[k+1]+1);

          }
          else
          {
              if(arr[p[k]]<arr[p[k]-1])
              {
                  int temp=arr[p[k]];
                  arr[p[k]]=arr[p[k]-1];
                  arr[p[k]-1]=temp;
              }
          }
           }
           else
           {  if(arr[p[k]]<arr[p[k]-1])
              {
               int tp=arr[p[k]];
               arr[p[k]]=arr[p[k]-1];
               arr[p[k]-1]=tp;

              }

           }

          k++;
       }

      for(a=0;a<n;a++)
      {
          if(arr[a]<arr[a-1])
          break;

      }
      if(a==n)
      {
          cout<<"YES"<<endl;
      }
      else
      {
          cout<<"NO"<<endl;
      }

    }
}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t,i;
    cin>>t;
    for(i=0;i<t;i++)
    {  int n,m,j,k,a;
       cin>>n>>m;
       int arr[n];
       for(j=0;j<n;j++)
       {
           cin>>arr[j];
       }
       vector<int> p(n);
		for (int k = 0; k < m; ++k) {
			int pos;
			cin >> pos;
			p[pos - 1] = 1;
		}

       int flag;
      while(true)
      {   flag=0;
       for(k=0;k<n-1;k++)
       {
           if(p[k]&&arr[k]>arr[k+1])
           {     //cout<<p[k]<<endl;
               flag=1;
               swap(arr[k],arr[k+1]);

           }
       }

       if(!flag)
       break;
      }
       for(k=1;k<n;k++)
       {
           if(arr[k]<arr[k-1])
           break;
       }

       if(k==n)
       {
           cout<<"YES"<<endl;
       }
       else
       {
           cout<<"NO"<<endl;
       }

    }
}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t,i;
    cin>>t;
    for(i=0;i<t;i++)
    {  int n,m,j,k,a;
       cin>>n>>m;
       int arr[n],p[m];
       for(j=0;j<n;j++)
       {
           cin>>arr[j];
       }
       for(k=0;k<m;k++)
       {
           cin>>p[k];
       }
       sort(p,p+m);
       k=0;

       while(k<m)
       {
           if(k!=m-1)
           {

          if(p[k+1]-p[k]==1)
          {
            if(p[k]-1==0)
            {
              sort(arr+p[k]-1,arr+p[k+1]+1);
            }
            else
            {
               sort(arr+p[k]-1,arr+p[k+1]+1);

          }
          }

          else
          {
              if(arr[p[k]]<arr[p[k]-1])
              {
                  int temp=arr[p[k]];
                  arr[p[k]]=arr[p[k]-1];
                  arr[p[k]-1]=temp;
              }
          }
           }
           else
           {  if(arr[p[k]]<arr[p[k]-1])
              {
               int tp=arr[p[k]];
               arr[p[k]]=arr[p[k]-1];
               arr[p[k]-1]=tp;

              }

           }

          k++;
       }

      for(a=1;a<n;a++)
      {
          if(arr[a]<arr[a-1])
          break;

      }
      if(a==n)
      {
          cout<<"YES"<<endl;
      }
      else
      {
          cout<<"NO"<<endl;
      }

    }
}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t,i;
    cin>>t;
    for(i=0;i<t;i++)
    {  int n,m,j,k,a;
       cin>>n>>m;
       int arr[n],p[m];
       for(j=0;j<n;j++)
       {
           cin>>arr[j];
       }
       for(k=0;k<m;k++)
       {
           cin>>p[k];
       }
       sort(p,p+m);
       k=0;

       while(k<m)
       {
           if(k!=m-1)
           {
          if(p[k+1]-p[k]==1)
          {
            if(p[k]-1==0)
            {
              sort(arr+p[k]-1,arr+p[k+1]+1);
            }
            else
               sort(arr+p[k],arr+p[k+1]+1);

          }
          else
          {
              if(arr[p[k]]<arr[p[k]-1])
              {
                  int temp=arr[p[k]];
                  arr[p[k]]=arr[p[k]-1];
                  arr[p[k]-1]=temp;
              }
          }
           }
           else
           {  if(arr[p[k]]<arr[p[k]-1])
              {
               int tp=arr[p[k]];
               arr[p[k]]=arr[p[k]-1];
               arr[p[k]-1]=tp;

              }

           }

          k++;
       }

      for(a=1;a<n;a++)
      {
          if(arr[a]<arr[a-1])
          break;

      }
      if(a==n)
      {
          cout<<"YES"<<endl;
      }
      else
      {
          cout<<"NO"<<endl;
      }

    }
}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t,i;
    cin>>t;
    for(i=0;i<t;i++)
    {  int n,m,j,k,a;
       cin>>n>>m;
       int arr[n],p[m];
       for(j=0;j<n;j++)
       {
           cin>>arr[j];
       }
       for(k=0;k<m;k++)
       {
           cin>>m;
           p[m-1]=1;
       }
       int flag;
      while(true)
      {   flag=0;
       for(k=0;k<n-1;k++)
       {
           if(p[k]&&arr[k]>arr[k+1])
           {
               flag=1;
               swap(arr[k],arr[k+1]);
           }
       }
       if(!flag)
       break;
      }
       for(k=1;k<m;k++)
       {
           if(arr[k]<arr[k-1])
           break;
       }
       if(k==m)
       {
           cout<<"YES"<<endl;
       }
       else
       {
           cout<<"NO"<<endl;
       }

    }
}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t,i;
    cin>>t;
    for(i=0;i<t;i++)
    {  int n,m,j,k,a;
       cin>>n>>m;
       int arr[n],p[m];
       for(j=0;j<n;j++)
       {
           cin>>arr[j];
       }
       for(k=0;k<m;k++)
       {
           cin>>m;
           p[m-1]=1;
       }
       int flag;
      while(true)
      {   flag=0;
       for(k=0;k<n;k++)
       {
           if(p[k]&&arr[k]>arr[k+1])
           {
               flag=1;
               swap(arr[k],arr[k+1]);
           }
       }
       if(!flag)
       break;
      }
       for(k=1;k<m;k++)
       {
           if(arr[k]<arr[k-1])
           break;
       }
       if(k==m)
       {
           cout<<"YES"<<endl;
       }
       else
       {
           cout<<"NO"<<endl;
       }

    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<int> vec;
typedef vector< pair<int, int> > vecp;
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define Runner_up ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define sz(a) int((a).size())
#define all(a) (a).begin(), (a).end()
#define onbits(a) __builtin_popcountll(a);
//-----------------------------------------------------------------------------------
const ll N=107;
int main()
{
	Runner_up
	int t; cin>>t;
	while(t--){
		map <ll, ll> m,z;
		ll n,q; cin>>n>>q;
		ll a[n],p[q];
		ll b[n];
		for(int i=0; i<n; i++) {
			cin>>a[i];
			b[i]=a[i];
			m[a[i]]=i;
		}
		for(int i=0; i<q; i++) {
			cin>>p[i];
			p[i]--;
			z[p[i]]=1;
		}
		sort(b, b+n); int flag=1;
		for(int i=0; i<n; i++){
			if(i!=n-1 && b[i]!=b[i+1]){
					for(int j=i; j<m[b[i]]; j++){
						if(!z[j]){
							flag=0; break;
						}
					}
			}	if(!flag) break;
		}
		if(flag) cout<<"YES"<<endl; else cout<<"NO"<<endl;

	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<int> vec;
typedef vector< pair<int, int> > vecp;
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define Runner_up ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define sz(a) int((a).size())
#define all(a) (a).begin(), (a).end()
#define onbits(a) __builtin_popcountll(a);
//-----------------------------------------------------------------------------------
const ll N=107;
int main()
{
	Runner_up
	int t; cin>>t;
	while(t--){
		map <ll, ll> m;
		ll n,q; cin>>n>>q;
		ll a[n],p[q];
		ll b[n];
		for(int i=0; i<n; i++) {
			cin>>a[i];
			b[i]=a[i];
		}
		for(int i=0; i<q; i++) {
			cin>>p[i];
			p[i]--;
			m[p[i]]=1;
		}
		sort(b, b+n);
		int flag=1;
		ll cnt=0,yo=0;
	    ll z=a[0];
		for(int i=1; i<n; i++){
			if(a[i]!=b[i]) {
				if(!m[i] && !m[i-1]){
					flag=0; break;
				}else if(m[i] && !m[i-1]){
					if(z > b[i])
						{
							flag=0; break;
						}
				}
				cnt++;
			}
			z=max(z, a[i]);
		}
		if(a[0]!=b[0] && !m[0]) flag=0;
		if(flag) cout<<"YES"<<endl; else cout<<"NO"<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<int> vec;
typedef vector< pair<int, int> > vecp;
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define Runner_up ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define sz(a) int((a).size())
#define all(a) (a).begin(), (a).end()
#define onbits(a) __builtin_popcountll(a);
//-----------------------------------------------------------------------------------
const ll N=107;
int main()
{
	Runner_up
	int t; cin>>t;
	while(t--){
		map <ll, ll> m;
		ll n,q; cin>>n>>q;
		ll a[n],p[q];
		ll b[n];
		for(int i=0; i<n; i++) {
			cin>>a[i];
			b[i]=a[i];
		}
		for(int i=0; i<q; i++) {
			cin>>p[i];
			p[i]--;
			m[p[i]]=1;
		}
		ll pmax[n]={},smin[n];
		pmax[0]=a[0];
		for(int i=1; i<n; i++){
			pmax[i]=max(pmax[i-1], a[i]);
		}
		smin[n-1]=a[n-1];
		for(int i=n-2; i>=0; i--){
			smin[i]=min(smin[i+1], a[i]);
		}
	//	for(int i=0; i<n; i++) cout<<pmax[i]<<" "; cout<<endl;
	//	for(int i=0; i<n; i++) cout<<smin[i]<<" "; cout<<endl;
		sort(b, b+n);
		int flag=1;
		for(int i=1; i<n; i++){
			if(a[i]!=b[i]) {
				if(m[i-1]==0){
					if(pmax[i-1] > b[i] || pmax[i-1] > a[i] ){
						flag=0; break;
					}
					if(pmax[i] > b[i] || pmax[i] > a[i]){
						if(m[i]==0){
							flag=0; break;
						}
					}
				}
				if(m[i]==0){
					if(smin[i] > b[i]){
						flag=0; break;
					}
				}
			}
		}
		if(a[0]!=b[0] && !m[0]) flag=0;
		if(flag) cout<<"YES"<<endl; else cout<<"NO"<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<int> vec;
typedef vector< pair<int, int> > vecp;
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define Runner_up ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define sz(a) int((a).size())
#define all(a) (a).begin(), (a).end()
#define onbits(a) __builtin_popcountll(a);
//-----------------------------------------------------------------------------------
const ll N=107;
int main()
{
	Runner_up
	int t; cin>>t;
	while(t--){
		map <ll, ll> m;
		ll n,q; cin>>n>>q;
		ll a[n],p[q];
		ll b[n];
		for(int i=0; i<n; i++) {
			cin>>a[i];
			b[i]=a[i];
		}
		for(int i=0; i<q; i++) {
			cin>>p[i];
			p[i]--;
			m[p[i]]=1;
		}
		ll pmax[n],smin[n];
		pmax[0]=a[0];
		for(int i=1; i<n; i++){
			pmax[i]=max(pmax[i-1], a[i]);
		}
		smin[n-1]=a[n-1];
		for(int i=n-2; i>=0; i--){
			smin[i]=min(smin[i+1], a[i]);
		}
	//	for(int i=0; i<n; i++) cout<<pmax[i]<<" "; cout<<endl;
	//	for(int i=0; i<n; i++) cout<<smin[i]<<" "; cout<<endl;
		sort(b, b+n);
		int flag=1;
		ll cnt=0,yo=0;
		for(int i=1; i<n; i++){
			if(a[i]!=b[i]) {
				if(m[i-1]==0){
					if(pmax[i-1] > b[i]){
						flag=0; break;
					}
				}
			/*	if(m[i]==0){
					if(smin[i] < b[i]){
						flag=0; break;
					}
				}*/
			}
		}
		if(a[0]!=b[0] && !m[0]) flag=0;
		if(flag) cout<<"YES"<<endl; else cout<<"NO"<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<int> vec;
typedef vector< pair<int, int> > vecp;
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define Runner_up ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define sz(a) int((a).size())
#define all(a) (a).begin(), (a).end()
#define onbits(a) __builtin_popcountll(a);
//-----------------------------------------------------------------------------------
const ll N=107;
int main()
{
	Runner_up
	int t; cin>>t;
	while(t--){
		map <ll, ll> m;
		ll n,q; cin>>n>>q;
		ll a[n],p[q];
		ll b[n];
		for(int i=0; i<n; i++) {
			cin>>a[i];
			b[i]=a[i];
		}
		for(int i=0; i<q; i++) {
			cin>>p[i];
			p[i]--;
			m[p[i]]=1;
		}
		ll pmax[n]={},smin[n];
		pmax[0]=a[0];
		for(int i=1; i<n; i++){
			pmax[i]=max(pmax[i-1], a[i]);
		}
		smin[n-1]=a[n-1];
		for(int i=n-2; i>=0; i--){
			smin[i]=min(smin[i+1], a[i]);
		}
	//	for(int i=0; i<n; i++) cout<<pmax[i]<<" "; cout<<endl;
	//	for(int i=0; i<n; i++) cout<<smin[i]<<" "; cout<<endl;
		sort(b, b+n);
		int flag=1;
		int cnt=0,yo=0;
		for(int i=1; i<n; i++){
			if(a[i]!=b[i]) {
				if(m[i-1] || m[i]) yo++;
				if(m[i-1]==0){
					if(pmax[i-1] > b[i] || pmax[i-1] > a[i] ){
						flag=0; break;
					}
					if(pmax[i] > b[i] || pmax[i] > a[i]){
						if(m[i]==0){
							flag=0; break;
						}
					}
				}
				if(m[i]==0){
					if(smin[i] > b[i]){
						flag=0; break;
					}
				}
				cnt++;
			}
		}
		if(a[0]!=b[0] && !m[0]) flag=0;
		if(cnt-1 > q) flag=0;
		if(flag) cout<<"YES"<<endl; else cout<<"NO"<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<int> vec;
typedef vector< pair<int, int> > vecp;
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define Runner_up ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define sz(a) int((a).size())
#define all(a) (a).begin(), (a).end()
#define onbits(a) __builtin_popcountll(a);
//-----------------------------------------------------------------------------------
const ll N=107;
int main()
{
	Runner_up
	int t; cin>>t;
	while(t--){
		map <ll, ll> m;
		ll n,q; cin>>n>>q;
		ll a[n],p[q];
		ll b[n];
		for(int i=0; i<n; i++) {
			cin>>a[i];
			b[i]=a[i];
		}
		for(int i=0; i<q; i++) {
			cin>>p[i];
			p[i]--;
			m[p[i]]=1;
		}
		sort(b, b+n);
		int flag=1;
		ll cnt=0,yo=0;
	    ll z=a[0];
		for(int i=1; i<n; i++){
			if(a[i]!=b[i]) {
				if(!m[i] && !m[i-1]){
					flag=0; break;
				}else if(m[i] && !m[i-1]){
					if(z > b[i])
						{
							flag=0; break;
						}
				}
				cnt++;
			}
			z=max(z, a[i]);
		}
		if(flag) cout<<"YES"<<endl; else cout<<"NO"<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<int> vec;
typedef vector< pair<int, int> > vecp;
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define Runner_up ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define sz(a) int((a).size())
#define all(a) (a).begin(), (a).end()
#define onbits(a) __builtin_popcountll(a);
//-----------------------------------------------------------------------------------
const ll N=107;
int main()
{
	Runner_up
	int t; cin>>t;
	while(t--){
		map <ll, ll> m;
		ll n,q; cin>>n>>q;
		ll a[n],p[q];
		ll b[n];
		for(int i=0; i<n; i++) {
			cin>>a[i];
			b[i]=a[i];
		}
		for(int i=0; i<q; i++) {
			cin>>p[i];
			p[i]--;
			m[p[i]]=1;
		}
		ll pmax[n]={},smin[n];
		pmax[0]=a[0];
		for(int i=1; i<n; i++){
			pmax[i]=max(pmax[i-1], a[i]);
		}
		smin[n-1]=a[n-1];
		for(int i=n-2; i>=0; i--){
			smin[i]=min(smin[i+1], a[i]);
		}
	//	for(int i=0; i<n; i++) cout<<pmax[i]<<" "; cout<<endl;
	//	for(int i=0; i<n; i++) cout<<smin[i]<<" "; cout<<endl;
		sort(b, b+n);
		int flag=1;
		for(int i=1; i<n; i++){
			if(a[i]!=b[i]) {
				if(m[i-1]==0){
					if(pmax[i-1] > b[i]){
						flag=0; break;
					}
					if(pmax[i] > b[i]){
						if(m[i]==0){
							flag=0; break;
						}
					}
				}
				/*if(m[i]==0){
					if(smin[i] > b[i]){
						flag=0; break;
					}
				}*/
			}
		}
		if(a[0]!=b[0] && !m[0]) flag=0;
		if(flag) cout<<"YES"<<endl; else cout<<"NO"<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<int> vec;
typedef vector< pair<int, int> > vecp;
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define Runner_up ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define sz(a) int((a).size())
#define all(a) (a).begin(), (a).end()
#define onbits(a) __builtin_popcountll(a);
//-----------------------------------------------------------------------------------
const ll N=107;
int main()
{
	Runner_up
	int t; cin>>t;
	while(t--){
		map <ll, ll> m;
		ll n,q; cin>>n>>q;
		ll a[n],p[q];
		ll b[n];
		for(int i=0; i<n; i++) {
			cin>>a[i];
			b[i]=a[i];
		}
		for(int i=0; i<q; i++) {
			cin>>p[i];
			p[i]--;
			m[p[i]]=1;
		}
		sort(b, b+n);
		int flag=1;
		ll cnt=0,yo=0;
	    ll z=a[0];
		for(int i=1; i<n; i++){
			if(a[i]!=b[i]) {
				if(m[i]==0 && m[i-1]==0){
					flag=0; break;
				}else if(m[i] && m[i-1]==0){
					if(z >= b[i])
						{
							flag=0; break;
						}
				}
				cnt++;
			}
			z=max(z, a[i]);
		}
		if(a[0]!=b[0] && !m[0]) flag=0;
		if(flag) cout<<"YES"<<endl; else cout<<"NO"<<endl;
	}

	return 0;
}


#include <bits/stdc++.h>

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

#define endl "\n"
#define llu unsigned long long int
#define ll long long int
#define fore(i, n) for(auto i = 0; i < n ; i++)
#define forb(i, n) for(auto i = n-1; i >= 0; i++)
#define pair(k, z) pair<k, z>
#define vec(k) vector<k>
#define map(k, z) map<k, z>
#define set(k) set<k>
#define gcd(a, b) __gcd(a,b)
#define lcm(a,b) a*b/__gcd(a,b)
#define pb push_back
#define ins insert
using namespace std;

int main()
{
    fastio;
    int t;
    cin >> t;

    while(t--)
    {
        ll n, k, f = 1, temp = 0, c = 0;
        map<ll, ll> mape;
        cin >> n >> k;
        ll a[n], p[k], b[n];
        for(ll i = 0; i < n; i++)
        {
            cin >> a[i];
            b[i] = a[i];
        }
        sort(b, b+n);
        for(ll i = 0; i < k; i++)
        {
            cin >> p[i];
            mape[p[i]]++;
        }
        if(k == n-1)
            cout << "YES\n";
        else
        {
            for(auto x : mape)
            {
                if(c == 0)
                    temp = x.first - 1;
                else
                {
                    if(abs(temp - x.first) > 1)
                        sort(a+temp, a+x.first-1);
                    // for(ll i = 0; i < n; i++)
                    //     cout << a[i] << " ";
                    // cout << "\n";
                    temp = x.first-1;
                }
                c++;
            }
            sort(a+temp, a+temp+2);
            for(ll i = 1; i < n; i++)
            {
                if(a[i-1] > a[i])
                    f = 0;
            }
            if(!f)
                cout << "NO\n";
            else
                cout << "YES\n";
            // for(ll i = 0; i < n; i++)
            //     cout << a[i] << " ";
        }
    }
    return 0;
}


#include <bits/stdc++.h>

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

#define endl "\n"
#define llu unsigned long long int
#define ll long long int
#define fore(i, n) for(auto i = 0; i < n ; i++)
#define forb(i, n) for(auto i = n-1; i >= 0; i++)
#define pair(k, z) pair<k, z>
#define vec(k) vector<k>
#define map(k, z) map<k, z>
#define set(k) set<k>
#define gcd(a, b) __gcd(a,b)
#define lcm(a,b) a*b/__gcd(a,b)
#define pb push_back
#define ins insert
using namespace std;

int main()
{
    fastio;
    int t;
    cin >> t;

    while(t--)
    {
        ll n, k, f = 1, temp = 0, c = 0;
        map<ll, ll> mape;
        cin >> n >> k;
        ll a[n], p[k], b[n];
        for(ll i = 0; i < n; i++)
        {
            cin >> a[i];
            b[i] = a[i];
        }
        sort(b, b+n);
        for(ll i = 0; i < k; i++)
        {
            cin >> p[i];
            mape[p[i]]++;
        }
        if(k == n-1 || n == 1)
            cout << "YES\n";
        else
        {
            for(auto x : mape)
            {
                if(c == 0)
                    temp = x.first - 1;
                else
                {
                    if(abs(temp - x.first) > 1)
                        sort(a+temp, a+x.first);
                    temp = x.first-1;
                }
                c++;
            }
            sort(a+temp, a+temp+2);
            for(ll i = 1; i < n; i++)
            {
                if(a[i-1] > a[i])
                    f = 0;
            }
            if(!f)
                cout << "NO\n";
            else
                cout << "YES\n";
            // for(ll i = 0; i < n; i++)
            //     cout << a[i] << " ";
        }
    }
    return 0;
}


#include <bits/stdc++.h>

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

#define endl "\n"
#define llu unsigned long long int
#define ll long long int
#define fore(i, n) for(auto i = 0; i < n ; i++)
#define forb(i, n) for(auto i = n-1; i >= 0; i++)
#define pair(k, z) pair<k, z>
#define vec(k) vector<k>
#define map(k, z) map<k, z>
#define set(k) set<k>
#define gcd(a, b) __gcd(a,b)
#define lcm(a,b) a*b/__gcd(a,b)
#define pb push_back
#define ins insert
using namespace std;

int main()
{
    fastio;
    int t;
    cin >> t;

    while(t--)
    {
        ll n, k, f = 1, temp = 0, c = 0;
        map<ll, ll> mape;
        cin >> n >> k;
        ll a[n], p[k], b[n];
        for(ll i = 0; i < n; i++)
        {
            cin >> a[i];
            b[i] = a[i];
        }
        sort(b, b+n);
        for(ll i = 0; i < k; i++)
        {
            cin >> p[i];
            mape[p[i]]++;
        }
        if(k == n-1)
            cout << "YES\n";
        else
        {
            for(auto x : mape)
            {
                if(a[x.first - 1] > a[x.first])
                {
                    temp = a[x.first - 1];
                    a[x.first - 1] = a[x.first];
                    a[x.first] = temp;
                }
            }
            for(ll i = 1; i < n; i++)
            {
                if(a[i-1] > a[i])
                    f = 0;
            }
            if(!f)
                cout << "NO\n";
            else
                cout << "YES\n";
            // for(ll i = 0; i < n; i++)
            //     cout << a[i] << " ";
        }
    }
    return 0;
}


#include <bits/stdc++.h>

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

#define endl "\n"
#define llu unsigned long long int
#define ll long long int
#define fore(i, n) for(auto i = 0; i < n ; i++)
#define forb(i, n) for(auto i = n-1; i >= 0; i++)
#define pair(k, z) pair<k, z>
#define vec(k) vector<k>
#define map(k, z) map<k, z>
#define set(k) set<k>
#define gcd(a, b) __gcd(a,b)
#define lcm(a,b) a*b/__gcd(a,b)
#define pb push_back
#define ins insert
using namespace std;

int main()
{
    fastio;
    int t;
    cin >> t;

    while(t--)
    {
        ll n, k, f = 1, temp = 0, c = 0;
        map<ll, ll> mape;
        cin >> n >> k;
        ll a[n], p[k], b[n];
        for(ll i = 0; i < n; i++)
        {
            cin >> a[i];
            b[i] = a[i];
        }
        sort(b, b+n);
        for(ll i = 0; i < k; i++)
        {
            cin >> p[i];
            mape[p[i]]++;
        }
        if(k == n-1)
            cout << "YES\n";
        else
        {
            for (ll i = 0; i < n ; i++)
            {
                for(auto x : mape)
                {
                    if(a[x.first - 1] > a[x.first] && x.first != n)
                    {
                        temp = a[x.first - 1];
                        a[x.first - 1] = a[x.first];
                        a[x.first] = temp;
                    }
                }
            }
            for(ll i = 1; i < n; i++)
            {
                if(a[i-1] > a[i])
                    f = 0;
            }
            if(!f)
                cout << "NO\n";
            else
                cout << "YES\n";
        }
    }
    return 0;
}


#include <bits/stdc++.h>

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

#define endl "\n"
#define llu unsigned long long int
#define ll long long int
#define fore(i, n) for(auto i = 0; i < n ; i++)
#define forb(i, n) for(auto i = n-1; i >= 0; i++)
#define pair(k, z) pair<k, z>
#define vec(k) vector<k>
#define map(k, z) map<k, z>
#define set(k) set<k>
#define gcd(a, b) __gcd(a,b)
#define lcm(a,b) a*b/__gcd(a,b)
#define pb push_back
#define ins insert
using namespace std;

int main()
{
    fastio;
    int t;
    cin >> t;

    while(t--)
    {
        ll n, k, f = 1, temp = 0, c = 0;
        map<ll, ll> mape;
        cin >> n >> k;
        ll a[n], p[k], b[n];
        for(ll i = 0; i < n; i++)
        {
            cin >> a[i];
            b[i] = a[i];
        }
        sort(b, b+n);
        for(ll i = 0; i < k; i++)
        {
            cin >> p[i];
            mape[p[i]]++;
        }
        if(k == n-1)
            cout << "YES\n";
        else
        {
            for(auto x : mape)
            {
                if(c == 0)
                    temp = x.first - 1;
                else
                {
                    if(abs(temp - x.first) > 1)
                        sort(a+temp, a+x.first);
                    temp = x.first-1;
                }
                c++;
            }
            sort(a+temp, a+temp+2);
            for(ll i = 1; i < n; i++)
            {
                if(a[i-1] > a[i])
                    f = 0;
            }
            if(!f)
                cout << "NO\n";
            else
                cout << "YES\n";
            // for(ll i = 0; i < n; i++)
            //     cout << a[i] << " ";
        }
    }
    return 0;
}


#include <bits/stdc++.h>

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

#define endl "\n"
#define llu unsigned long long int
#define ll long long int
#define fore(i, n) for(auto i = 0; i < n ; i++)
#define forb(i, n) for(auto i = n-1; i >= 0; i++)
#define pair(k, z) pair<k, z>
#define vec(k) vector<k>
#define map(k, z) map<k, z>
#define set(k) set<k>
#define gcd(a, b) __gcd(a,b)
#define lcm(a,b) a*b/__gcd(a,b)
#define pb push_back
#define ins insert
using namespace std;

int main()
{
    fastio;
    int t;
    cin >> t;

    while(t--)
    {
        ll n, k, f = 1, temp = 0, c = 0;
        map<ll, ll> mape;
        cin >> n >> k;
        ll a[n], p[k];
        for(ll i = 0; i < n; i++)
        {
            cin >> a[i];
        }
        for(ll i = 0; i < k; i++)
        {
            cin >> p[i];
            mape[p[i]]++;
        }
        if(k == n-1 || n == 1)
            cout << "YES\n";
        else
        {
            for(auto x : mape)
            {
                if(c == 0)
                    temp = x.first - 1;
                else
                {
                    if(abs(temp - x.first) > 1)
                    {
                        // cout << temp << " " << x.first;
                        sort(a+temp, a+x.first);
                    }
                    temp = x.first-1;
                }
                c++;
            }
            sort(a+temp, a+temp+2);
            for(ll i = 1; i < n; i++)
            {
                if(a[i-1] > a[i])
                    f = 0;
            }
            if(!f)
                cout << "NO\n";
            else
                cout << "YES\n";
            for(ll i = 0; i < n; i++)
                cout << a[i] << " ";
            cout << "\n";
        }
    }
    return 0;
}


#include <iostream>
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int i,j,n,m,x,p=0;
        cin>>n;
        cin>>m;
        int a[n];
        int b[n]={0};
        for(i=0;i<n;i++)
        {
            cin>>a[i];
        }
        for(i=0;i<m;i++)
        {
           cin>>x;
          b[x-1]++;
        }
        for(i=1;i<n;i++)
        {
           for(j=0;j<i;j++)
           {
               if(a[j]>a[i]&&b[j]==0)
               {
                   q++;
               }
           }
        }
        if(p==0)
        {
            cout<<"YES"<<"\n";
        }
        else
        {
            cout<<"NO"<<"\n";
        }
    }

    return 0;
}


#include <iostream>
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int i,j,n,m,x,p=0;
        cin>>n;
        cin>>m;
        int a[n];
        int b[n]={0};
        for(i=0;i<n;i++)
        {
            cin>>a[i];
        }
        for(i=0;i<m;i++)
        {
           cin>>x;
          b[x-1]++;
        }
        for(i=1;i<n;i++)
        {
           for(j=0;j<i;j++)
           {
               if(a[j]>a[i]&&b[j]==0)
               {
                   p++;
               }
           }
        }
        if(p==0)
        {
            cout<<"YES"<<"\n";
        }
        else
        {
            cout<<"NO"<<"\n";
        }
    }

    return 0;
}


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

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int i,n,m,temp,q=0;
        cin>>n;
        cin>>m;
        int a[n];
        int p[m];
        for(i=0;i<n;i++)
        {
            cin>>a[i];
        }
        for(i=0;i<m;i++)
        {
            cin>>p[i];
            p[i]--;
        }
        sort(p,p+m);
        for(i=m-1;i>=0;i--)
        {
            if(a[p[i]]>a[p[i]+1])
            {
                temp=a[p[i]];
                a[p[i]]=a[p[i]+1];
                a[p[i]+1]=temp;
            }
        }
        for(i=0;i<(n-1);i++)
        {
            if(a[i]>a[i+1])
            {
                q++;
            }
        }
        if(q==0)
        {
            cout<<"YES"<<"\n";
        }
        else
        {
            cout<<"NO"<<"\n";
        }
    }

    return 0;
}


#include <iostream>
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int i,n,m,x,p=0;
        cin>>n;
        cin>>m;
        int a[n];
        int b[n]={0};
        for(i=0;i<n;i++)
        {
            cin>>a[i];
        }
        for(i=0;i<m;i++)
        {
           cin>>x;
          b[x-1]++;
        }
        for(i=1;i<n;i++)
        {
            if(a[i]<a[i-1]&&b[i-1]==0)
            {
                p++;
            }
        }
        if(p==0)
        {
            cout<<"YES"<<"\n";
        }
        else
        {
            cout<<"NO"<<"\n";
        }
    }

    return 0;
}


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

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int i,j,n,m,q=0;
        cin>>n;
        cin>>m;
        int a[n];
        int p[m];
        for(i=0;i<n;i++)
        {
            cin>>a[i];
        }
        for(i=0;i<m;i++)
        {
            cin>>p[i];
        }
        sort(p,p+m);
        i=0;
        while(i<(m-1))
        {
            for(j=p[i]+1;j<p[i+1];j++)
            {
                if(a[j]>a[j+1])
                {
                    q++;
                }
            }
        }
        if(q==0)
        {
            cout<<"YES"<<"\n";
        }
        else
        {
            cout<<"NO"<<"\n";
        }

    }

    return 0;
}


#include <iostream>
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int i,n,m,x,p=0;
        cin>>n;
        cin>>m;
        int a[n];
        int b[n]={0};
        for(i=0;i<n;i++)
        {
            cin>>a[i];
        }
        for(i=0;i<m;i++)
        {
           cin>>x;
          b[x-1]++;
        }
        for(i=1;i<(n-1);i++)
        {
            if(a[i]<a[i-1]&&b[i-1]==0)
            {
                p++;
            }
        }
        if(p==0)
        {
            cout<<"YES"<<"\n";
        }
        else
        {
            cout<<"NO"<<"\n";
        }
    }

    return 0;
}


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

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int i,n,m,q=0,p=0;
        unordered_map<int,int> mp;
        cin>>n;
        cin>>m;
        int a[n];
        int p[m];
        for(i=0;i<n;i++)
        {
            cin>>a[i];
        }
        for(i=0;i<m;i++)
        {
            cin>>p[i];
            mp[p[i]-1]++;
        }
        for(i=0;i<(n-1);i++)
        {
            if(a[i]>a[i+1])
            {
                if(mp.find(i)==mp.end())
                {
                    q++;
                }
                else
                {
                    q==0;
                }
                if(q==2)
                {
                    break;
                }
            }
            else
            {
                if(q==1)
                {
                    break;
                }

            }

        }
        if(q==1||q==2)
        {
            cout<<"NO"<<"\n";
        }
        else
        {
            cout<<"YES"<<"\n";
        }

    }

    return 0;
}


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

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int i,n,m,x,p=0;
        cin>>n;
        cin>>m;
        int a[n],c[n];
        int b[n]={0};
        for(i=0;i<n;i++)
        {
            cin>>a[i];
            c[i]={a[i]};
        }
        for(i=0;i<m;i++)
        {
           cin>>x;
          b[x-1]++;
        }
        sort(c,c+n);
        for(i=1;i<n;i++)
        {
           if(a[i]!=c[i]&&b[i-1]==0)
           {
               p++;
           }
        }
        if(p==0)
        {
            cout<<"YES"<<"\n";
        }
        else
        {
            cout<<"NO"<<"\n";
        }
    }

    return 0;
}


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

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int i,n,m,q=0;
        unordered_map<int,int> mp;
        cin>>n;
        cin>>m;
        int a[n];
        int p[m];
        for(i=0;i<n;i++)
        {
            cin>>a[i];
        }
        for(i=0;i<m;i++)
        {
            cin>>p[i];
            mp[p[i]-1]++;
        }
        for(i=0;i<(n-1);i++)
        {
            if(a[i]>a[i+1])
            {
                if(mp.find(i)==mp.end())
                {
                    q++;
                }
                else
                {
                    q==0;
                }
                if(q==2)
                {
                    break;
                }
            }
            else
            {
                if(q==1)
                {
                    break;
                }

            }

        }
        if(q==1||q==2)
        {
            cout<<"NO"<<"\n";
        }
        else
        {
            cout<<"YES"<<"\n";
        }

    }

    return 0;
}


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

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int i,n,m,q=0;
        unordered_map<int,int> mp;
        cin>>n;
        cin>>m;
        int a[n];
        int p[m];
        for(i=0;i<n;i++)
        {
            cin>>a[i];
        }
        for(i=0;i<m;i++)
        {
            cin>>p[i];
            mp[p[i]-1]++;
        }
        for(i=0;i<(n-1);i++)
        {
            if(a[i]>a[i+1])
            {
                if(mp.find(i)==mp.end())
                {
                    q++;
                }
                else
                {
                    q==0;
                }
                if(q==2)
                {
                    break;
                }
            }
            else
            {
                if(q==1)
                {
                    break;
                }
                else
                {
                    q++;
                }
            }

        }
        if(q==1||q==2)
        {
            cout<<"NO"<<"\n";
        }
        else
        {
            cout<<"YES"<<"\n";
        }

    }

    return 0;
}


#include <iostream>
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int i,n,m,x,p=0;
        cin>>n;
        cin>>m;
        int a[n];
        int b[n]={0};
        for(i=0;i<n;i++)
        {
            cin>>a[i];
        }
        for(i=0;i<m;i++)
        {
           cin>>x;
          b[x-1]++;
        }
        for(i=1;i<n;i++)
        {
           for(j=0;j<i;j++)
           {
               if(a[j]>a[i]&&b[j]==0)
               {
                   q++;
               }
           }
        }
        if(p==0)
        {
            cout<<"YES"<<"\n";
        }
        else
        {
            cout<<"NO"<<"\n";
        }
    }

    return 0;
}


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

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int i,j,n,m,q=0;
        cin>>n;
        cin>>m;
        int a[n];
        int p[m];
        for(i=0;i<n;i++)
        {
            cin>>a[i];
        }
        for(i=0;i<m;i++)
        {
            cin>>p[i];
        }
        sort(p,p+m);
        i=0;
        while(i<(m-1))
        {
            for(j=p[i]+1;j<p[i+1];j++)
            {
                if(a[j]>a[j+1])
                {
                    q++;
                }
            }
            i++;
        }
        if(q==0)
        {
            cout<<"YES"<<"\n";
        }
        else
        {
            cout<<"NO"<<"\n";
        }

    }

    return 0;
}


#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
int n, m, t, i, j, z, b[105], x,f;
int a[105];
main(){
    cin >> t;
    for (i= 1; i<=t; ++i){
        cin >> n >> m;
        for (j = 1; j<=n; ++j) {cin >> a[j];}
        for (j = 1; j<105; ++j) {b[j]=0;}
        for (j = 1; j<=m; ++j) {cin >> x; b[x]++;}
        f = 0;
        for(j = 1; j<=n; ++j)
            for(z = 1; z<n; ++z)
                if (a[z]>a[z+1]){
                    if (b[z]==0) f = 1;
                    swap(a[z], a[z+1]);
                }
        if(f) cout <<"NO\n";
        else cout <<"YES\n";
    }
}


#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
int n, m, t, i, j, z, b[105], x;
int a[105];
main(){
    cin >> t;
    for (i= 1; i<=t; ++i){
        cin >> n >> m;
        for (j = 1; j<=n; ++j) {cin >> a[j];}
        for (j = 1; j<=m; ++j) {b[j]=0;}
        for (j = 1; j<=m; ++j) {cin >> x; b[x]++;}
int f = 0;
        for(j = 1; j<=n+n; ++j)
            for(z = 1; z<n; ++z)
                if (a[z]>a[z+1]){
                    if (b[z]==0) f = 1;
                    else swap(a[z], a[z+1]);
                }
        for (j = 1; j<n; ++j)
            if (a[j]>a[j+1]) ++f;
        if(f) cout <<"NO\n";
        else cout <<"YES\n";
    }
}


#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
int n, m, t, i, j, z, b[105], x;
pair<int,int> a[105];
main(){
    cin >> t;
    for (i= 1; i<=t; ++i){
        cin >> n >> m;
        for (j = 1; j<=n; ++j) {cin >> a[j].first; a[j].second = j;}
        for (j = 1; j<=m; ++j) {b[j]=0;}
        for (j = 1; j<=m; ++j) {cin >> x; b[x]++;}

        sort(a+1, a+n+1);
        int f=0;
        for (j = 1; j<=n; ++j){
                int cnt= 0;
            for (z = min(j, a[j].second); z<max(j, a[j].second); ++z)
                if (b[z]) cnt++;
            //cout << a[j].first<<" "<<cnt<<" "<< abs(a[j].second - j)<<"\n";
            if (cnt!= abs(a[j].second - j)) f=1;
        }
        if(f) cout <<"NO\n";
        else cout <<"YES\n";
    }
}


#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
int n, m, t, i, j, z, b[105], x;
pair<int,int> a[105];
main(){
    cin >> t;
    for (i= 1; i<=t; ++i){
        cin >> n >> m;
        for (j = 1; j<=n; ++j) {cin >> a[j].first; a[j].second = j;}
        for (j = 1; j<=m; ++j) {b[j]=0;}
        for (j = 1; j<=m; ++j) {cin >> x; b[x]++;}

        sort(a+1, a+n+1);
        int f=0;
        for (j = 1; j<=n; ++j){
                int cnt= 0;
            for (z = min(j, a[j].second); z<max(j, a[j].second); ++z)
                if (b[z]) cnt++;
            cout << a[j].first<<" "<<cnt<<" "<< abs(a[j].second - j)<<"\n";
            if (cnt!= abs(a[j].second - j)) f=1;
        }
        if(f) cout <<"NO\n";
        else cout <<"YES\n";
    }
}


#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
int n, m, t, i, j, z, b[105], x;
int a[105];
main(){
    cin >> t;
    for (i= 1; i<=t; ++i){
        cin >> n >> m;
        for (j = 1; j<=n; ++j) {cin >> a[j];}
        for (j = 1; j<=m; ++j) {b[j]=0;}
        for (j = 1; j<=m; ++j) {cin >> x; b[x]++;}
int f = 0;
        for(j = 1; j<=n+n; ++j)
            for(z = 1; z<n; ++z)
                if (a[z]>a[z+1]){
                    if (b[z]==0) f = 1;
                    swap(a[z], a[z+1]);
                }
        if(f) cout <<"NO\n";
        else cout <<"YES\n";
    }
}


#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
int n, m, t, i, j, z, b[105], x;
int a[105];
main(){
    cin >> t;
    for (i= 1; i<=t; ++i){
        cin >> n >> m;
        for (j = 1; j<=n; ++j) {cin >> a[j];}
        for (j = 1; j<=m; ++j) {b[j]=0;}
        for (j = 1; j<=m; ++j) {cin >> x; b[x]++;}
int f = 0;
        for(j = 1; j<=n; ++j)
            for(z = 1; z<n; ++z)
                if (a[z]>a[z+1]){
                    if (b[z]==0) f = 1;
                    swap(a[z], a[z+1]);
                }
        if(f) cout <<"NO\n";
        else cout <<"YES\n";
    }
}