#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <cmath>
#include <iomanip>

using namespace std;
using ll = long long;
using ld = long double;

#define int ll

const int MOD = 998'244'353;
const int SIGNES = 6;
const int INF = 1e9;

void gen(int i, int k, vector<int> &a, vector<int> &b, int sumb, int last_time, int cur, int &ans, int &last, vector<int> &prefsum) {
    if (i == k) {
        if (cur > ans) {
            ans = cur;
        }
        return;
    }
    sumb -= b[i];
    int s = prefsum[last] - (i > 0? prefsum[i - 1]: 0);
    if (b[i] < 0) {
        gen(i + 1, k, a, b, sumb, last_time, cur + a[i] + b[i] * last_time, ans, last, prefsum);
    }
    else {
        gen(i + 1, k, a, b, sumb, i, cur + a[i] + i * b[i], ans, last, prefsum);
        if (s * i < s * last_time) {
            gen(i + 1, k, a, b, sumb, last_time, cur + a[i] + b[i] * last_time, ans, last, prefsum);
        } 
    }
} 

void solve() {
    int n;
    cin >> n;
    vector<int> a(n);
    vector<int> b(n);
    vector<int> p(n);
    bool les0 = true;
    bool bigger0 = true;
    for (int i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
        if (b[i] > 0) {
            les0 = false;
        }
        if (b[i] < 0) {
            bigger0 = false;
        }
    }
    bool pi = true;
    for (auto &i : p) {
        cin >> i;
        if (i != -1) {
            pi = false;
        }
    }
    if (les0) {
        int ans = 0;
        int t = 0;
        if (pi) {
            vector<int> pref(n);
            for (int i = 0; i < n; i++) {
                pref[i] = ((i > 0? pref[i - 1] : 0) + a[i]);
                ans = max(ans, pref[i]);
            }
        } else {
            vector<int> pref(n);
            for (int i = 0; i < n; i++) {
                pref[i] = ((i > 0? pref[i - 1] : 0) + a[i]);
                ans = max(ans, pref[i]);
            }
        }
        cout << ans << '\n';
    } else if (bigger0) {
        int ans = 0;
        if (pi) {
            vector<int> pref(n);
            for (int i = 0; i < n; i++) {
                pref[i] = ((i > 0? pref[i - 1] : 0) + a[i] + i * b[i]);
                ans = max(ans, pref[i]);
            }
        } else {
            vector<int> t(n);
            for (int i = 0; i < n; i++) {
                t[i] = i;
            }
            int minK = 0;
            for (int i = 0; i < n; i++) {
                minK = max(minK, p[i]);
            }
            int already = 0;
            for (int i = 0; i < n; i++) {
                if (p[i] != -1) {
                    for (int j = already; j < p[i]; j++) {
                        t[j] = min(i, t[j]);
                    }
                    already = p[i];
                }
            }
            vector<int> pref(n);
            for (int i = 0; i < n; i++) {
                pref[i] = (i > 0? pref[i - 1] : 0) + a[i] + b[i] * t[i];
            }
            ans = pref[minK - 1];
            for (int i = minK; i < n; i++) {
                ans = max(ans, pref[i]);
            }
        }
        cout << ans << '\n';
    } else {
        if (pi) {
            int mx = -1e18;
            vector<int> time(n);
            for (int k = 0; k <= n; k++) {
                int sumb = 0;
                int last = -1;
                vector<int> pref(n);
                for (int i = 0; i < k; i++) {
                    sumb += b[i];
                    pref[i] = (i > 0? pref[i - 1] : 0) + b[i];
                    if (b[i] < 0) {
                        last = i;
                    }
                }
                gen(0, k, a, b, sumb, 0, 0, mx, last, pref);
            }
            cout << mx << '\n';
        } else {

        }
    }
}

signed main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(SIGNES);

    int q = 1;

    #ifdef LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    cin >> q;
    #endif

    while (q--) {
        solve();
    }

}