#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp> // Common file
//#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace std;
//using namespace __gnu_pbds;
//typedef tree<
// pair<long long, int>, // change type
// null_type,
// less<pair<long long, int> >, // change type
// rb_tree_tag,
// tree_order_statistics_node_update>
// ordered_set;
typedef long long ll;
#define rep(i, start, end) for(int i = start; i < end; ++i)
#define sz(x) (int)(x).size()
#define pb push_back
#define row first
#define col second
#define all(x) x.begin(), x.end()
#define clr(d, v) memset(d, v, sizeof(d))
#define pii pair<int, int>
const double PI = 3.14159265358979323846 ;
const double eps = ( 1e-4 ) ;
int dcmp( long double x, long double y)
{
if ( abs ( x - y) < eps)
return 0 ;
if ( x > y)
return 1 ;
return - 1 ;
}
map< int , int > after;
int n, m;
const int MAX_N = 2005 ;
ll mem[ MAX_N] [ MAX_N] ;
int visit[ MAX_N] [ MAX_N] , vid;
int a[ MAX_N] , b[ MAX_N] , c[ MAX_N] , cpr[ 2 * MAX_N] ;
bool existL[ MAX_N] [ MAX_N] , existR[ MAX_N] [ MAX_N] ;
ll solve( int i, int j)
{
if ( i == n && j == m)
return 0 ;
if ( i == n)
return ( 1LL<< 50 ) ;
ll & ret = mem[ i] [ j] ;
if ( ret ! = - 1 )
return ret;
visit[ i] [ j] = vid;
ret = ( 1LL<< 50 ) ;
// match
if ( j < m && a[ i] == b[ j] )
ret = min( ret, solve( i + 1 , j + 1 ) ) ;
// delete with no left
if ( ! existL[ j] [ a[ i] ] )
ret = min( ret, solve( i + 1 , j) + c[ i] ) ;
// delete with no right
if ( ! existR[ j] [ a[ i] ] )
ret = min( ret, solve( i + 1 ,j) + c[ i] ) ;
return ret;
}
void clear( )
{
++ vid;
clr( mem, - 1 ) ;
clr( existL, 0 ) ;
clr( existR, 0 ) ;
after.clear ( ) ;
}
int main( )
{
ios_base:: sync_with_stdio ( false ) ;
cin .tie ( 0 ) ;
cout .tie ( 0 ) ;
#ifndef ONLINE_JUDGE
freopen ( "input.txt" , "r" , stdin ) ;
// freopen("facebook.txt", "w", stdout);
#endif
freopen ( "transform.in" , "r" , stdin ) ;
int tc;
cin >> tc;
while ( tc-- )
{
cin >> n >> m;
clear( ) ;
int cnt = 0 ;
rep( i,0 ,n)
{
cin >> a[ i] ;
cpr[ cnt++ ] = a[ i] ;
}
rep( i,0 ,m)
{
cin >> b[ i] ;
cpr[ cnt++ ] = b[ i] ;
}
rep( i,0 ,n)
cin >> c[ i] ;
sort( cpr, cpr + cnt) ;
int id = 0 ;
for ( int i = 0 ; i < cnt; ++ i)
{
auto it = after.find ( cpr[ i] ) ;
if ( it == after.end ( ) )
after[ cpr[ i] ] = id++ ;
}
for ( int i = 0 ; i < n; ++ i)
a[ i] = after[ a[ i] ] ;
for ( int i = 0 ; i < m; ++ i)
b[ i] = after[ b[ i] ] ;
for ( int i = 0 ; i < m; ++ i)
{
for ( int j = i + 1 ; j <= m; ++ j)
existL[ j] [ b[ i] ] = 1 ;
}
for ( int i = m - 1 ; i >= 0 ; -- i)
{
for ( int j = i; j >= 0 ; -- j)
existR[ j] [ b[ i] ] = 1 ;
}
ll ans = solve( 0 , 0 ) ;
if ( ans >= ( 1LL<< 49 ) )
cout << "No" ;
else
cout << ans;
cout << '\n ' ;
}
return 0 ;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KLy8jaW5jbHVkZSA8ZXh0L3BiX2RzL2Fzc29jX2NvbnRhaW5lci5ocHA+IC8vIENvbW1vbiBmaWxlCi8vI2luY2x1ZGUgPGV4dC9wYl9kcy90cmVlX3BvbGljeS5ocHA+IC8vIEluY2x1ZGluZyB0cmVlX29yZGVyX3N0YXRpc3RpY3Nfbm9kZV91cGRhdGUKCnVzaW5nIG5hbWVzcGFjZSBzdGQ7Ci8vdXNpbmcgbmFtZXNwYWNlIF9fZ251X3BiZHM7Ci8vdHlwZWRlZiB0cmVlPAovLyAgICAgICAgcGFpcjxsb25nIGxvbmcsIGludD4sIC8vIGNoYW5n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bTUFYX05dW01BWF9OXSwgZXhpc3RSW01BWF9OXVtNQVhfTl07CgoKbGwgc29sdmUoaW50IGksIGludCBqKQp7CiAgICBpZiAoaSA9PSBuICYmIGogPT0gbSkKICAgICAgICByZXR1cm4gMDsKICAgIGlmIChpID09IG4pCiAgICAgICAgcmV0dXJuICgxTEw8PDUwKTsKICAgIGxsICZyZXQgPSBtZW1baV1bal07CiAgICBpZiAocmV0ICE9IC0xKQogICAgICAgIHJldHVybiByZXQ7CiAgICB2aXNpdFtpXVtqXSA9IHZpZDsKICAgIHJldCA9ICgxTEw8PDUwKTsKICAgIC8vIG1hdGNoCiAgICBpZiAoaiA8IG0gJiYgYVtpXSA9PSBiW2pdKQogICAgICAgIHJldCA9IG1pbihyZXQsIHNvbHZlKGkgKyAxLCBqICsgMSkpOwogICAgLy8gZGVsZXRlIHdpdGggbm8gbGVmdAogICAgaWYgKCFleGlzdExbal1bYVtpXV0pCiAgICAgICAgcmV0ID0gbWluKHJldCwgc29sdmUoaSArIDEsIGopICsgY1tpXSk7CiAgICAvLyBkZWxldGUgd2l0aCBubyByaWdodAogICAgaWYgKCFleGlzdFJbal1bYVtpXV0pCiAgICAgICAgcmV0ID0gbWluKHJldCwgc29sdmUoaSArIDEsaikgKyBjW2ldKTsKICAgIHJldHVybiByZXQ7Cn0KCgp2b2lkIGNsZWFyKCkKewogICAgKyt2aWQ7CiAgICBjbHIobWVtLCAtMSk7CiAgICBjbHIoZXhpc3RMLCAwKTsKICAgIGNscihleGlzdFIsIDApOwogICAgYWZ0ZXIuY2xlYXIoKTsKfQoKaW50IG1haW4oKQp7CiAgICBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKGZhbHNlKTsKICAgIGNpbi50aWUoMCk7CiAgICBjb3V0LnRpZSgwKTsKI2lmbmRlZiBPTkxJTkVfSlVER0UKICAgIGZyZW9wZW4oImlucHV0LnR4dCIsICJyIiwgc3RkaW4pOwovLyAgICBmcmVvcGVuKCJmYWNlYm9vay50eHQiLCAidyIsIHN0ZG91dCk7CiNlbmRpZgogICAgZnJlb3BlbigidHJhbnNmb3JtLmluIiwgInIiLCBzdGRpbik7CiAgICBpbnQgdGM7CiAgICBjaW4gPj4gdGM7CiAgICB3aGlsZSh0Yy0tKQogICAgewogICAgICAgIGNpbiA+PiBuID4+IG07CiAgICAgICAgY2xlYXIoKTsKICAgICAgICBpbnQgY250ID0gMDsKICAgICAgICByZXAoaSwwLG4pCiAgICAgICAgewogICAgICAgICAgICBjaW4gPj4gYVtpXTsKICAgICAgICAgICAgY3ByW2NudCsrXSA9IGFbaV07CiAgICAgICAgfQogICAgICAgIHJlcChpLDAsbSkKICAgICAgICB7CiAgICAgICAgICAgIGNpbiA+PiBiW2ldOwogICAgICAgICAgICBjcHJbY250KytdID0gYltpXTsKICAgICAgICB9CiAgICAgICAgcmVwKGksMCxuKQogICAgICAgICAgICBjaW4gPj4gY1tpXTsKCiAgICAgICAgc29ydChjcHIsIGNwciArIGNudCk7CiAgICAgICAgaW50IGlkID0gMDsKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IGNudDsgKytpKQogICAgICAgIHsKICAgICAgICAgICAgYXV0byBpdCA9IGFmdGVyLmZpbmQoY3ByW2ldKTsKICAgICAgICAgICAgaWYgKGl0ID09IGFmdGVyLmVuZCgpKQogICAgICAgICAgICAgICAgYWZ0ZXJbY3ByW2ldXSA9IGlkKys7CiAgICAgICAgfQoKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IG47ICsraSkKICAgICAgICAgICAgYVtpXSA9IGFmdGVyW2FbaV1dOwogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbTsgKytpKQogICAgICAgICAgICBiW2ldID0gYWZ0ZXJbYltpXV07CgoKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IG07ICsraSkKICAgICAgICB7CiAgICAgICAgICAgIGZvciAoaW50IGogPSBpICsgMTsgaiA8PSBtOyArK2opCiAgICAgICAgICAgICAgICBleGlzdExbal1bYltpXV0gPSAxOwogICAgICAgIH0KCiAgICAgICAgZm9yIChpbnQgaSA9IG0gLSAxOyBpID49IDA7IC0taSkKICAgICAgICB7CiAgICAgICAgICAgIGZvciAoaW50IGogPSBpOyBqID49IDA7IC0taikKICAgICAgICAgICAgICAgIGV4aXN0UltqXVtiW2ldXSA9IDE7CiAgICAgICAgfQoKICAgICAgICBsbCBhbnMgPSBzb2x2ZSgwLCAwKTsKICAgICAgICBpZiAoYW5zID49ICgxTEw8PDQ5KSkKICAgICAgICAgICAgY291dCA8PCAiTm8iOwogICAgICAgIGVsc2UKICAgICAgICAgICAgY291dCA8PCBhbnM7CiAgICAgICAgY291dCA8PCAnXG4nOwogICAgfQoKICAgIHJldHVybiAwOwp9