// Problema Superhedgy, OJI XI 2022
// Solutie in O(n) - 100p
#include <fstream>
using namespace std;
ifstream cin("superhedgy.in");
ofstream cout("superhedgy.out");
typedef long long ll;
const int mxN = 1e5;
struct cladire
{
ll L , H , E;
void read(){cin >> L >> H >> E;}
}A[mxN+10], B[mxN+10];
ll dp[2][2 * mxN+10];
int main()
{
ll n, m, totalA = 0, totalB = 0;
cin >> n ;
for(int i = 1;i<=n;i++)
A[i].read();
cin >> m;
for(int i = 1;i<=m;i++)
B[i].read();
ll state = 0;
dp[0][state] = min(A[1].H, B[1].H + A[1].E + B[1].E);
dp[1][state] = min(B[1].H, A[1].H + A[1].E + B[1].E);
ll indexA = 1, indexB = 1;
totalA += A[1].L;
totalB += B[1].L;
bool changedA = false, changedB = false;
while(indexA < n && indexB < m)
{
if(totalA == totalB)
{
indexA++;
changedA = true;
changedB = true;
indexB++;
totalA += A[indexA].L;
totalB += B[indexB].L;
state++;
}
else if(totalA < totalB)
{
indexA++;
changedA = true;
changedB = false;
totalA += A[indexA].L;
state++;
}
else if(totalA > totalB)
{
indexB++;
changedA = false;
changedB = true;
totalB += B[indexB].L;
state++;
}
ll indexA2 = indexA - changedA;
ll indexB2 = indexB - changedB;
dp[0][state] = dp[0][state-1] + abs(A[indexA].H - A[indexA2].H);
dp[1][state] = dp[1][state-1] + abs(B[indexB].H - B[indexB2].H);
dp[0][state] = min(dp[0][state], dp[1][state] + A[indexA].E + B[indexB].E);
dp[1][state] = min(dp[1][state], dp[0][state] + A[indexA].E + B[indexB].E);
}
while(indexA < n)
{
indexA++;
changedA = true;
changedB = false;
totalA += A[indexA].L;
state++;
ll indexA2 = indexA - changedA;
ll indexB2 = indexB - changedB;
dp[0][state] = dp[0][state-1] + abs(A[indexA].H - A[indexA2].H);
dp[1][state] = dp[1][state-1] + abs(B[indexB].H - B[indexB2].H);
dp[0][state] = min(dp[0][state], dp[1][state] + A[indexA].E + B[indexB].E);
dp[1][state] = min(dp[1][state], dp[0][state] + A[indexA].E + B[indexB].E);
}
while(indexB < m)
{
indexB++;
changedA = false;
changedB = true;
totalB += B[indexB].L;
state++;
ll indexA2 = indexA - changedA;
ll indexB2 = indexB - changedB;
dp[0][state] = dp[0][state-1] + abs(A[indexA].H - A[indexA2].H);
dp[1][state] = dp[1][state-1] + abs(B[indexB].H - B[indexB2].H);
dp[0][state] = min(dp[0][state], dp[1][state] + A[indexA].E + B[indexB].E);
dp[1][state] = min(dp[1][state], dp[0][state] + A[indexA].E + B[indexB].E);
}
cout << totalA + min(dp[0][state] + A[n].H, dp[1][state] + B[m].H);
return 0;
}