#include <bits/stdc++.h>
using namespace std;
class Graph {
map<int, list> mp;
public:
Graph() {
}
void addEdge(int x, int y) {
mp[x].push_back(y);
mp[y].push_back(x);
}
void bfs(int src, int dest) {
map<int, int> dist;
for (auto i : mp) {
dist[i.first] = INT_MAX;
}
queue<int> q;
q.push(src);
dist[src] = 0;
while (!q.empty()) {
int front = q.front();
q.pop();
for (auto nbr : mp[front]) {
if (dist[nbr] == INT_MAX) {
dist[nbr] = 1 + dist[front];
q.push(nbr);
}
}
}
if (dist[dest] != INT_MAX)
cout << dist[dest] << endl;
else {
cout << "-1" << endl;
}
}
};
int main() {
// #ifndef ONLINE_JUDGE
// freopen(“input.txt”, “r”, stdin);
// freopen(“output.txt”, “w”, stdout);
// #endif
int t;
cin >> t;
while (t--) {
Graph g;
int n;
cin >> n;
int board[101] = {0};
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
board[x] = y - x;
}
int m;
cin >> m;
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
board[x] = y - x;
}
for (int i = 1; i <= 100; i++) {
for (int dice = 1; dice <= 6; dice++) {
int j = dice + i;
j += board[j];
if (j <= 100) {
g.addEdge(i, j);
}
}
}
g.addEdge(100, 100);
g.bfs(1, 100);
}
}