#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
const long long mod = 1000000007;
int g[110][110][110];
int n, m;
void dfs(int r, int c, vector<int>& mark) {
  if (mark[r]) return;
  mark[r] = 1;
  for (int u = 1; u <= n; u++) {
    if (g[c][r][u]) dfs(u, c, mark);
  }
}
int calc(int a, int b) {
  int ret = 0;
  for (int c = 1; c <= m; c++) {
    vector<int> mark(n + 1);
    dfs(a, c, mark);
    ret += mark[b];
  }
  return ret;
}
void solve(int ncase) {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    g[c][a][b] = 1;
    g[c][b][a] = 1;
  }
  int q;
  cin >> q;
  while (q--) {
    int u, v;
    cin >> u >> v;
    cout << calc(u, v) << endl;
  }
}
int main() {
  int T = 1;
  int ncase = 0;
  while (T--) {
    solve(++ncase);
  }
}
