//Written by rraj001 :)
#include<bits/stdc++.h>
#define ll int
#define inf 1000000000
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
using namespace std;
vector<ll>adj[112];
vector<ll>d[112];
vector<ll>money[112];
ll budget,n,r,N;
ll dp[112][100011];
ll cal(ll city,ll budget)
{
ll& ans=dp[city][budget];
if(ans==-1)
{
if(city==N)ans=0;
else
{
ans=inf;
for(ll i=0;i<adj[city].size();i++)
{
if(money[city][i]<=budget)
{
ans=min(ans,d[city][i]+cal(adj[city][i],budget-money[city][i]));
}
}
}
}
return ans;
}
int main()
{
//freopen("input.txt","r",stdin);
std::ios::sync_with_stdio(false);
ll tc;
cin>>tc;
while(tc--)
{
cin>>budget>>n>>r;
for(ll i=0;i<n;i++){adj[i].clear();d[i].clear();money[i].clear();}
for(ll i=0;i<r;i++)
{
ll src,dest,len,t;
cin>>src>>dest>>len>>t;
src--;
dest--;
adj[src].pb(dest);
d[src].pb(len);
money[src].pb(t);
}
N=n-1;
memset(dp,-1,sizeof(dp));
ll ans=cal(0,budget);
cout << (ans == inf? -1 : ans) << endl;
}
}
Ly9Xcml0dGVuIGJ5IHJyYWowMDEgOikKI2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KI2RlZmluZSBsbCBpbnQKI2RlZmluZSBpbmYgMTAwMDAwMDAwMAojZGVmaW5lIG1wIG1ha2VfcGFpcgojZGVmaW5lIHBiIHB1c2hfYmFjawojZGVmaW5lIGZmIGZpcnN0CiNkZWZpbmUgc3Mgc2Vjb25kCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnZlY3RvcjxsbD5hZGpbMTEyXTsKdmVjdG9yPGxsPmRbMTEyXTsKdmVjdG9yPGxsPm1vbmV5WzExMl07CmxsIGJ1ZGdldCxuLHIsTjsKbGwgZHBbMTEyXVsxMDAwMTFdOwpsbCBjYWwobGwgY2l0eSxsbCBidWRnZXQpCnsKICAgIGxsJiBhbnM9ZHBbY2l0eV1bYnVkZ2V0XTsKICAgIGlmKGFucz09LTEpCiAgICB7CiAgICAgICAgaWYoY2l0eT09TilhbnM9MDsKICAgICAgICBlbHNlCiAgICAgICAgewogICAgICAgICAgICBhbnM9aW5mOwogICAgICAgICAgICBmb3IobGwgaT0wO2k8YWRqW2NpdHldLnNpemUoKTtpKyspCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGlmKG1vbmV5W2NpdHldW2ldPD1idWRnZXQpCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgYW5zPW1pbihhbnMsZFtjaXR5XVtpXStjYWwoYWRqW2NpdHldW2ldLGJ1ZGdldC1tb25leVtjaXR5XVtpXSkpOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAKICAgICAgICB9CiAgICB9CiAgICByZXR1cm4gYW5zOwp9CiAKIAogCiAKIAogCiAKIAoKaW50IG1haW4oKQp7CiAgICAvL2ZyZW9wZW4oImlucHV0LnR4dCIsInIiLHN0ZGluKTsKICAgIHN0ZDo6aW9zOjpzeW5jX3dpdGhfc3RkaW8oZmFsc2UpOwogICAgbGwgdGM7CiAgICBjaW4+PnRjOwogICAgd2hpbGUodGMtLSkKICAgIHsKICAgICAgICBjaW4+PmJ1ZGdldD4+bj4+cjsKICAgICAgICBmb3IobGwgaT0wO2k8bjtpKyspe2FkaltpXS5jbGVhcigpO2RbaV0uY2xlYXIoKTttb25leVtpXS5jbGVhcigpO30KICAgICAgICBmb3IobGwgaT0wO2k8cjtpKyspCiAgICAgICAgewogICAgICAgICAgICBsbCBzcmMsZGVzdCxsZW4sdDsKICAgICAgICAgICAgY2luPj5zcmM+PmRlc3Q+Pmxlbj4+dDsKICAgICAgICAgICAgc3JjLS07CiAgICAgICAgICAgIGRlc3QtLTsKICAgICAgICAgICAgYWRqW3NyY10ucGIoZGVzdCk7CiAgICAgICAgICAgIGRbc3JjXS5wYihsZW4pOwogICAgICAgICAgICBtb25leVtzcmNdLnBiKHQpOwogICAgICAgIH0KICAgICAgICBOPW4tMTsKICAgICAgICBtZW1zZXQoZHAsLTEsc2l6ZW9mKGRwKSk7CiAgICAgICAgbGwgYW5zPWNhbCgwLGJ1ZGdldCk7CiAgICAgICAgY291dCA8PCAoYW5zID09IGluZj8gLTEgOiBhbnMpIDw8IGVuZGw7CiAKIAogICAgfQogCn0=