#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > v[105];
int n, m;
int flag = 0;
bool visited[101];
void dfs(int x, int y, int p, int j) {
  int i;
  if (visited[x]) {
    return;
  }
  if (x == y) {
    flag = 1;
    return;
  }
  visited[x] = 1;
  for (i = 0; i < v[x].size(); i++) {
    if (v[x][i].first != p && v[x][i].second == j) {
      dfs(v[x][i].first, y, x, j);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int i, j, k;
  cin >> n >> m;
  int color[105];
  for (i = 0; i < m; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    v[x].push_back(make_pair(y, z));
    v[y].push_back(make_pair(x, z));
  }
  int q;
  cin >> q;
  for (i = 0; i < q; i++) {
    int x, y;
    cin >> x >> y;
    int cnt = 0;
    for (j = 1; j <= m; j++) {
      memset(visited, 0, sizeof(visited));
      flag = 0;
      dfs(x, y, -1, j);
      if (flag == 1) cnt++;
    }
    cout << cnt << "\n";
  }
  return 0;
}
